@@ -2,10 +2,13 @@ package sharedotlptraces
22
33import (
44 "context"
5+ "fmt"
6+ "strings"
57
68 sharedotlp "github.com/formancehq/go-libs/sharedotlp/pkg"
79 "go.opentelemetry.io/contrib/propagators/b3"
810 "go.opentelemetry.io/otel"
11+ "go.opentelemetry.io/otel/attribute"
912 "go.opentelemetry.io/otel/exporters/jaeger"
1013 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
1114 "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
@@ -37,29 +40,38 @@ type OTLPConfig struct {
3740}
3841
3942type ModuleConfig struct {
40- Exporter string
41- Batch bool
42- JaegerConfig * JaegerConfig
43- OTLPConfig * OTLPConfig
44- Resource * resource.Resource
43+ Exporter string
44+ Batch bool
45+ JaegerConfig * JaegerConfig
46+ OTLPConfig * OTLPConfig
47+ ResourceAttributes []string
48+ ServiceName string
4549}
4650
4751func ProvideTracerProviderOption (v any , annotations ... fx.Annotation ) fx.Option {
4852 annotations = append (annotations , fx .ResultTags (TracerProviderOptionKey ))
4953 return fx .Provide (fx .Annotate (v , annotations ... ))
5054}
5155
56+ func loadResource (cfg ModuleConfig ) (* resource.Resource , error ) {
57+ defaultResource := resource .Default ()
58+ attributes := make ([]attribute.KeyValue , 0 )
59+ attributes = append (attributes , attribute .String ("service.name" , cfg .ServiceName ))
60+ for _ , ra := range cfg .ResourceAttributes {
61+ parts := strings .SplitN (ra , "=" , 2 )
62+ if len (parts ) < 2 {
63+ return nil , fmt .Errorf ("malformed otlp attribute: %s" , ra )
64+ }
65+ attributes = append (attributes , attribute .String (parts [0 ], parts [1 ]))
66+ }
67+ return resource .Merge (defaultResource , resource .NewSchemaless (attributes ... ))
68+ }
69+
5270func TracesModule (cfg ModuleConfig ) fx.Option {
5371 options := make ([]fx.Option , 0 )
5472 options = append (options ,
55- fx .Provide (func () (* resource.Resource , error ) {
56- defaultResource := resource .Default ()
57- if cfg .Resource == nil {
58- return defaultResource , nil
59- }
60- return resource .Merge (defaultResource , cfg .Resource )
61- }),
62- fx .Supply (resource .Default ()),
73+ fx .Supply (cfg ),
74+ fx .Provide (loadResource ),
6375 fx .Provide (func (tp * tracesdk.TracerProvider ) trace.TracerProvider { return tp }),
6476 fx .Provide (fx .Annotate (func (options ... tracesdk.TracerProviderOption ) * tracesdk.TracerProvider {
6577 return tracesdk .NewTracerProvider (options ... )
0 commit comments