From 5aff3a873ec500c03fc609d3e0fca855555b0749 Mon Sep 17 00:00:00 2001 From: Van Phan <48898401+vanpt1114@users.noreply.github.com> Date: Thu, 15 Feb 2024 17:15:19 +0700 Subject: [PATCH] Support Exponential metrics view (#7) --- pkg/constant/env.go | 25 ++++++++++++++----------- pkg/constant/otel.go | 21 ++++++++++++--------- pkg/metric/provider.go | 22 ++++++++++++++++++++++ pkg/util/env/env.go | 13 +++++++++++++ pkg/util/env/env_test.go | 18 ++++++++++++++++++ 5 files changed, 79 insertions(+), 20 deletions(-) diff --git a/pkg/constant/env.go b/pkg/constant/env.go index 5611f0a..a816457 100644 --- a/pkg/constant/env.go +++ b/pkg/constant/env.go @@ -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" ) diff --git a/pkg/constant/otel.go b/pkg/constant/otel.go index b62b525..879f3a1 100644 --- a/pkg/constant/otel.go +++ b/pkg/constant/otel.go @@ -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 ) diff --git a/pkg/metric/provider.go b/pkg/metric/provider.go index 90a6b52..2645ccd 100644 --- a/pkg/metric/provider.go +++ b/pkg/metric/provider.go @@ -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) diff --git a/pkg/util/env/env.go b/pkg/util/env/env.go index 43dc15c..ee714cc 100644 --- a/pkg/util/env/env.go +++ b/pkg/util/env/env.go @@ -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 +} diff --git a/pkg/util/env/env_test.go b/pkg/util/env/env_test.go index ff2c3f6..6f1789d 100644 --- a/pkg/util/env/env_test.go +++ b/pkg/util/env/env_test.go @@ -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)) +}