-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
572 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" | ||
"go.opentelemetry.io/otel/propagation" | ||
"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" | ||
) | ||
|
||
type Telemetry interface { | ||
Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, Span) | ||
Shutdown(ctx context.Context) | ||
} | ||
|
||
type Span interface { | ||
trace.Span | ||
} | ||
|
||
type OTel struct { | ||
provider *sdktrace.TracerProvider | ||
tracer trace.Tracer | ||
} | ||
|
||
func New(ctx context.Context, serviceName string) (*OTel, error) { | ||
var tp *sdktrace.TracerProvider | ||
var err error | ||
tp, err = createTraceProvider(ctx, serviceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
otel.SetTracerProvider(tp) | ||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) | ||
tracer := tp.Tracer(serviceName) | ||
|
||
return &OTel{ | ||
provider: tp, | ||
tracer: tracer, | ||
}, nil | ||
} | ||
|
||
func (ot *OTel) Start(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, Span) { | ||
if len(opts) == 0 { | ||
return ot.tracer.Start(ctx, name) | ||
} | ||
return ot.tracer.Start(ctx, name, opts[0]) | ||
} | ||
|
||
func (ot *OTel) Shutdown(ctx context.Context) { | ||
ot.provider.Shutdown(ctx) | ||
} | ||
|
||
func createResource(ctx context.Context, serviceName string) (*resource.Resource, error) { | ||
return resource.New(ctx, | ||
resource.WithAttributes( | ||
semconv.ServiceNameKey.String(serviceName), | ||
), | ||
) | ||
} | ||
|
||
func createTraceProvider(ctx context.Context, serviceName string) (*sdktrace.TracerProvider, error) { | ||
res, err := createResource(ctx, serviceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client := otlptracehttp.NewClient() | ||
exp, err := otlptrace.New(ctx, client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tp := sdktrace.NewTracerProvider( | ||
sdktrace.WithBatcher(exp), | ||
sdktrace.WithResource(res), | ||
sdktrace.WithResource(resource.NewWithAttributes( | ||
semconv.SchemaURL, | ||
semconv.ServiceNameKey.String(serviceName), | ||
semconv.DeploymentEnvironmentKey.String("prod"), | ||
)), | ||
) | ||
return tp, nil | ||
} |