Skip to content

Commit

Permalink
Support Exponential metrics view (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpt1114 authored Feb 15, 2024
1 parent 07b08b0 commit 5aff3a8
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 20 deletions.
25 changes: 14 additions & 11 deletions pkg/constant/env.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package constant

const (
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"
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"
EnvKeyOtelEnabledExponentialHistogramMetrics = "OTEL_ENABLED_EXPONENTIAL_HISTOGRAM_METRICS"
EnvKeyOtelExponentialHistogramMetricsMaxScale = "OTEL_EXPONENTIAL_HISTOGRAM_METRICS_MAX_SCALE"
EnvKeyOtelExponentialHistogramMetricsMaxSize = "OTEL_EXPONENTIAL_HISTOGRAM_METRICS_MAX_SIZE"
)
21 changes: 12 additions & 9 deletions pkg/constant/otel.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package constant

const (
OtelDefaultServiceName = "default-service-name"
OtelDefaultServiceVersion = "0.1.0"
OtelProtocolGRPC = "grpc"
OtelProtocolHTTP = "http"
OtelDefaultTraceAgentGRPCPort = "4317"
OtelDefaultTraceAgentHTTPPort = "4318"
OtelDefaultMetricAgentGRPCPort = "4315"
OtelDefaultMetricAgentHTTPPort = "4316"
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
OtelDefaultEnabledExponentialHistogrammetrics = false
OtelDefaultExponentialHistogramMetricsMaxSize = 30
OtelDefaultExponentialHistogramMetricsMaxScale = 3
)
22 changes: 22 additions & 0 deletions pkg/metric/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,37 @@ func InitProvider() {
return
}

metricsView := make([]metric.View, 0)

exporter, err := newOTLPExporter()
if err != nil {
fmt.Printf("kyber-trace-go: failed to init metric provider, %s\n", err)
return
}

if env.BoolFromEnv(constant.EnvKeyOtelEnabledExponentialHistogramMetrics) {
exponentialHistogramView := metric.NewView(
metric.Instrument{
Kind: metric.InstrumentKindHistogram,
}, metric.Stream{
Aggregation: metric.AggregationBase2ExponentialHistogram{
MaxSize: int32(
env.IntFromEnv(
constant.EnvKeyOtelExponentialHistogramMetricsMaxSize,
constant.OtelDefaultExponentialHistogramMetricsMaxSize)),
MaxScale: int32(
env.IntFromEnv(
constant.EnvKeyOtelExponentialHistogramMetricsMaxScale,
constant.OtelDefaultExponentialHistogramMetricsMaxScale)),
},
})
metricsView = append(metricsView, exponentialHistogramView)
}

provider = metric.NewMeterProvider(
metric.WithResource(newResources()),
metric.WithReader(metric.NewPeriodicReader(exporter)),
metric.WithView(metricsView...),
)

otel.SetMeterProvider(provider)
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ func FloatFromEnv(key string, defaultValue float64) float64 {
}
return defaultValue
}

// IntFromEnv returns the int number for the given key
// and falls back to the given defaultValue if not set
func IntFromEnv(key string, defaultValue int64) int64 {
str := ""
if str = os.Getenv(key); str != "" {
str = strings.TrimSpace(str)
}
if s, err := strconv.ParseInt(str, 10, 64); err == nil {
return s
}
return defaultValue
}
18 changes: 18 additions & 0 deletions pkg/util/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,21 @@ func TestFloatFromEnv(t *testing.T) {
// Test trim space
assert.Equal(t, 0.75, FloatFromEnv("test_float_from_env_trim", 0.5))
}

func TestIntFromEnv(t *testing.T) {
// mock
_ = os.Setenv("test_int_from_env", "75")
_ = os.Setenv("test_int_from_env_empty", "")
_ = os.Setenv("test_int_from_env_string", "seventyfive")

// Test default value
assert.Equal(t, int64(75), IntFromEnv("not_exists", 75))
assert.Equal(t, int64(75), IntFromEnv("test_int_from_env_empty", 75))
assert.Equal(t, int64(75), IntFromEnv("test_int_from_env_string", 75))

// Test not default value
assert.Equal(t, int64(75), IntFromEnv("test_int_from_env", 50))

// Test trim space
assert.Equal(t, int64(75), IntFromEnv("test_int_from_env", 50))
}

0 comments on commit 5aff3a8

Please sign in to comment.