diff --git a/pkg/constant/env.go b/pkg/constant/env.go index f7a2d22..5611f0a 100644 --- a/pkg/constant/env.go +++ b/pkg/constant/env.go @@ -1,11 +1,15 @@ package constant const ( - EnvKeyOtelEnabled = "OTEL_ENABLED" - EnvKeyOtelCollectorUrl = "OTEL_COLLECTOR_URL" - EnvKeyOtelInsecure = "OTEL_INSECURE" - EnvKeyOtelProtocol = "OTEL_PROTOCOL" - EnvKeyOtelServiceName = "OTEL_SERVICE_NAME" - EnvKeyOtelServiceVersion = "OTEL_SERVICE_VERSION" - EnvKeyOtelTraceSampleRate = "OTEL_TRACE_SAMPLE_RATE" + EnvKeyOtelEnabled = "OTEL_ENABLED" + EnvKeyOtelAgentHost = "OTEL_AGENT_HOST" + EnvKeyOtelMetricAgentGRPCPort = "OTEL_METRIC_AGENT_GRPC_PORT" + EnvKeyOtelMetricAgentHTTPPort = "OTEL_METRIC_AGENT_HTTP_PORT" + EnvKeyOtelTraceAgentGRPCPort = "OTEL_TRACE_AGENT_GRPC_PORT" + EnvKeyOtelTraceAgentHTTPPort = "OTEL_TRACE_AGENT_HTTP_PORT" + EnvKeyOtelInsecure = "OTEL_INSECURE" + EnvKeyOtelProtocol = "OTEL_PROTOCOL" + EnvKeyOtelServiceName = "OTEL_SERVICE_NAME" + EnvKeyOtelServiceVersion = "OTEL_SERVICE_VERSION" + EnvKeyOtelTraceSampleRate = "OTEL_TRACE_SAMPLE_RATE" ) diff --git a/pkg/constant/otlp.go b/pkg/constant/otlp.go index 7ed598a..b62b525 100644 --- a/pkg/constant/otlp.go +++ b/pkg/constant/otlp.go @@ -1,9 +1,13 @@ package constant const ( - OtelDefaultServiceName = "default-service-name" - OtelDefaultServiceVersion = "0.1.0" - OtelProtocolGRPC = "grpc" - OtelProtocolHTTP = "http" - OtelDefaultSampleRate = 0.5 + OtelDefaultServiceName = "default-service-name" + OtelDefaultServiceVersion = "0.1.0" + OtelProtocolGRPC = "grpc" + OtelProtocolHTTP = "http" + OtelDefaultTraceAgentGRPCPort = "4317" + OtelDefaultTraceAgentHTTPPort = "4318" + OtelDefaultMetricAgentGRPCPort = "4315" + OtelDefaultMetricAgentHTTPPort = "4316" + OtelDefaultSampleRate = 0.5 ) diff --git a/pkg/metric/provider.go b/pkg/metric/provider.go index f7593dd..b166410 100644 --- a/pkg/metric/provider.go +++ b/pkg/metric/provider.go @@ -3,6 +3,7 @@ package metric import ( "context" "fmt" + "net" "sync" "time" @@ -21,9 +22,11 @@ import ( var provider *metric.MeterProvider var lock sync.Mutex -func newGRPCExporter(ctx context.Context, providerServerUrl string, isInsecure bool) (metric.Exporter, error) { +func newGRPCExporter(ctx context.Context, agentHost string, isInsecure bool) (metric.Exporter, error) { + addr := net.JoinHostPort(agentHost, env.StringFromEnv( + constant.EnvKeyOtelMetricAgentGRPCPort, constant.OtelDefaultMetricAgentGRPCPort)) clientOpts := []otlpmetricgrpc.Option{ - otlpmetricgrpc.WithEndpoint(providerServerUrl), + otlpmetricgrpc.WithEndpoint(addr), otlpmetricgrpc.WithDialOption(grpc.WithBlock()), } if isInsecure { @@ -36,9 +39,11 @@ func newGRPCExporter(ctx context.Context, providerServerUrl string, isInsecure b return exporter, nil } -func newHTTPExporter(ctx context.Context, providerServerUrl string, isInsecure bool) (metric.Exporter, error) { +func newHTTPExporter(ctx context.Context, agentHost string, isInsecure bool) (metric.Exporter, error) { + addr := net.JoinHostPort(agentHost, env.StringFromEnv( + constant.EnvKeyOtelMetricAgentHTTPPort, constant.OtelDefaultMetricAgentHTTPPort)) clientOpts := []otlpmetrichttp.Option{ - otlpmetrichttp.WithEndpoint(providerServerUrl), + otlpmetrichttp.WithEndpoint(addr), } if isInsecure { clientOpts = append(clientOpts, otlpmetrichttp.WithInsecure()) @@ -54,17 +59,17 @@ func newOTLPExporter() (metric.Exporter, error) { ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() - providerServerUrl := env.StringFromEnv(constant.EnvKeyOtelCollectorUrl, "") + agentHost := env.StringFromEnv(constant.EnvKeyOtelAgentHost, "") isInsecure := env.BoolFromEnv(constant.EnvKeyOtelInsecure) protocol := env.StringFromEnv(constant.EnvKeyOtelProtocol, constant.OtelProtocolGRPC) // gRPC if protocol == constant.OtelProtocolGRPC { - return newGRPCExporter(ctx, providerServerUrl, isInsecure) + return newGRPCExporter(ctx, agentHost, isInsecure) } // HTTP - return newHTTPExporter(ctx, providerServerUrl, isInsecure) + return newHTTPExporter(ctx, agentHost, isInsecure) } func newResources() *resource.Resource { diff --git a/pkg/tracer/provider.go b/pkg/tracer/provider.go index c9e18a6..3b3fb2c 100644 --- a/pkg/tracer/provider.go +++ b/pkg/tracer/provider.go @@ -3,6 +3,7 @@ package tracer import ( "context" "fmt" + "net" "sync" "time" @@ -23,9 +24,11 @@ import ( var provider *trace.TracerProvider var lock sync.Mutex -func newGRPCExporter(ctx context.Context, providerServerUrl string, isInsecure bool) (*otlptrace.Exporter, error) { +func newGRPCExporter(ctx context.Context, agentHost string, isInsecure bool) (*otlptrace.Exporter, error) { + addr := net.JoinHostPort(agentHost, env.StringFromEnv( + constant.EnvKeyOtelTraceAgentGRPCPort, constant.OtelDefaultTraceAgentGRPCPort)) clientOpts := []otlptracegrpc.Option{ - otlptracegrpc.WithEndpoint(providerServerUrl), + otlptracegrpc.WithEndpoint(addr), otlptracegrpc.WithDialOption(grpc.WithBlock()), } if isInsecure { @@ -40,9 +43,11 @@ func newGRPCExporter(ctx context.Context, providerServerUrl string, isInsecure b return exporter, nil } -func newHTTPExporter(ctx context.Context, providerServerUrl string, isInsecure bool) (*otlptrace.Exporter, error) { +func newHTTPExporter(ctx context.Context, agentHost string, isInsecure bool) (*otlptrace.Exporter, error) { + addr := net.JoinHostPort(agentHost, env.StringFromEnv( + constant.EnvKeyOtelTraceAgentHTTPPort, constant.OtelDefaultTraceAgentHTTPPort)) clientOpts := []otlptracehttp.Option{ - otlptracehttp.WithEndpoint(providerServerUrl), + otlptracehttp.WithEndpoint(addr), } if isInsecure { clientOpts = append(clientOpts, otlptracehttp.WithInsecure()) @@ -60,17 +65,17 @@ func newOTLPExporter() (*otlptrace.Exporter, error) { ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, time.Second) defer cancel() - providerServerUrl := env.StringFromEnv(constant.EnvKeyOtelCollectorUrl, "") + agentHost := env.StringFromEnv(constant.EnvKeyOtelAgentHost, "") isInsecure := env.BoolFromEnv(constant.EnvKeyOtelInsecure) protocol := env.StringFromEnv(constant.EnvKeyOtelProtocol, constant.OtelProtocolGRPC) // gRPC if protocol == constant.OtelProtocolGRPC { - return newGRPCExporter(ctx, providerServerUrl, isInsecure) + return newGRPCExporter(ctx, agentHost, isInsecure) } // HTTP - return newHTTPExporter(ctx, providerServerUrl, isInsecure) + return newHTTPExporter(ctx, agentHost, isInsecure) } func newResources() *resource.Resource {