From 3588c25b61fc11788697a92fe597a8ef232edc19 Mon Sep 17 00:00:00 2001 From: ww24 Date: Thu, 7 Oct 2021 10:54:27 +0900 Subject: [PATCH 1/3] feat(infra/firestore): add OpenTelemetry instruments --- infra/firestore/conversation.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/infra/firestore/conversation.go b/infra/firestore/conversation.go index c96a54c6..dc5e43aa 100644 --- a/infra/firestore/conversation.go +++ b/infra/firestore/conversation.go @@ -6,6 +6,7 @@ import ( "cloud.google.com/go/firestore" "github.com/ww24/linebot/domain/model" + "go.opentelemetry.io/otel" "google.golang.org/api/iterator" ) @@ -76,6 +77,10 @@ func (c *Conversation) shopping(conversationID model.ConversationID) *firestore. } func (c *Conversation) AddShoppingItem(ctx context.Context, items ...*model.ShoppingItem) error { + tracer := otel.Tracer("infra/firestore") + ctx, span := tracer.Start(ctx, "AddShoppingItem") + defer span.End() + batch := c.cli.Batch() for _, item := range items { if err := item.Validate(); err != nil { @@ -94,6 +99,10 @@ func (c *Conversation) AddShoppingItem(ctx context.Context, items ...*model.Shop } func (c *Conversation) FindShoppingItem(ctx context.Context, conversationID model.ConversationID) ([]*model.ShoppingItem, error) { + tracer := otel.Tracer("infra/firestore") + ctx, span := tracer.Start(ctx, "FindShoppingItem") + defer span.End() + iter := c.shopping(conversationID). OrderBy("created_at", firestore.Asc). OrderBy("order", firestore.Asc). @@ -117,6 +126,10 @@ func (c *Conversation) FindShoppingItem(ctx context.Context, conversationID mode } func (c *Conversation) DeleteShoppingItems(ctx context.Context, conversationID model.ConversationID, ids []string) error { + tracer := otel.Tracer("infra/firestore") + ctx, span := tracer.Start(ctx, "DeleteShoppingItems") + defer span.End() + batch := c.cli.Batch() for _, id := range ids { item := c.shopping(conversationID).Doc(id) @@ -131,6 +144,10 @@ func (c *Conversation) DeleteShoppingItems(ctx context.Context, conversationID m } func (c *Conversation) DeleteAllShoppingItem(ctx context.Context, conversationID model.ConversationID) error { + tracer := otel.Tracer("infra/firestore") + ctx, span := tracer.Start(ctx, "DeleteAllShoppingItem") + defer span.End() + iter := c.shopping(conversationID).DocumentRefs(ctx) batch := c.cli.Batch() @@ -159,6 +176,10 @@ func (c *Conversation) DeleteAllShoppingItem(ctx context.Context, conversationID } func (c *Conversation) SetStatus(ctx context.Context, status *model.ConversationStatus) error { + tracer := otel.Tracer("infra/firestore") + ctx, span := tracer.Start(ctx, "SetStatus") + defer span.End() + if err := status.Validate(); err != nil { return err } @@ -173,6 +194,10 @@ func (c *Conversation) SetStatus(ctx context.Context, status *model.Conversation } func (c *Conversation) GetStatus(ctx context.Context, conversationID model.ConversationID) (*model.ConversationStatus, error) { + tracer := otel.Tracer("infra/firestore") + ctx, span := tracer.Start(ctx, "GetStatus") + defer span.End() + doc, err := c.conversation(conversationID).Collection("status").Doc("#").Get(ctx) if err != nil { return nil, err From f6475eb6d51046ba3213d6153378f235ec71f647 Mon Sep 17 00:00:00 2001 From: ww24 Date: Thu, 7 Oct 2021 12:26:40 +0900 Subject: [PATCH 2/3] chore: update dependencies --- go.mod | 4 ++++ go.sum | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 026d44c3..7ccc5ec1 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/line/line-bot-sdk-go/v7 v7.10.1 github.com/stretchr/testify v1.7.0 go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0 + go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.25.0 go.opentelemetry.io/otel v1.0.1 go.opentelemetry.io/otel/sdk v1.0.1 go.opentelemetry.io/otel/trace v1.0.1 @@ -29,6 +30,7 @@ require ( cloud.google.com/go v0.94.1 // indirect cloud.google.com/go/trace v1.0.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/felixge/httpsnoop v1.0.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.6 // indirect @@ -37,6 +39,8 @@ require ( github.com/ikawaha/kagome-dict v1.0.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect go.opencensus.io v0.23.0 // indirect + go.opentelemetry.io/otel/internal/metric v0.24.0 // indirect + go.opentelemetry.io/otel/metric v0.24.0 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/net v0.0.0-20210716203947-853a461950ff // indirect diff --git a/go.sum b/go.sum index 5cb1a76f..d007620d 100644 --- a/go.sum +++ b/go.sum @@ -233,18 +233,21 @@ go.opentelemetry.io/contrib v0.21.0 h1:RMJ6GlUVzLYp/zmItxTTdAmr1gnpO/HHMFmvjAhvJ go.opentelemetry.io/contrib v0.21.0/go.mod h1:EH4yDYeNoaTqn/8yCWQmfNB78VHfGX2Jt2bvnvzBlGM= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0 h1:Wx7nFnvCaissIUZxPkBqDz2963Z+Cl+PkYbDKzTxDqQ= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0/go.mod h1:E5NNboN0UqSAki0Atn9kVwaN7I+l25gGxDqBueo/74E= -go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.21.0 h1:G1vNyNfKknFvrKVC8ga8EYIECy0s5D/QPW4QPRSMhwc= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.21.0/go.mod h1:JQAtechjxLEL81EjmbRwxBq/XEzGaHcsPuDHAx54hg4= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.25.0 h1:FIbb8m2PtTWjvXLHOEnXAoSmkaiXbg3fuvoZAjsAT3Q= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.25.0/go.mod h1:NyB05cd+yPX6W5SiRNuJ90w7PV2+g2cgRbsPL7MvpME= go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo= go.opentelemetry.io/otel v1.0.0-RC1/go.mod h1:x9tRa9HK4hSSq7jf2TKbqFbtt58/TGk0f9XiEYISI1I= go.opentelemetry.io/otel v1.0.0-RC2/go.mod h1:w1thVQ7qbAy8MHb0IFj8a5Q2QU0l2ksf8u/CN8m3NOM= go.opentelemetry.io/otel v1.0.1 h1:4XKyXmfqJLOQ7feyV5DB6gsBFZ0ltB8vLtp6pj4JIcc= go.opentelemetry.io/otel v1.0.1/go.mod h1:OPEOD4jIT2SlZPMmwT6FqZz2C0ZNdQqiWcoK6M0SNFU= -go.opentelemetry.io/otel/internal/metric v0.21.0 h1:gZlIBo5O51hZOOZz8vEcuRx/l5dnADadKfpT70AELoo= go.opentelemetry.io/otel/internal/metric v0.21.0/go.mod h1:iOfAaY2YycsXfYD4kaRSbLx2LKmfpKObWBEv9QK5zFo= +go.opentelemetry.io/otel/internal/metric v0.24.0 h1:O5lFy6kAl0LMWBjzy3k//M8VjEaTDWL9DPJuqZmWIAA= +go.opentelemetry.io/otel/internal/metric v0.24.0/go.mod h1:PSkQG+KuApZjBpC6ea6082ZrWUUy/w132tJ/LOU3TXk= go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU= -go.opentelemetry.io/otel/metric v0.21.0 h1:ZtcJlHqVE4l8Su0WOLOd9fEPheJuYEiQ0wr9wv2p25I= go.opentelemetry.io/otel/metric v0.21.0/go.mod h1:JWCt1bjivC4iCrz/aCrM1GSw+ZcvY44KCbaeeRhzHnc= +go.opentelemetry.io/otel/metric v0.24.0 h1:Rg4UYHS6JKR1Sw1TxnI13z7q/0p/XAbgIqUTagvLJuU= +go.opentelemetry.io/otel/metric v0.24.0/go.mod h1:tpMFnCD9t+BEGiWY2bWF5+AwjuAdM0lSowQ4SBA3/K4= go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw= go.opentelemetry.io/otel/oteltest v1.0.0-RC1/go.mod h1:+eoIG0gdEOaPNftuy1YScLr1Gb4mL/9lpDkZ0JjMRq4= go.opentelemetry.io/otel/sdk v1.0.0-RC2/go.mod h1:fgwHyiDn4e5k40TD9VX243rOxXR+jzsWBZYA2P5jpEw= From b1d663718901e6520493065238744503dff7ae36 Mon Sep 17 00:00:00 2001 From: ww24 Date: Thu, 7 Oct 2021 12:27:37 +0900 Subject: [PATCH 3/3] fix(bot): add otelhttp transport for http client to call line api --- bot/bot.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index ad7935d1..476b77c6 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -7,10 +7,12 @@ import ( "github.com/google/wire" "github.com/line/line-bot-sdk-go/v7/linebot" - "github.com/ww24/linebot/domain/model" - "github.com/ww24/linebot/logger" + "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.uber.org/zap" "golang.org/x/xerrors" + + "github.com/ww24/linebot/domain/model" + "github.com/ww24/linebot/logger" ) var Set = wire.NewSet( @@ -40,7 +42,7 @@ func New( log *logger.Logger, shopping *ShoppingService, ) (*Bot, error) { - hc := &http.Client{} + hc := &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport)} cli, err := linebot.New(c.ChannelSecret, c.ChannelToken, linebot.WithHTTPClient(hc)) if err != nil { return nil, xerrors.Errorf("failed to initialize LINE Bot client: %w", err)