Skip to content

Commit

Permalink
otel tracing rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Kleonikos Kyriakis committed Apr 29, 2024
1 parent 678285b commit 1579f0e
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 56 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/ava-labs/avalanchego v1.10.18
github.com/ava-labs/hypersdk v0.0.16
github.com/chain4travel/caminotravelvm v0.0.0-20240419161941-a32dadd85f51
github.com/google/uuid v1.4.0
github.com/klauspost/compress v1.17.3
github.com/mattn/go-sqlite3 v1.14.19
github.com/spf13/pflag v1.0.5
Expand Down
1 change: 0 additions & 1 deletion internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package app

import (
"context"

"fmt"

"github.com/chain4travel/camino-messenger-bot/config"
Expand Down
2 changes: 0 additions & 2 deletions internal/matrix/matrix_messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"sync"
"time"

"github.com/chain4travel/camino-messenger-bot/internal/compression"

"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/chain4travel/camino-messenger-bot/config"
Expand Down
44 changes: 23 additions & 21 deletions internal/matrix/mock_room_handler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions internal/matrix/room_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type Client interface {
IsEncrypted(ctx context.Context, roomID id.RoomID) bool
IsEncrypted(ctx context.Context, roomID id.RoomID) (bool, error)
CreateRoom(ctx context.Context, req *mautrix.ReqCreateRoom) (*mautrix.RespCreateRoom, error)
SendStateEvent(ctx context.Context, roomID id.RoomID, eventType event.Type, stateKey string, content interface{}) (*mautrix.RespSendEvent, error)
JoinedRooms(ctx context.Context) (*mautrix.RespJoinedRooms, error)
Expand All @@ -23,7 +23,7 @@ type wrappedClient struct {
*mautrix.Client
}

func (c *wrappedClient) IsEncrypted(ctx context.Context, roomID id.RoomID) bool {
func (c *wrappedClient) IsEncrypted(ctx context.Context, roomID id.RoomID) (bool, error) {
return c.StateStore.IsEncrypted(ctx, roomID)
}

Expand All @@ -32,7 +32,7 @@ func NewClient(mautrixClient *mautrix.Client) Client {
}

type RoomHandler interface {
GetOrCreateRoomForRecipient(recipient id.UserID) (id.RoomID, error)
GetOrCreateRoomForRecipient(ctx context.Context, recipient id.UserID) (id.RoomID, error)
}
type roomHandler struct {
client Client
Expand Down Expand Up @@ -99,7 +99,7 @@ func (r *roomHandler) getEncryptedRoomForRecipient(ctx context.Context, recipien
return "", false
}
for _, roomID := range rooms.JoinedRooms {
if !r.client.IsEncrypted(ctx, roomID) {
if encrypted, err := r.client.IsEncrypted(ctx, roomID); err != nil || !encrypted {
continue
}
members, err := r.client.JoinedMembers(ctx, roomID)
Expand Down
37 changes: 19 additions & 18 deletions internal/matrix/room_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package matrix

import (
"context"
"errors"
"testing"

Expand Down Expand Up @@ -51,9 +52,9 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) {
rooms: map[id.UserID]id.RoomID{},
},
mocks: func(*roomHandler) {
mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(false)
mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{
mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(false, nil)
mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{
Visibility: "private",
Preset: "private_chat",
Invite: []id.UserID{userID},
Expand All @@ -67,14 +68,14 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) {
rooms: map[id.UserID]id.RoomID{},
},
mocks: func(*roomHandler) {
mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(false)
mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{
mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(false, nil)
mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{
Visibility: "private",
Preset: "private_chat",
Invite: []id.UserID{userID},
}).Times(1).Return(&mautrix.RespCreateRoom{RoomID: newRoomID}, nil)
mockRoomClient.EXPECT().SendStateEvent(newRoomID, event.StateEncryption, "",
mockRoomClient.EXPECT().SendStateEvent(gomock.Any(), newRoomID, event.StateEncryption, "",
event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}).Times(1).Return(nil, errEnableEncryptionFailed)
},
args: args{recipient: userID},
Expand All @@ -92,9 +93,9 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) {
rooms: map[id.UserID]id.RoomID{},
},
mocks: func(*roomHandler) {
mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(true)
mockRoomClient.EXPECT().JoinedMembers(roomID).Times(1).Return(&mautrix.RespJoinedMembers{Joined: map[id.UserID]mautrix.JoinedMember{userID: {}}}, nil)
mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(true, nil)
mockRoomClient.EXPECT().JoinedMembers(gomock.Any(), roomID).Times(1).Return(&mautrix.RespJoinedMembers{Joined: map[id.UserID]mautrix.JoinedMember{userID: {}}}, nil)
},
args: args{recipient: userID},
want: roomID,
Expand All @@ -104,13 +105,13 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) {
rooms: map[id.UserID]id.RoomID{},
},
mocks: func(*roomHandler) {
mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{}}, nil)
mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{
mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{}}, nil)
mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{
Visibility: "private",
Preset: "private_chat",
Invite: []id.UserID{userID},
}).Times(1).Return(&mautrix.RespCreateRoom{RoomID: newRoomID}, nil)
mockRoomClient.EXPECT().SendStateEvent(newRoomID, event.StateEncryption, "",
mockRoomClient.EXPECT().SendStateEvent(gomock.Any(), newRoomID, event.StateEncryption, "",
event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}).Times(1).Return(nil, nil)
},
args: args{recipient: userID},
Expand All @@ -121,14 +122,14 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) {
rooms: map[id.UserID]id.RoomID{},
},
mocks: func(*roomHandler) {
mockRoomClient.EXPECT().JoinedRooms().Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(roomID).Times(1).Return(false)
mockRoomClient.EXPECT().CreateRoom(&mautrix.ReqCreateRoom{
mockRoomClient.EXPECT().JoinedRooms(gomock.Any()).Times(1).Return(&mautrix.RespJoinedRooms{JoinedRooms: []id.RoomID{roomID}}, nil)
mockRoomClient.EXPECT().IsEncrypted(gomock.Any(), roomID).Times(1).Return(false, nil)
mockRoomClient.EXPECT().CreateRoom(gomock.Any(), &mautrix.ReqCreateRoom{
Visibility: "private",
Preset: "private_chat",
Invite: []id.UserID{userID},
}).Times(1).Return(&mautrix.RespCreateRoom{RoomID: newRoomID}, nil)
mockRoomClient.EXPECT().SendStateEvent(newRoomID, event.StateEncryption, "",
mockRoomClient.EXPECT().SendStateEvent(gomock.Any(), newRoomID, event.StateEncryption, "",
event.EncryptionEventContent{Algorithm: id.AlgorithmMegolmV1}).Times(1).Return(nil, nil)
},
args: args{recipient: userID},
Expand All @@ -146,7 +147,7 @@ func TestGetOrCreateRoomForRecipient(t *testing.T) {
tt.mocks(r)
}

got, err := r.GetOrCreateRoomForRecipient(tt.args.recipient)
got, err := r.GetOrCreateRoomForRecipient(context.Background(), tt.args.recipient)
require.ErrorIs(t, err, tt.err, "GetOrCreateRoomForRecipient() error = %w, wantErr %w", err, tt.err)
require.Equal(t, got, tt.want, "GetOrCreateRoomForRecipient() got = %v, expRoomID %v", got, tt.want)
})
Expand Down
2 changes: 1 addition & 1 deletion internal/messaging/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

"github.com/chain4travel/camino-messenger-bot/config"
"github.com/chain4travel/camino-messenger-bot/internal/metadata"
"go.uber.org/zap"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"google.golang.org/grpc"
grpc_metadata "google.golang.org/grpc/metadata"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,4 @@ func (s *server) processMetadata(ctx context.Context, id trace.TraceID) (metadat
md.Stamp(fmt.Sprintf("%s-%s", s.Checkpoint(), "received"))
err := md.ExtractMetadata(ctx)
return md, err
}
}
12 changes: 7 additions & 5 deletions internal/tracing/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ package tracing
import (
"context"
"fmt"
"time"

"github.com/chain4travel/camino-messenger-bot/config"
utils "github.com/chain4travel/camino-messenger-bot/utils/tls"
"time"

"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/trace"
)

const exportTimeout = 10 * time.Second
const exporterInstantiationTimeout = 5 * time.Second
const (
exportTimeout = 10 * time.Second
exporterInstantiationTimeout = 5 * time.Second
)

func newExporter(cfg *config.TracingConfig) (trace.SpanExporter, error) {

var client otlptrace.Client
opts := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)),
Expand All @@ -32,7 +34,7 @@ func newExporter(cfg *config.TracingConfig) (trace.SpanExporter, error) {
} else {
creds, err := utils.LoadTLSCredentials(cfg.CertFile, cfg.KeyFile)
if err != nil {
return nil, fmt.Errorf("could not load TLS keys: %s", err)
return nil, fmt.Errorf("could not load TLS keys: %w", err)
}
opts = append(opts, otlptracegrpc.WithTLSCredentials(creds))
}
Expand Down
3 changes: 3 additions & 0 deletions internal/tracing/nooptracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tracing

import (
"context"

"go.opentelemetry.io/otel/trace"
)

Expand All @@ -19,9 +20,11 @@ type noopTracer struct {
func NewNoOpTracer() (Tracer, error) {
return &noopTracer{tp: trace.NewNoopTracerProvider()}, nil
}

func (n *noopTracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return n.tp.Tracer("").Start(ctx, spanName, opts...)
}

func (n *noopTracer) Shutdown() error {
return nil // nothing to do here
}
4 changes: 3 additions & 1 deletion internal/tracing/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ package tracing

import (
"context"
"time"

"github.com/chain4travel/camino-messenger-bot/config"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
"go.opentelemetry.io/otel/trace"
"time"
)

const tracerProviderShutdownTimeout = exportTimeout + 5*time.Second
Expand All @@ -32,6 +33,7 @@ type tracer struct {
func (t *tracer) Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) {
return t.tp.Tracer("").Start(ctx, spanName, opts...)
}

func (t *tracer) Shutdown() error {
ctx, cancel := context.WithTimeout(context.Background(), tracerProviderShutdownTimeout)
defer cancel()
Expand Down
1 change: 0 additions & 1 deletion scripts/mocks.mockgen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ github.com/chain4travel/camino-messenger-bot/internal/matrix=Client=internal/mat
buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/activity/v1alpha/activityv1alphagrpc=ActivityProductListServiceClient=internal/messaging/mock_list_grpc.pb.go
github.com/chain4travel/camino-messenger-bot/internal/messaging=ServiceRegistry=internal/messaging/mock_service_registry.go
github.com/chain4travel/camino-messenger-bot/internal/messaging=Messenger=internal/messaging/mock_messenger.go

0 comments on commit 1579f0e

Please sign in to comment.