package opentelemetry import ( "context" "time" extConfig "go-admin/config" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp" "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric" "go.opentelemetry.io/otel/sdk/metric" "go.opentelemetry.io/otel/sdk/resource" ) func initMeter(res *resource.Resource) (provider *metric.MeterProvider, err error) { otelConfig := extConfig.ExtConfig.Otel endpoint := otelConfig.Common.Endpoint grpcEndpoint := otelConfig.Common.GrpcEndpoint interval := otelConfig.Metrics.Interval exporter := new(metric.Exporter) if endpoint == "" && grpcEndpoint == "" { *exporter, err = stdoutmetric.New(stdoutmetric.WithPrettyPrint()) } else if grpcEndpoint != "" { options := []otlpmetricgrpc.Option{ otlpmetricgrpc.WithEndpoint(grpcEndpoint), otlpmetricgrpc.WithInsecure(), } *exporter, err = otlpmetricgrpc.New(context.TODO(), options...) } else { *exporter, err = otlpmetrichttp.New(context.TODO(), otlpmetrichttp.WithEndpoint(endpoint), otlpmetrichttp.WithInsecure()) } if interval == 0 { interval = 60 } if err != nil { return nil, err } provider = metric.NewMeterProvider( metric.WithResource(res), metric.WithReader(metric.NewPeriodicReader(*exporter, metric.WithInterval(time.Duration(interval)*time.Second))), ) otel.SetMeterProvider(provider) return provider, nil }