Skip to content

Commit

Permalink
feat: helper function to initialize open telemetry SDK (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Muchogoc authored Jul 14, 2021
1 parent e231e52 commit d30641b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 4 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/objx v0.3.0 // indirect
github.com/stretchr/testify v1.7.0
go.opencensus.io v0.23.0
gopkg.in/yaml.v2 v2.4.0 // indirect
go.opentelemetry.io/otel v1.0.0-RC1
go.opentelemetry.io/otel/exporters/jaeger v1.0.0-RC1
go.opentelemetry.io/otel/sdk v1.0.0-RC1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
18 changes: 15 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down Expand Up @@ -315,6 +317,16 @@ go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opencensus.io v0.22.6/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M=
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.opentelemetry.io/otel v1.0.0-RC1 h1:4CeoX93DNTWt8awGK9JmNXzF9j7TyOu9upscEdtcdXc=
go.opentelemetry.io/otel v1.0.0-RC1/go.mod h1:x9tRa9HK4hSSq7jf2TKbqFbtt58/TGk0f9XiEYISI1I=
go.opentelemetry.io/otel/exporters/jaeger v1.0.0-RC1 h1:tVhw2BMSAk248rhdeirOe9hlXKwGHDvVtF7P8F+H2DU=
go.opentelemetry.io/otel/exporters/jaeger v1.0.0-RC1/go.mod h1:FXJnjGCoTQL6nQ8OpFJ0JI1DrdOvMoVx49ic0Hg4+D4=
go.opentelemetry.io/otel/oteltest v1.0.0-RC1 h1:G685iP3XiskCwk/z0eIabL55XUl2gk0cljhGk9sB0Yk=
go.opentelemetry.io/otel/oteltest v1.0.0-RC1/go.mod h1:+eoIG0gdEOaPNftuy1YScLr1Gb4mL/9lpDkZ0JjMRq4=
go.opentelemetry.io/otel/sdk v1.0.0-RC1 h1:Sy2VLOOg24bipyC29PhuMXYNJrLsxkie8hyI7kUlG9Q=
go.opentelemetry.io/otel/sdk v1.0.0-RC1/go.mod h1:kj6yPn7Pgt5ByRuwesbaWcRLA+V7BSDg3Hf8xRvsvf8=
go.opentelemetry.io/otel/trace v1.0.0-RC1 h1:jrjqKJZEibFrDz+umEASeU3LvdVyWKlnTh7XEfwrT58=
go.opentelemetry.io/otel/trace v1.0.0-RC1/go.mod h1:86UHmyHWFEtWjfWPSbu0+d0Pf9Q6e1U+3ViBOc+NXAg=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -678,12 +690,12 @@ gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3M
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
61 changes: 61 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import (
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

// GenerateLatencyBounds is used in generating latency bounds
Expand Down Expand Up @@ -271,3 +278,57 @@ func (m *MetricsResponseWriter) Write(b []byte) (int, error) {
size, err := m.w.Write(b)
return size, err
}

// InitOtelSDK returns an OpenTelemetry TracerProvider configured to use
// the Jaeger exporter for sending traces/spans. The returned
// TracerProvider will also use a Resource configured with all the information
// about the service deployment.
func InitOtelSDK(ctx context.Context, serviceName string) (*tracesdk.TracerProvider, error) {
// Jaeger Exporter initialization
jaegerURL := MustGetEnvVar("JAEGER_URL")

exporter, err := jaeger.New(
jaeger.WithCollectorEndpoint(
jaeger.WithEndpoint(jaegerURL),
),
)
if err != nil {
return nil, err
}

// Environment variables automatically added to the running containers in cloud run
// K_SERVICE The name of the Cloud Run service being run.
service, _ := GetEnvVar("K_SERVICE")

// K_REVISION The name of the Cloud Run revision being run.
revision, _ := GetEnvVar("K_REVISION")

// K_CONFIGURATION The name of the Cloud Run configuration that created the revision.
configuration, _ := GetEnvVar("K_CONFIGURATION")

resource := resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceNameKey.String(MetricsCollectorService(serviceName)),
attribute.String("cloudrun.service", service),
attribute.String("cloudrun.revision", revision),
attribute.String("cloudrun.configuration", configuration),
)

tp := tracesdk.NewTracerProvider(
// Always be sure to batch in production.
tracesdk.WithBatcher(exporter),
tracesdk.WithResource(resource),
)

// Register the TracerProvider as the global so any imported
// instrumentation will default to using it
otel.SetTracerProvider(tp)

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

return tp, nil
}
78 changes: 78 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/savannahghi/serverutils"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
)

func TestRecordGraphqlResolverMetrics(t *testing.T) {
Expand Down Expand Up @@ -328,3 +329,80 @@ func TestGenerateLatencyBounds(t *testing.T) {
})
}
}

func TestInitOtelSDK(t *testing.T) {
type args struct {
ctx context.Context
serviceName string
}
tests := []struct {
name string
args args
want *tracesdk.TracerProvider
wantErr bool
wantPanic bool
}{
{
name: "success:initialize otel sdk",
args: args{
ctx: context.Background(),
serviceName: "test",
},
wantErr: false,
wantPanic: false,
},
{
name: "fail:initialize otel sdk missing environment",
args: args{
ctx: context.Background(),
serviceName: "test",
},
wantErr: true,
wantPanic: true,
},
{
name: "fail:initialize otel sdk missing jaeger env",
args: args{
ctx: context.Background(),
serviceName: "test",
},
wantErr: true,
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
r := recover()
if (r != nil) != tt.wantPanic {
t.Errorf("InitOtelSDK() recover = %v, wantPanic = %v", r, tt.wantPanic)
}
}()

initialEnvironment := os.Getenv("ENVIRONMENT")
initialJaegerEnv := os.Getenv("JAEGER_URL")
if tt.name == "success:initialize otel sdk" {
os.Setenv("JAEGER_URL", "http://jaeger")
os.Setenv("ENVIRONMENT", "staging")
}

if tt.name == "fail:initialize otel sdk missing environment" {
os.Setenv("JAEGER_URL", "http://jaeger")
os.Setenv("ENVIRONMENT", "")
}

if tt.name == "fail:initialize otel sdk missing jaeger env" {
os.Setenv("JAEGER_URL", "")
}

_, err := serverutils.InitOtelSDK(tt.args.ctx, tt.args.serviceName)
if (err != nil) != tt.wantErr {
t.Errorf("InitOtelSDK() error = %v, wantErr %v", err, tt.wantErr)
return
}

os.Setenv("ENVIRONMENT", initialEnvironment)
os.Setenv("JAEGER_URL", initialJaegerEnv)
})
}
}

0 comments on commit d30641b

Please sign in to comment.