Skip to content

Commit 16269a2

Browse files
Ivan RylachivanRylach
authored andcommitted
add interfaces to enable out-of-the-box instrumentations
1 parent b9cdf1e commit 16269a2

15 files changed

+110
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Plugin - [`idempotent-requests`](https://github.com/checkr/kong-plugin-idempoten
2727
| --- | --- | --- | --- |
2828
| `MONGODB_URI` | yes | `mongodb://root:password123@localhost:27017` | URI to connect to MongoDB |
2929
| `OPENTRACING_ENABLED` | no | `true` | Enable `opentracing` |
30-
| `OPENTRACING_TRACER_IMPLEMENTATION` | no | `datadog` | Use Datadog tracer implementation |
30+
| `OPENTRACING_TRACER_IMPLEMENTATION` | no | `datadog` | [Use Datadog tracer implementation](https://docs.datadoghq.com/tracing/setup_overview/setup/go/?tab=containers) |
3131

3232
## Example topology
3333

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
7070
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
7171
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
7272
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
73-
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
7473
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
7574
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
7675
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
@@ -89,6 +88,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
8988
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
9089
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9190
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
91+
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
9292
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
9393
github.com/google/pprof v0.0.0-20210423192551-a2663126120b/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
9494
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
@@ -147,6 +147,7 @@ github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
147147
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
148148
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
149149
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
150+
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
150151
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
151152
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
152153
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ func main() {
1919
zap.ReplaceGlobals(logger)
2020
defer logger.Sync()
2121

22+
var tracer tracing.Tracer
2223
if tracing.GetConfig().TracingEnabled {
23-
tracers := tracing.NewTracer()
24-
opentracing.SetGlobalTracer(tracers.OpentracingTracer())
25-
defer tracers.Stop()
24+
tracer = tracing.NewTracer()
25+
opentracing.SetGlobalTracer(tracer.OpentracingTracer())
26+
defer tracer.Stop()
2627
}
2728

28-
mongo := mongodb.NewClient()
29+
mongo := mongodb.NewClient(tracer)
2930
capturesRepo := captures_mongo.NewRepository(mongo)
3031

31-
httpServer := http.NewServer(capturesRepo)
32+
httpServer := http.NewServer(tracer, capturesRepo)
3233
go httpServer.Start()
3334

3435
waitForShutdownSignal()

pkg/http/captures_allocate_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package http
33
import (
44
"bytes"
55
"checkr.com/idempotent-requests/pkg/captures/captures_mock"
6+
"checkr.com/idempotent-requests/pkg/tracing"
67
"encoding/base64"
78
"encoding/json"
89
"github.com/stretchr/testify/assert"
@@ -16,7 +17,9 @@ const CapturesV2URL = "/api/v2/captures"
1617

1718
func TestCaptures_Allocate(t *testing.T) {
1819
capturesRepo := captures_mock.NewRepositoryImpl()
19-
router := NewRouter(capturesRepo)
20+
tracer := tracing.MockTracer{}
21+
22+
router := NewRouter(tracer, capturesRepo)
2023

2124
tests := []struct {
2225
name string

pkg/http/captures_record_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package http
22

33
import (
44
"checkr.com/idempotent-requests/pkg/captures/captures_mock"
5+
"checkr.com/idempotent-requests/pkg/tracing"
56
"checkr.com/idempotent-requests/pkg/views"
67
"encoding/base64"
78
"github.com/gin-gonic/gin"
@@ -21,7 +22,9 @@ const (
2122

2223
func TestCaptures_Record(t *testing.T) {
2324
capturesRepo := captures_mock.NewRepositoryImpl()
24-
router := NewRouter(capturesRepo)
25+
tracer := tracing.MockTracer{}
26+
27+
router := NewRouter(tracer, capturesRepo)
2528

2629
tests := []struct {
2730
name string

pkg/http/handler_allocate_capture.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func NewAllocateCaptureHandler(capturesRepo captures.Repository) Handler {
2020
}
2121

2222
func (h AllocateCaptureHandler) Handle(c *gin.Context) {
23+
2324
var attemptedAllocation views.CaptureAllocation
2425
if err := c.BindJSON(&attemptedAllocation); err != nil {
2526
c.JSON(http.StatusBadRequest, views.MalformedPayload)
@@ -31,7 +32,7 @@ func (h AllocateCaptureHandler) Handle(c *gin.Context) {
3132
return
3233
}
3334

34-
allocation, err := h.capturesRepo.Allocate(c, attemptedAllocation.IdempotencyKey)
35+
allocation, err := h.capturesRepo.Allocate(c.Request.Context(), attemptedAllocation.IdempotencyKey)
3536

3637
if err != nil {
3738
zap.S().Errorw("failed to allocate a capture", "err", err.Error())

pkg/http/handler_readiness.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func NewReadinessHandler(capturesRepo captures.Repository) Handler {
1818

1919
func (h ReadinessHandler) Handle(c *gin.Context) {
2020

21-
ready := h.capturesRepo.Ready(c)
21+
ready := h.capturesRepo.Ready(c.Request.Context())
2222

2323
if ready {
2424
c.Status(http.StatusOK)

pkg/http/handler_record_capture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (h RecordCaptureHandler) Handle(c *gin.Context) {
3535

3636
allocation := adapters.CaptureRecordToAllocation(&capture)
3737

38-
if err := h.capturesRepo.Record(c, allocation); err != nil {
38+
if err := h.capturesRepo.Record(c.Request.Context(), allocation); err != nil {
3939

4040
if err == captures.ErrConflictOrMissing {
4141
c.JSON(http.StatusForbidden, views.CaptureIsCompleted)

pkg/http/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package http
22

33
import (
44
"checkr.com/idempotent-requests/pkg/captures"
5+
"checkr.com/idempotent-requests/pkg/tracing"
56
"context"
67
"github.com/gin-contrib/zap"
78
"github.com/gin-gonic/gin"
@@ -14,8 +15,8 @@ type Server struct {
1415
Server *http.Server
1516
}
1617

17-
func NewServer(records captures.Repository) *Server {
18-
router := NewRouter(records)
18+
func NewServer(tracer tracing.Tracer, records captures.Repository) *Server {
19+
router := NewRouter(tracer, records)
1920

2021
srv := &http.Server{
2122
Addr: ":8080",
@@ -26,10 +27,11 @@ func NewServer(records captures.Repository) *Server {
2627
return &Server{Server: srv}
2728
}
2829

29-
func NewRouter(captures captures.Repository) *gin.Engine {
30+
func NewRouter(tracer tracing.Tracer, captures captures.Repository) *gin.Engine {
3031
gin.SetMode(gin.ReleaseMode)
3132
router := gin.New()
3233

34+
tracer.Gin(router)
3335
router.Use(ginzap.Ginzap(zap.S().Desugar(), time.RFC822, false))
3436
router.Use(ginzap.RecoveryWithZap(zap.S().Desugar(), false))
3537

pkg/mongodb/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mongodb
22

33
import (
4+
"checkr.com/idempotent-requests/pkg/tracing"
45
"context"
56
"go.mongodb.org/mongo-driver/mongo"
67
"go.mongodb.org/mongo-driver/mongo/options"
@@ -12,9 +13,13 @@ type Client struct {
1213
Mongo *mongo.Client
1314
}
1415

15-
func NewClient() *Client {
16+
func NewClient(tracer tracing.Tracer) *Client {
1617
cfg := GetConfig()
17-
client, err := mongo.NewClient(options.Client().ApplyURI(cfg.URI))
18+
opts := options.Client().ApplyURI(cfg.URI)
19+
20+
opts = tracer.MongoDB(opts)
21+
22+
client, err := mongo.NewClient(opts)
1823
if err != nil {
1924
zap.S().Panic(err)
2025
}

0 commit comments

Comments
 (0)