Skip to content

Commit

Permalink
style(otel): fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bradub committed Jun 13, 2024
1 parent 4efda09 commit e38f013
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
2 changes: 1 addition & 1 deletion otel/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.27.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
go.opentelemetry.io/otel/trace v1.27.0
go.uber.org/zap v1.27.0
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.64.0
Expand Down Expand Up @@ -47,7 +48,6 @@ require (
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.52.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.52.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
Expand Down
63 changes: 52 additions & 11 deletions otel/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package otel

import (
"context"
"errors"
"fmt"
"time"

Expand All @@ -13,42 +14,82 @@ import (
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/embedded"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

var _ trace.TracerProvider = (*TracerProvider)(nil)

// TracerProvider is a wrapper around the GRPC OpenTelemetry TracerProvider.
type TracerProvider struct {
embedded.TracerProvider

conn *grpc.ClientConn
tp *sdktrace.TracerProvider
}

// Tracer returns a named tracer.
func (t *TracerProvider) Tracer(name string, options ...trace.TracerOption) trace.Tracer {
return t.tp.Tracer(name, options...)
}

// Close closes the TracerProvider and the GRPC Conn.
func (t *TracerProvider) Close() error {
var errs error

const timeout = 10 * time.Second

timeoutCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

if err := t.tp.Shutdown(timeoutCtx); err != nil {
errs = errors.Join(errs, fmt.Errorf("shutdown: %w", err))
}

if err := t.conn.Close(); err != nil {
errs = errors.Join(errs, fmt.Errorf("close connection: %w", err))
}

return errs
}

// Init initializes the OpenTelemetry SDK with the OTLP exporter.
func Init(
ctx context.Context,
otelCollectorEndopoint string,
otelCollectorEndpoint string,
serviceName string,
) error {
oltpCollectorConn, err := newOTLPCollectorConn(otelCollectorEndopoint)
) (*TracerProvider, error) {
oltpCollectorConn, err := newOTLPCollectorConn(otelCollectorEndpoint)
if err != nil {
return fmt.Errorf("failed to create otlp collector connection: %w", err)
return nil, fmt.Errorf("new otlp collector connection: %w", err)
}

traceProvider, err := newTraceProvider(ctx, oltpCollectorConn, serviceName)
tracerProvider, err := newTracerProvider(ctx, oltpCollectorConn, serviceName)
if err != nil {
return fmt.Errorf("failed to create trace provider: %w", err)
return nil, fmt.Errorf("new trace provider: %w", err)
}

// Start runtime instrumentation
// report runtime metrics
if err := runtime.Start(runtime.WithMinimumReadMemStatsInterval(time.Second)); err != nil {
return fmt.Errorf("failed to start runtime instrumentation: %w", err)
return nil, fmt.Errorf("start runtime instrumentation: %w", err)
}

// Set the global TracerProvider.
otel.SetTracerProvider(traceProvider)
otel.SetTracerProvider(tracerProvider)

otel.SetTextMapPropagator(
propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{}, propagation.Baggage{},
),
)

return nil
return &TracerProvider{
tp: tracerProvider,
conn: oltpCollectorConn,
}, nil
}

func newOTLPCollectorConn(
Expand All @@ -65,7 +106,7 @@ func newOTLPCollectorConn(
return conn, err
}

func newTraceProvider(
func newTracerProvider(
ctx context.Context,
otlpCollectorConn *grpc.ClientConn,
serviceName string,
Expand Down
14 changes: 7 additions & 7 deletions otel/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"testing"

commonsgrpc "github.com/purposeinplay/go-commons/grpc"
"github.com/purposeinplay/go-commons/grpc/grpcclient"
"github.com/purposeinplay/go-commons/grpc/test_data/greetpb"
"github.com/purposeinplay/go-commons/otel"
"github.com/stretchr/testify/require"
ootel "go.opentelemetry.io/otel"
"go.uber.org/zap"
"google.golang.org/grpc/test/bufconn"
"github.com/purposeinplay/go-commons/grpc/grpcclient"
"github.com/purposeinplay/go-commons/grpc/test_data/greetpb"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc"
ootel "go.opentelemetry.io/otel"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/test/bufconn"
)

func TestIntegration(t *testing.T) {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *greeterService) Greet(
) (*greetpb.GreetResponse, error) {
tracer := ootel.Tracer("test")

ctx, span := tracer.Start(ctx, "greet")
spanCtx, span := tracer.Start(ctx, "greet")
defer span.End()

if s.greetFunc != nil {
Expand All @@ -114,7 +114,7 @@ func (s *greeterService) Greet(

res := req.Greeting.FirstName + req.Greeting.LastName

if md, ok := metadata.FromIncomingContext(ctx); ok {
if md, ok := metadata.FromIncomingContext(spanCtx); ok {
if len(md["custom"]) > 0 {
res += md["custom"][0]
}
Expand Down

0 comments on commit e38f013

Please sign in to comment.