From 7511aabfc63949814b2d15ac81cb451ec9484b51 Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Thu, 21 Apr 2022 13:16:29 +0200 Subject: [PATCH] Use otlp module provided by go-libs. --- cmd/container.go | 30 +-- cmd/container_test.go | 6 +- go.mod | 2 +- go.sum | 2 + .../opentelemetrymetrics/controller.go | 39 ---- .../opentelemetrymetrics/exporter.go | 20 -- .../opentelemetrymetrics/metrics.go | 66 ------- .../opentelemetrymetrics/metrics_test.go | 85 --------- .../opentelemetrymetrics/noopexporter.go | 17 -- .../opentelemetrymetrics/otlpexporter.go | 65 ------- .../opentelemetrytraces/jaegerexporter.go | 29 --- .../opentelemetrytraces/otlpexporter.go | 62 ------- .../opentelemetrytraces/resource.go | 37 ---- .../opentelemetrytraces/stdoutexporter.go | 22 --- .../opentelemetrytraces/traces.go | 175 ------------------ .../opentelemetrytraces/traces_test.go | 110 ----------- 16 files changed, 21 insertions(+), 746 deletions(-) delete mode 100644 pkg/opentelemetry/opentelemetrymetrics/controller.go delete mode 100644 pkg/opentelemetry/opentelemetrymetrics/exporter.go delete mode 100644 pkg/opentelemetry/opentelemetrymetrics/metrics.go delete mode 100644 pkg/opentelemetry/opentelemetrymetrics/metrics_test.go delete mode 100644 pkg/opentelemetry/opentelemetrymetrics/noopexporter.go delete mode 100644 pkg/opentelemetry/opentelemetrymetrics/otlpexporter.go delete mode 100644 pkg/opentelemetry/opentelemetrytraces/jaegerexporter.go delete mode 100644 pkg/opentelemetry/opentelemetrytraces/otlpexporter.go delete mode 100644 pkg/opentelemetry/opentelemetrytraces/resource.go delete mode 100644 pkg/opentelemetry/opentelemetrytraces/stdoutexporter.go delete mode 100644 pkg/opentelemetry/opentelemetrytraces/traces.go delete mode 100644 pkg/opentelemetry/opentelemetrytraces/traces_test.go diff --git a/cmd/container.go b/cmd/container.go index 7570179a6..26b967394 100644 --- a/cmd/container.go +++ b/cmd/container.go @@ -8,6 +8,8 @@ import ( "github.com/gin-gonic/gin" "github.com/numary/go-libs/sharedlogging" "github.com/numary/go-libs/sharedlogging/sharedlogginglogrus" + "github.com/numary/go-libs/sharedotlp/sharedotlpmetrics" + "github.com/numary/go-libs/sharedotlp/sharedotlptraces" "github.com/numary/go-libs/sharedpublish" "github.com/numary/go-libs/sharedpublish/sharedpublishhttp" "github.com/numary/go-libs/sharedpublish/sharedpublishkafka" @@ -109,26 +111,24 @@ func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App { // Handle OpenTelemetry if v.GetBool(otelTracesFlag) { - options = append(options, opentelemetrytraces.TracesModule(opentelemetrytraces.ModuleConfig{ - ServiceName: ServiceName, - Version: Version, - Batch: v.GetBool(otelTracesBatchFlag), - Exporter: v.GetString(otelTracesExporterFlag), - JaegerConfig: func() *opentelemetrytraces.JaegerConfig { - if v.GetString(otelTracesExporterFlag) != opentelemetrytraces.JaegerExporter { + options = append(options, sharedotlptraces.TracesModule(sharedotlptraces.ModuleConfig{ + Batch: v.GetBool(otelTracesBatchFlag), + Exporter: v.GetString(otelTracesExporterFlag), + JaegerConfig: func() *sharedotlptraces.JaegerConfig { + if v.GetString(otelTracesExporterFlag) != sharedotlptraces.JaegerExporter { return nil } - return &opentelemetrytraces.JaegerConfig{ + return &sharedotlptraces.JaegerConfig{ Endpoint: v.GetString(otelTracesExporterJaegerEndpointFlag), User: v.GetString(otelTracesExporterJaegerUserFlag), Password: v.GetString(otelTracesExporterJaegerPasswordFlag), } }(), - OTLPConfig: func() *opentelemetrytraces.OTLPConfig { - if v.GetString(otelTracesExporterFlag) != opentelemetrytraces.OTLPExporter { + OTLPConfig: func() *sharedotlptraces.OTLPConfig { + if v.GetString(otelTracesExporterFlag) != sharedotlptraces.OTLPExporter { return nil } - return &opentelemetrytraces.OTLPConfig{ + return &sharedotlptraces.OTLPConfig{ Mode: v.GetString(otelTracesExporterOTLPModeFlag), Endpoint: v.GetString(otelTracesExporterOTLPEndpointFlag), Insecure: v.GetBool(otelTracesExporterOTLPInsecureFlag), @@ -137,13 +137,13 @@ func NewContainer(v *viper.Viper, userOptions ...fx.Option) *fx.App { })) } if v.GetBool(otelMetricsFlag) { - options = append(options, opentelemetrymetrics.MetricsModule(opentelemetrymetrics.MetricsModuleConfig{ + options = append(options, sharedotlpmetrics.MetricsModule(sharedotlpmetrics.MetricsModuleConfig{ Exporter: v.GetString(otelMetricsExporterFlag), - OTLPConfig: func() *opentelemetrymetrics.OTLPMetricsConfig { - if v.GetString(otelMetricsExporterFlag) != opentelemetrymetrics.OTLPMetricsExporter { + OTLPConfig: func() *sharedotlpmetrics.OTLPMetricsConfig { + if v.GetString(otelMetricsExporterFlag) != sharedotlpmetrics.OTLPMetricsExporter { return nil } - return &opentelemetrymetrics.OTLPMetricsConfig{ + return &sharedotlpmetrics.OTLPMetricsConfig{ Mode: v.GetString(otelMetricsExporterOTLPModeFlag), Endpoint: v.GetString(otelMetricsExporterOTLPEndpointFlag), Insecure: v.GetBool(otelMetricsExporterOTLPInsecureFlag), diff --git a/cmd/container_test.go b/cmd/container_test.go index 6e83bf6e3..a360baabe 100644 --- a/cmd/container_test.go +++ b/cmd/container_test.go @@ -4,11 +4,11 @@ import ( "context" "errors" "github.com/ThreeDotsLabs/watermill/pubsub/gochannel" + "github.com/numary/go-libs/sharedotlp/sharedotlptraces" "github.com/numary/ledger/internal/pgtesting" "github.com/numary/ledger/pkg/bus" "github.com/numary/ledger/pkg/core" "github.com/numary/ledger/pkg/ledger" - "github.com/numary/ledger/pkg/opentelemetry/opentelemetrytraces" "github.com/numary/ledger/pkg/storage" "github.com/numary/ledger/pkg/storage/sqlstorage" "github.com/pborman/uuid" @@ -77,7 +77,7 @@ func TestContainers(t *testing.T) { return nil }, }) - }, fx.ParamTags(``, ``, ``, opentelemetrytraces.TracerProviderOptionKey))), + }, fx.ParamTags(``, ``, ``, sharedotlptraces.TracerProviderOptionKey))), }, }, { @@ -115,7 +115,7 @@ func TestContainers(t *testing.T) { return nil }, }) - }, fx.ParamTags(``, ``, ``, opentelemetrytraces.TracerProviderOptionKey))), + }, fx.ParamTags(``, ``, ``, sharedotlptraces.TracerProviderOptionKey))), }, }, { diff --git a/go.mod b/go.mod index 41877581a..2d52858cc 100644 --- a/go.mod +++ b/go.mod @@ -43,7 +43,7 @@ require ( github.com/gibson042/canonicaljson-go v1.0.3 github.com/go-redis/redis/v8 v8.11.4 github.com/go-redis/redismock/v8 v8.0.6 - github.com/numary/go-libs v0.0.0-20220325095600-77c50f13215d + github.com/numary/go-libs v0.0.0-20220421111437-7c311b956456 github.com/xdg-go/scram v1.1.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b ) diff --git a/go.sum b/go.sum index f1d8d8e10..9ddac5b98 100644 --- a/go.sum +++ b/go.sum @@ -697,6 +697,8 @@ github.com/numary/go-libs v0.0.0-20220325080412-02604f34c81f h1:eeqeJgc0BhJ1vjRx github.com/numary/go-libs v0.0.0-20220325080412-02604f34c81f/go.mod h1:Ko0TOXuzUP7hP70Vj+E8xRU0BBqxHKeh/He/r8xqmNY= github.com/numary/go-libs v0.0.0-20220325095600-77c50f13215d h1:9liBLU1oViJz9B3dAdSSIxbUaYA9vns1gxSClcTJmjY= github.com/numary/go-libs v0.0.0-20220325095600-77c50f13215d/go.mod h1:Ko0TOXuzUP7hP70Vj+E8xRU0BBqxHKeh/He/r8xqmNY= +github.com/numary/go-libs v0.0.0-20220421111437-7c311b956456 h1:LXyf/KcN++yvqhv3PpHuJ+y0Ngl9BGrHBuQFyfATtAA= +github.com/numary/go-libs v0.0.0-20220421111437-7c311b956456/go.mod h1:6tDsBoFNhjhfN3/G7JFmv5B2GIfuzTNhA2QrdnwOCew= github.com/numary/ledger v0.0.0-20210702172952-a5bd30e551d0/go.mod h1:u2K28z9TDYd6id1qeD2uv7JDlajuRZ0fvOnCeDZmDxk= github.com/numary/ledger v0.0.0-20211227131550-dc7b78f85b5b/go.mod h1:uovuDsK7Gs7duqKQ9PgaFulJnPTDftGdR/n3rBRzNIs= github.com/numary/ledger v1.2.1/go.mod h1:dCGgCTrHvVfl4YKMKY0oq3bQexsEU/mhGHM9qRrNjJ0= diff --git a/pkg/opentelemetry/opentelemetrymetrics/controller.go b/pkg/opentelemetry/opentelemetrymetrics/controller.go deleted file mode 100644 index 127b8fde7..000000000 --- a/pkg/opentelemetry/opentelemetrymetrics/controller.go +++ /dev/null @@ -1,39 +0,0 @@ -package opentelemetrymetrics - -import ( - "context" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" - "go.opentelemetry.io/otel/metric" - controller "go.opentelemetry.io/otel/sdk/metric/controller/basic" - processor "go.opentelemetry.io/otel/sdk/metric/processor/basic" - "go.opentelemetry.io/otel/sdk/metric/selector/simple" - "go.uber.org/fx" - "time" -) - -func LoadController(exp *otlpmetric.Exporter) *controller.Controller { - return controller.New( - processor.NewFactory(simple.NewWithHistogramDistribution(), exp), - controller.WithExporter(exp), - controller.WithCollectPeriod(2*time.Second), - ) -} - -func MetricsControllerModule() fx.Option { - return fx.Options( - fx.Provide(LoadController), - fx.Provide(func(ctrl *controller.Controller) metric.MeterProvider { - return ctrl - }), - fx.Invoke(func(lc fx.Lifecycle, pusher *controller.Controller) { - lc.Append(fx.Hook{ - OnStart: func(ctx context.Context) error { - return pusher.Start(context.Background()) - }, - OnStop: func(ctx context.Context) error { - return pusher.Stop(ctx) - }, - }) - }), - ) -} diff --git a/pkg/opentelemetry/opentelemetrymetrics/exporter.go b/pkg/opentelemetry/opentelemetrymetrics/exporter.go deleted file mode 100644 index 124a84a3d..000000000 --- a/pkg/opentelemetry/opentelemetrymetrics/exporter.go +++ /dev/null @@ -1,20 +0,0 @@ -package opentelemetrymetrics - -import ( - "go.opentelemetry.io/otel/metric" - "go.opentelemetry.io/otel/metric/global" - "go.uber.org/fx" -) - -const ( - NoOpMetricsExporter = "noop" - OTLPMetricsExporter = "otlp" -) - -func metricsSdkExportModule() fx.Option { - return fx.Options( - fx.Invoke(func(mp metric.MeterProvider) { - global.SetMeterProvider(mp) - }), - ) -} diff --git a/pkg/opentelemetry/opentelemetrymetrics/metrics.go b/pkg/opentelemetry/opentelemetrymetrics/metrics.go deleted file mode 100644 index 39f314016..000000000 --- a/pkg/opentelemetry/opentelemetrymetrics/metrics.go +++ /dev/null @@ -1,66 +0,0 @@ -package opentelemetrymetrics - -import ( - "github.com/numary/ledger/pkg/opentelemetry" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" - "go.uber.org/fx" -) - -type OTLPMetricsConfig struct { - Mode string - Endpoint string - Insecure bool -} - -type MetricsModuleConfig struct { - Exporter string - OTLPConfig *OTLPMetricsConfig -} - -func MetricsModule(cfg MetricsModuleConfig) fx.Option { - options := make([]fx.Option, 0) - switch cfg.Exporter { - case OTLPMetricsExporter: - options = append(options, OTLPMeterModule()) - mode := opentelemetry.ModeGRPC - if cfg.OTLPConfig != nil { - if cfg.OTLPConfig.Mode != "" { - mode = cfg.OTLPConfig.Mode - } - switch mode { - case opentelemetry.ModeGRPC: - if cfg.OTLPConfig.Endpoint != "" { - options = append(options, ProvideOTLPMeterGRPCClientOption(func() otlpmetricgrpc.Option { - return otlpmetricgrpc.WithEndpoint(cfg.OTLPConfig.Endpoint) - })) - } - if cfg.OTLPConfig.Insecure { - options = append(options, ProvideOTLPMeterGRPCClientOption(func() otlpmetricgrpc.Option { - return otlpmetricgrpc.WithInsecure() - })) - } - case opentelemetry.ModeHTTP: - if cfg.OTLPConfig.Endpoint != "" { - options = append(options, ProvideOTLPMeterHTTPClientOption(func() otlpmetrichttp.Option { - return otlpmetrichttp.WithEndpoint(cfg.OTLPConfig.Endpoint) - })) - } - if cfg.OTLPConfig.Insecure { - options = append(options, ProvideOTLPMeterHTTPClientOption(func() otlpmetrichttp.Option { - return otlpmetrichttp.WithInsecure() - })) - } - } - } - switch mode { - case opentelemetry.ModeGRPC: - options = append(options, OTLPMeterGRPCClientModule()) - case opentelemetry.ModeHTTP: - options = append(options, OTLPMeterHTTPClientModule()) - } - case NoOpMetricsExporter: - options = append(options, NoOpMeterModule()) - } - return fx.Options(options...) -} diff --git a/pkg/opentelemetry/opentelemetrymetrics/metrics_test.go b/pkg/opentelemetry/opentelemetrymetrics/metrics_test.go deleted file mode 100644 index b46cd52f4..000000000 --- a/pkg/opentelemetry/opentelemetrymetrics/metrics_test.go +++ /dev/null @@ -1,85 +0,0 @@ -package opentelemetrymetrics - -import ( - "context" - "fmt" - "github.com/numary/ledger/pkg/opentelemetry" - "github.com/stretchr/testify/assert" - "go.opentelemetry.io/otel/metric" - "go.uber.org/fx" - "testing" -) - -func TestMetricsModule(t *testing.T) { - - type testCase struct { - name string - config MetricsModuleConfig - } - - tests := []testCase{ - { - name: fmt.Sprintf("otlp-exporter"), - config: MetricsModuleConfig{ - Exporter: OTLPMetricsExporter, - }, - }, - { - name: fmt.Sprintf("otlp-exporter-with-grpc-config"), - config: MetricsModuleConfig{ - Exporter: OTLPMetricsExporter, - OTLPConfig: &OTLPMetricsConfig{ - Mode: opentelemetry.ModeGRPC, - Endpoint: "remote:8080", - Insecure: true, - }, - }, - }, - { - name: fmt.Sprintf("otlp-exporter-with-http-config"), - config: MetricsModuleConfig{ - Exporter: OTLPMetricsExporter, - OTLPConfig: &OTLPMetricsConfig{ - Mode: opentelemetry.ModeHTTP, - Endpoint: "remote:8080", - Insecure: true, - }, - }, - }, - { - name: fmt.Sprintf("noop-exporter"), - config: MetricsModuleConfig{ - Exporter: NoOpMetricsExporter, - }, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - options := []fx.Option{MetricsModule(test.config)} - if !testing.Verbose() { - options = append(options, fx.NopLogger) - } - options = append(options, fx.Provide(func() *testing.T { - return t - })) - assert.NoError(t, fx.ValidateApp(options...)) - - ch := make(chan struct{}) - options = append(options, fx.Invoke(func(mp metric.MeterProvider) { // Inject validate the object availability - close(ch) - })) - - app := fx.New(options...) - assert.NoError(t, app.Start(context.Background())) - defer app.Stop(context.Background()) - - select { - case <-ch: - default: - assert.Fail(t, "something went wrong") - } - }) - } - -} diff --git a/pkg/opentelemetry/opentelemetrymetrics/noopexporter.go b/pkg/opentelemetry/opentelemetrymetrics/noopexporter.go deleted file mode 100644 index 3cdadfa3a..000000000 --- a/pkg/opentelemetry/opentelemetrymetrics/noopexporter.go +++ /dev/null @@ -1,17 +0,0 @@ -package opentelemetrymetrics - -import ( - "go.opentelemetry.io/otel/metric" - "go.uber.org/fx" -) - -func LoadNoOpMeterProvider() metric.MeterProvider { - return metric.NewNoopMeterProvider() -} - -func NoOpMeterModule() fx.Option { - return fx.Options( - fx.Provide(LoadNoOpMeterProvider), - metricsSdkExportModule(), - ) -} diff --git a/pkg/opentelemetry/opentelemetrymetrics/otlpexporter.go b/pkg/opentelemetry/opentelemetrymetrics/otlpexporter.go deleted file mode 100644 index 270f08796..000000000 --- a/pkg/opentelemetry/opentelemetrymetrics/otlpexporter.go +++ /dev/null @@ -1,65 +0,0 @@ -package opentelemetrymetrics - -import ( - "context" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" - "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" - "go.uber.org/fx" -) - -func LoadOTLPMeterExporter(client otlpmetric.Client) (*otlpmetric.Exporter, error) { - exp, err := otlpmetric.New(context.Background(), client) - if err != nil { - return nil, err - } - return exp, nil -} - -func LoadOTLPMeterGRPCClient(options ...otlpmetricgrpc.Option) otlpmetric.Client { - return otlpmetricgrpc.NewClient(options...) -} - -func LoadOTLPMeterHTTPClient(options ...otlpmetrichttp.Option) otlpmetric.Client { - return otlpmetrichttp.NewClient(options...) -} - -func OTLPMeterModule() fx.Option { - return fx.Options( - fx.Provide(LoadOTLPMeterExporter), - metricsSdkExportModule(), - MetricsControllerModule(), - ) -} - -const OTLPMeterGRPCOptionsKey = `group:"_otlpMeterGrpcOptions"` - -func ProvideOTLPMeterGRPCClientOption(provider interface{}) fx.Option { - return fx.Provide( - fx.Annotate(provider, fx.ResultTags(OTLPMeterGRPCOptionsKey), fx.As(new(otlpmetricgrpc.Option))), - ) -} - -func OTLPMeterGRPCClientModule() fx.Option { - return fx.Options( - fx.Provide( - fx.Annotate(LoadOTLPMeterGRPCClient, fx.ParamTags(OTLPMeterGRPCOptionsKey)), - ), - ) -} - -const OTLPMeterHTTPOptionsKey = `group:"_otlpMeterHTTPOptions"` - -func ProvideOTLPMeterHTTPClientOption(provider interface{}) fx.Option { - return fx.Provide( - fx.Annotate(provider, fx.ResultTags(OTLPMeterHTTPOptionsKey), fx.As(new(otlpmetrichttp.Option))), - ) -} - -func OTLPMeterHTTPClientModule() fx.Option { - return fx.Options( - fx.Provide( - fx.Annotate(LoadOTLPMeterHTTPClient, fx.ParamTags(OTLPMeterHTTPOptionsKey)), - ), - ) -} diff --git a/pkg/opentelemetry/opentelemetrytraces/jaegerexporter.go b/pkg/opentelemetry/opentelemetrytraces/jaegerexporter.go deleted file mode 100644 index 1578656b1..000000000 --- a/pkg/opentelemetry/opentelemetrytraces/jaegerexporter.go +++ /dev/null @@ -1,29 +0,0 @@ -package opentelemetrytraces - -import ( - "go.opentelemetry.io/otel/exporters/jaeger" - "go.opentelemetry.io/otel/sdk/trace" - "go.uber.org/fx" -) - -func LoadJaegerTracerExporter(options ...jaeger.CollectorEndpointOption) (*jaeger.Exporter, error) { - return jaeger.New(jaeger.WithCollectorEndpoint(options...)) -} - -const ( - JaegerCollectorEndpointGroupKey = `group:"_tracerCollectorEndpointOptions"` -) - -func ProvideJaegerTracerCollectorEndpoint(provider interface{}) fx.Option { - return fx.Provide(fx.Annotate(provider, fx.ResultTags(JaegerCollectorEndpointGroupKey))) -} - -func JaegerTracerModule() fx.Option { - return fx.Options( - fx.Provide( - fx.Annotate(LoadJaegerTracerExporter, fx.ParamTags( - JaegerCollectorEndpointGroupKey, - ), fx.As(new(trace.SpanExporter))), - ), - ) -} diff --git a/pkg/opentelemetry/opentelemetrytraces/otlpexporter.go b/pkg/opentelemetry/opentelemetrytraces/otlpexporter.go deleted file mode 100644 index 90e9af653..000000000 --- a/pkg/opentelemetry/opentelemetrytraces/otlpexporter.go +++ /dev/null @@ -1,62 +0,0 @@ -package opentelemetrytraces - -import ( - "context" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" - "go.opentelemetry.io/otel/sdk/trace" - "go.uber.org/fx" -) - -func LoadOTLPTracerProvider(client otlptrace.Client) (*otlptrace.Exporter, error) { - return otlptrace.New(context.Background(), client) -} - -func LoadOTLPTracerGRPCClient(options ...otlptracegrpc.Option) otlptrace.Client { - return otlptracegrpc.NewClient(options...) -} - -func LoadOTLPTracerHTTPClient(options ...otlptracehttp.Option) otlptrace.Client { - return otlptracehttp.NewClient(options...) -} - -func OTLPTracerModule() fx.Option { - return fx.Options( - fx.Provide( - fx.Annotate(LoadOTLPTracerProvider, fx.As(new(trace.SpanExporter))), - ), - ) -} - -const OTLPTracerGRPCOptionsKey = `group:"_otlpTracerGrpcOptions"` - -func ProvideOTLPTracerGRPCClientOption(provider interface{}) fx.Option { - return fx.Provide( - fx.Annotate(provider, fx.ResultTags(OTLPTracerGRPCOptionsKey), fx.As(new(otlptracegrpc.Option))), - ) -} - -func OTLPTracerGRPCClientModule() fx.Option { - return fx.Options( - fx.Provide( - fx.Annotate(LoadOTLPTracerGRPCClient, fx.ParamTags(OTLPTracerGRPCOptionsKey)), - ), - ) -} - -const OTLPTracerHTTPOptionsKey = `group:"_otlpTracerHTTPOptions"` - -func ProvideOTLPTracerHTTPClientOption(provider interface{}) fx.Option { - return fx.Provide( - fx.Annotate(provider, fx.ResultTags(OTLPTracerHTTPOptionsKey), fx.As(new(otlptracehttp.Option))), - ) -} - -func OTLPTracerHTTPClientModule() fx.Option { - return fx.Options( - fx.Provide( - fx.Annotate(LoadOTLPTracerHTTPClient, fx.ParamTags(OTLPTracerHTTPOptionsKey)), - ), - ) -} diff --git a/pkg/opentelemetry/opentelemetrytraces/resource.go b/pkg/opentelemetry/opentelemetrytraces/resource.go deleted file mode 100644 index 388c67267..000000000 --- a/pkg/opentelemetry/opentelemetrytraces/resource.go +++ /dev/null @@ -1,37 +0,0 @@ -package opentelemetrytraces - -import ( - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/sdk/resource" - semconv "go.opentelemetry.io/otel/semconv/v1.7.0" - "go.uber.org/fx" -) - -type resourceFactory struct { - attributes []attribute.KeyValue -} - -func (f *resourceFactory) Make() (*resource.Resource, error) { - return resource.Merge( - resource.Default(), - resource.NewWithAttributes(semconv.SchemaURL, f.attributes...), - ) -} - -func NewResourceFactory(attributes ...attribute.KeyValue) *resourceFactory { - return &resourceFactory{attributes: attributes} -} - -const oltpAttribute = `group:"_otlpAttributes"` - -func ProvideOTLPAttribute(attr attribute.KeyValue) fx.Option { - return fx.Provide(fx.Annotate(func() attribute.KeyValue { - return attr - }, fx.ResultTags(oltpAttribute))) -} - -func ResourceFactoryModule() fx.Option { - return fx.Options(fx.Provide( - fx.Annotate(NewResourceFactory, fx.ParamTags(oltpAttribute))), - ) -} diff --git a/pkg/opentelemetry/opentelemetrytraces/stdoutexporter.go b/pkg/opentelemetry/opentelemetrytraces/stdoutexporter.go deleted file mode 100644 index 6cc9aa871..000000000 --- a/pkg/opentelemetry/opentelemetrytraces/stdoutexporter.go +++ /dev/null @@ -1,22 +0,0 @@ -package opentelemetrytraces - -import ( - "go.opentelemetry.io/otel/exporters/stdout/stdouttrace" - "go.opentelemetry.io/otel/sdk/trace" - "go.uber.org/fx" - "os" -) - -func LoadStdoutTracerProvider() (*stdouttrace.Exporter, error) { - return stdouttrace.New( - stdouttrace.WithWriter(os.Stdout), - ) -} - -func StdoutTracerModule() fx.Option { - return fx.Options( - fx.Provide( - fx.Annotate(LoadStdoutTracerProvider, fx.As(new(trace.SpanExporter))), - ), - ) -} diff --git a/pkg/opentelemetry/opentelemetrytraces/traces.go b/pkg/opentelemetry/opentelemetrytraces/traces.go deleted file mode 100644 index e3a4aad7f..000000000 --- a/pkg/opentelemetry/opentelemetrytraces/traces.go +++ /dev/null @@ -1,175 +0,0 @@ -package opentelemetrytraces - -import ( - "context" - "fmt" - "github.com/XSAM/otelsql" - "github.com/numary/ledger/pkg/opentelemetry" - "github.com/numary/ledger/pkg/storage/sqlstorage" - "go.opentelemetry.io/otel" - "go.opentelemetry.io/otel/attribute" - "go.opentelemetry.io/otel/exporters/jaeger" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" - "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" - "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.7.0" - "go.opentelemetry.io/otel/trace" - "go.uber.org/fx" -) - -const ( - JaegerExporter = "jaeger" - StdoutExporter = "stdout" - OTLPExporter = "otlp" - - TracerProviderOptionKey = `group:"_tracerProviderOption"` -) - -type JaegerConfig struct { - Endpoint string - User string - Password string -} - -type OTLPConfig struct { - Mode string - Endpoint string - Insecure bool -} - -type ModuleConfig struct { - ServiceName string - Version string - Exporter string - Batch bool - JaegerConfig *JaegerConfig - OTLPConfig *OTLPConfig -} - -func ProvideTracerProviderOption(v interface{}, annotations ...fx.Annotation) fx.Option { - annotations = append(annotations, fx.ResultTags(TracerProviderOptionKey)) - return fx.Provide(fx.Annotate(v, annotations...)) -} - -func TracesModule(cfg ModuleConfig) fx.Option { - options := make([]fx.Option, 0) - options = append(options, - ResourceFactoryModule(), - ProvideOTLPAttribute(semconv.ServiceNameKey.String(cfg.ServiceName)), - ProvideOTLPAttribute(semconv.ServiceVersionKey.String(cfg.Version)), - fx.Provide(func(tp *tracesdk.TracerProvider) trace.TracerProvider { return tp }), - fx.Provide(fx.Annotate(func(options ...tracesdk.TracerProviderOption) *tracesdk.TracerProvider { - return tracesdk.NewTracerProvider(options...) - }, fx.ParamTags(TracerProviderOptionKey))), - fx.Invoke(func(lc fx.Lifecycle, tracerProvider *tracesdk.TracerProvider) { - // set global propagator to tracecontext (the default is no-op). - otel.SetTextMapPropagator(propagation.TraceContext{}) - lc.Append(fx.Hook{ - OnStart: func(ctx context.Context) error { - otel.SetTracerProvider(tracerProvider) - return nil - }, - OnStop: func(ctx context.Context) error { - return tracerProvider.Shutdown(ctx) - }, - }) - }), - fx.Provide(func(factory *resourceFactory) (*resource.Resource, error) { - return factory.Make() - }), - ProvideTracerProviderOption(tracesdk.WithResource), - ) - if cfg.Batch { - options = append(options, ProvideTracerProviderOption(tracesdk.WithBatcher, fx.ParamTags(``, `group:"_batchOptions"`))) - } else { - options = append(options, ProvideTracerProviderOption(tracesdk.WithSyncer)) - } - - options = append(options, fx.Invoke(func(cfg struct { - fx.In - Flavor sqlstorage.Flavor `optional:"true"` - }) error { - if cfg.Flavor != 0 { - var attr attribute.KeyValue - switch cfg.Flavor { - case sqlstorage.SQLite: - attr = semconv.DBSystemSqlite - case sqlstorage.PostgreSQL: - attr = semconv.DBSystemPostgreSQL - } - sqlDriverName, err := otelsql.Register(sqlstorage.SQLDriverName(cfg.Flavor), attr.Value.AsString()) - if err != nil { - return fmt.Errorf("Error registering otel driver: %s", err) - } - sqlstorage.UpdateSQLDriverMapping(cfg.Flavor, sqlDriverName) - } - return nil - })) - - switch cfg.Exporter { - case JaegerExporter: - options = append(options, JaegerTracerModule()) - if cfg.JaegerConfig != nil { - if v := cfg.JaegerConfig.Endpoint; v != "" { - options = append(options, ProvideJaegerTracerCollectorEndpoint(func() jaeger.CollectorEndpointOption { - return jaeger.WithEndpoint(v) - })) - } - - if v := cfg.JaegerConfig.User; v != "" { - options = append(options, ProvideJaegerTracerCollectorEndpoint(func() jaeger.CollectorEndpointOption { - return jaeger.WithUsername(v) - })) - } - - if v := cfg.JaegerConfig.Password; v != "" { - options = append(options, ProvideJaegerTracerCollectorEndpoint(func() jaeger.CollectorEndpointOption { - return jaeger.WithPassword(v) - })) - } - } - case StdoutExporter: - options = append(options, StdoutTracerModule()) - case OTLPExporter: - options = append(options, OTLPTracerModule()) - mode := opentelemetry.ModeGRPC - if cfg.OTLPConfig != nil { - if cfg.OTLPConfig.Mode != "" { - mode = cfg.OTLPConfig.Mode - } - switch mode { - case opentelemetry.ModeGRPC: - if cfg.OTLPConfig.Endpoint != "" { - options = append(options, ProvideOTLPTracerGRPCClientOption(func() otlptracegrpc.Option { - return otlptracegrpc.WithEndpoint(cfg.OTLPConfig.Endpoint) - })) - } - if cfg.OTLPConfig.Insecure { - options = append(options, ProvideOTLPTracerGRPCClientOption(func() otlptracegrpc.Option { - return otlptracegrpc.WithInsecure() - })) - } - case opentelemetry.ModeHTTP: - if cfg.OTLPConfig.Endpoint != "" { - options = append(options, ProvideOTLPTracerHTTPClientOption(func() otlptracehttp.Option { - return otlptracehttp.WithEndpoint(cfg.OTLPConfig.Endpoint) - })) - } - if cfg.OTLPConfig.Insecure { - options = append(options, ProvideOTLPTracerHTTPClientOption(func() otlptracehttp.Option { - return otlptracehttp.WithInsecure() - })) - } - } - } - switch mode { - case opentelemetry.ModeGRPC: - options = append(options, OTLPTracerGRPCClientModule()) - case opentelemetry.ModeHTTP: - options = append(options, OTLPTracerHTTPClientModule()) - } - } - return fx.Options(options...) -} diff --git a/pkg/opentelemetry/opentelemetrytraces/traces_test.go b/pkg/opentelemetry/opentelemetrytraces/traces_test.go deleted file mode 100644 index 61920309c..000000000 --- a/pkg/opentelemetry/opentelemetrytraces/traces_test.go +++ /dev/null @@ -1,110 +0,0 @@ -package opentelemetrytraces - -import ( - "context" - "fmt" - "github.com/numary/ledger/pkg/opentelemetry" - "github.com/stretchr/testify/assert" - "go.uber.org/fx" - "testing" -) - -func TestTracesModule(t *testing.T) { - - type testCase struct { - name string - config ModuleConfig - } - - tests := []testCase{ - { - name: fmt.Sprintf("otlp-exporter"), - config: ModuleConfig{ - ServiceName: "testing", - Version: "latest", - Exporter: OTLPExporter, - }, - }, - { - name: fmt.Sprintf("otlp-exporter-with-grpc-config"), - config: ModuleConfig{ - ServiceName: "testing", - Version: "latest", - Exporter: OTLPExporter, - OTLPConfig: &OTLPConfig{ - Mode: opentelemetry.ModeGRPC, - Endpoint: "remote:8080", - Insecure: true, - }, - }, - }, - { - name: fmt.Sprintf("otlp-exporter-with-http-config"), - config: ModuleConfig{ - ServiceName: "testing", - Version: "latest", - Exporter: OTLPExporter, - OTLPConfig: &OTLPConfig{ - Mode: opentelemetry.ModeHTTP, - Endpoint: "remote:8080", - Insecure: true, - }, - }, - }, - { - name: fmt.Sprintf("jaeger-exporter"), - config: ModuleConfig{ - ServiceName: "testing", - Version: "latest", - Exporter: JaegerExporter, - }, - }, - { - name: fmt.Sprintf("jaeger-exporter-with-config"), - config: ModuleConfig{ - ServiceName: "testing", - Version: "latest", - Exporter: JaegerExporter, - JaegerConfig: &JaegerConfig{}, - }, - }, - { - name: fmt.Sprintf("stdout-exporter"), - config: ModuleConfig{ - ServiceName: "testing", - Version: "latest", - Exporter: StdoutExporter, - }, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - options := []fx.Option{TracesModule(test.config)} - if !testing.Verbose() { - options = append(options, fx.NopLogger) - } - options = append(options, fx.Provide(func() *testing.T { - return t - })) - assert.NoError(t, fx.ValidateApp(options...)) - - ch := make(chan struct{}) - options = append(options, fx.Invoke(func(f *resourceFactory) { // Inject validate the object availability - assert.Len(t, f.attributes, 2) - close(ch) - })) - - app := fx.New(options...) - assert.NoError(t, app.Start(context.Background())) - defer app.Stop(context.Background()) - - select { - case <-ch: - default: - assert.Fail(t, "something went wrong") - } - }) - } - -}