88 "strconv"
99 "time"
1010
11+ "github.com/NdoleStudio/go-otelroundtripper"
12+ "go.opentelemetry.io/otel/metric/global"
13+
1114 lemonsqueezy "github.com/NdoleStudio/lemonsqueezy-go"
1215 "github.com/hashicorp/go-retryablehttp"
1316
@@ -20,7 +23,6 @@ import (
2023 cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
2124
2225 "go.opentelemetry.io/otel"
23- "go.opentelemetry.io/otel/attribute"
2426 "go.opentelemetry.io/otel/sdk/resource"
2527 semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
2628
@@ -58,24 +60,26 @@ import (
5860type Container struct {
5961 projectID string
6062 db * gorm.DB
63+ version string
6164 app * fiber.App
6265 eventDispatcher * services.EventDispatcher
6366 logger telemetry.Logger
6467}
6568
6669// NewContainer creates a new dependency injection container
67- func NewContainer (projectID string ) (container * Container ) {
70+ func NewContainer (projectID string , version string ) (container * Container ) {
6871 // Set location to UTC
6972 now .DefaultConfig = & now.Config {
7073 TimeLocation : time .UTC ,
7174 }
7275
7376 container = & Container {
7477 projectID : projectID ,
78+ version : version ,
7579 logger : logger (3 ).WithService (fmt .Sprintf ("%T" , container )),
7680 }
7781
78- container .InitializeTraceProvider ("0.0.1" , os . Getenv ( "GCP_PROJECT_ID" ) )
82+ container .InitializeTraceProvider ()
7983
8084 container .RegisterMessageListeners ()
8185 container .RegisterMessageRoutes ()
@@ -572,19 +576,31 @@ func (container *Container) HTTPClient(name string) *http.Client {
572576 container .logger .Debug (fmt .Sprintf ("creating %s %T" , name , http .DefaultClient ))
573577 return & http.Client {
574578 Timeout : 60 * time .Second ,
575- Transport : container .RetryHTTPRoundTripper ( ),
579+ Transport : container .HTTPRoundTripper ( name ),
576580 }
577581}
578582
579- //func (container *Container) HTTPRoundTripper(name string) http.RoundTripper {
580- // container.logger.Debug(fmt.Sprintf("Debug: initializing %s %T", name, http.DefaultTransport))
581- // return otelroundtripper.New(
582- // otelroundtripper.WithName(name),
583- // otelroundtripper.WithParent(container.RetryHTTPRoundTripper()),
584- // otelroundtripper.WithMeter(global.Meter(os.Getenv("NAMESPACE"))),
585- // otelroundtripper.WithAttributes(initializers.InitializeOtelResources(container.Version, container.Namespace).Attributes()...),
586- // )
587- //}
583+ // HTTPRoundTripper creates an open telemetry http.RoundTripper
584+ func (container * Container ) HTTPRoundTripper (name string ) http.RoundTripper {
585+ container .logger .Debug (fmt .Sprintf ("Debug: initializing %s %T" , name , http .DefaultTransport ))
586+ return otelroundtripper .New (
587+ otelroundtripper .WithName (name ),
588+ otelroundtripper .WithParent (container .RetryHTTPRoundTripper ()),
589+ otelroundtripper .WithMeter (global .Meter (container .projectID )),
590+ otelroundtripper .WithAttributes (container .OtelResources (container .version , container .projectID ).Attributes ()... ),
591+ )
592+ }
593+
594+ // OtelResources generates default open telemetry resources
595+ func (container * Container ) OtelResources (version string , namespace string ) * resource.Resource {
596+ return resource .NewWithAttributes (
597+ semconv .SchemaURL ,
598+ semconv .ServiceNameKey .String (namespace ),
599+ semconv .ServiceVersionKey .String (version ),
600+ semconv .ServiceInstanceIDKey .String (hostName ()),
601+ semconv .DeploymentEnvironmentKey .String (os .Getenv ("ENV" )),
602+ )
603+ }
588604
589605// RetryHTTPRoundTripper creates a retryable http.RoundTripper
590606func (container * Container ) RetryHTTPRoundTripper () http.RoundTripper {
@@ -961,11 +977,11 @@ func (container *Container) UserRepository() repositories.UserRepository {
961977}
962978
963979// InitializeTraceProvider initializes the open telemetry trace provider
964- func (container * Container ) InitializeTraceProvider (version string , namespace string ) func () {
980+ func (container * Container ) InitializeTraceProvider () func () {
965981 if isLocal () {
966- return container .initializeUptraceProvider (version , namespace )
982+ return container .initializeUptraceProvider (container . version , container . projectID )
967983 }
968- return container .initializeGoogleTraceProvider (version , namespace )
984+ return container .initializeGoogleTraceProvider (container . version , container . projectID )
969985}
970986
971987func (container * Container ) initializeGoogleTraceProvider (version string , namespace string ) func () {
@@ -979,7 +995,7 @@ func (container *Container) initializeGoogleTraceProvider(version string, namesp
979995 tp := trace .NewTracerProvider (
980996 trace .WithBatcher (exporter ),
981997 trace .WithSampler (trace .AlwaysSample ()),
982- trace .WithResource (container .InitializeOtelResources (version , namespace )),
998+ trace .WithResource (container .OtelResources (version , namespace )),
983999 )
9841000
9851001 otel .SetTracerProvider (tp )
@@ -996,6 +1012,7 @@ func (container *Container) initializeUptraceProvider(version string, namespace
9961012 uptrace .WithDSN (os .Getenv ("UPTRACE_DSN" )),
9971013 uptrace .WithServiceName (namespace ),
9981014 uptrace .WithServiceVersion (version ),
1015+ uptrace .WithDeploymentEnvironment (os .Getenv ("ENV" )),
9991016 )
10001017
10011018 // Send buffered spans and free resources.
@@ -1007,18 +1024,6 @@ func (container *Container) initializeUptraceProvider(version string, namespace
10071024 }
10081025}
10091026
1010- // InitializeOtelResources initializes open telemetry resources
1011- func (container * Container ) InitializeOtelResources (version string , namespace string ) * resource.Resource {
1012- return resource .NewWithAttributes (
1013- semconv .SchemaURL ,
1014- semconv .ServiceNameKey .String (namespace ),
1015- semconv .ServiceNamespaceKey .String (namespace ),
1016- semconv .ServiceVersionKey .String (version ),
1017- semconv .ServiceInstanceIDKey .String (hostName ()),
1018- attribute .String ("service.environment" , os .Getenv ("ENV" )),
1019- )
1020- }
1021-
10221027func logger (skipFrameCount int ) telemetry.Logger {
10231028 fields := map [string ]string {
10241029 "pid" : strconv .Itoa (os .Getpid ()),
0 commit comments