Skip to content

Commit

Permalink
Separate trace and metric agent port (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpt1114 authored Aug 28, 2023
1 parent a95bd07 commit 72f2e9f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
18 changes: 11 additions & 7 deletions pkg/constant/env.go
Original file line number Diff line number Diff line change
@@ -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"
)
14 changes: 9 additions & 5 deletions pkg/constant/otlp.go
Original file line number Diff line number Diff line change
@@ -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
)
19 changes: 12 additions & 7 deletions pkg/metric/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metric
import (
"context"
"fmt"
"net"
"sync"
"time"

Expand All @@ -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 {
Expand All @@ -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())
Expand All @@ -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 {
Expand Down
19 changes: 12 additions & 7 deletions pkg/tracer/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tracer
import (
"context"
"fmt"
"net"
"sync"
"time"

Expand All @@ -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 {
Expand All @@ -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())
Expand All @@ -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 {
Expand Down

0 comments on commit 72f2e9f

Please sign in to comment.