Skip to content

Commit

Permalink
Merge pull request #47 from ww24/add-instrument
Browse files Browse the repository at this point in the history
Add instrument
  • Loading branch information
ww24 authored Oct 7, 2021
2 parents d09da56 + b1d6637 commit 0a9e74d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
8 changes: 5 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
25 changes: 25 additions & 0 deletions infra/firestore/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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).
Expand All @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 0a9e74d

Please sign in to comment.