From 792c3300bc4511d5106d409fb7d17455dc8dcf9d Mon Sep 17 00:00:00 2001 From: EpicMo <1982742309@qq.com> Date: Thu, 31 Aug 2023 23:47:59 +0800 Subject: [PATCH] feat: sampler --- .env.docker.compose | 8 +++- src/constant/config/.env.example | 4 ++ src/constant/config/env.go | 78 ++++++++++++++++---------------- src/extra/tracing/otel.go | 9 +++- 4 files changed, 58 insertions(+), 41 deletions(-) diff --git a/.env.docker.compose b/.env.docker.compose index 8dfe655..2812a94 100644 --- a/.env.docker.compose +++ b/.env.docker.compose @@ -3,7 +3,7 @@ CONSUL_ADDR=consul:8500 CONSUL_ANONYMITY_NAME=paraparty. # Configure logger level, support: DEBUG, INFO, WARN (WARNING), ERROR, FATAL -LOGGER_LEVEL=DEBUG +LOGGER_LEVEL=INFO # Cofigure logger integrated with otel, support: enable, disable # If this setting is enable, you will see log in the OTEL Export with possible runtime waste LOGGER_OUT_TRACING=enable @@ -41,13 +41,17 @@ REDIS_PASSWORD= REDIS_DB= REDIS_MASTER= # Config Tracing EndPoint, support Jaeger +# Config state, if use `disable` the sampler will be closed. use `enable` to enable +TRACING_STATE=disable +# Config tracing sampler, suggest 0.01 +TRACING_SAMPLER=0.01 TRACING_ENDPOINT=jaeger:4318 # Optional: Config Pyroscope # Decide whether to enable the service, support : enable, disable. # If you enable this service, you must provide Pyroscope server environment # This profiling is ONLY designed for DEBUGGING # SO, PLEASE DO NOT ENABLE THIS SERVICE IN YOUR PRODUCTION ENVIRONMENT, OR IT MAY TAKE MUCH RUNTIME COST. -PYROSCOPE_STATE=enable +PYROSCOPE_STATE=disable PYROSCOPE_ADDR=http://pyroscope:4040/ # Configure RabbitMQ # Optional: `RABBITMQ_VHOST_PREFIX`: If you provide this config, the service will use value as the rabbit mq vhost prefix. diff --git a/src/constant/config/.env.example b/src/constant/config/.env.example index 529a217..ce8d656 100644 --- a/src/constant/config/.env.example +++ b/src/constant/config/.env.example @@ -47,6 +47,10 @@ REDIS_PASSWORD= REDIS_DB= REDIS_MASTER= # Config Tracing EndPoint, support Jaeger +# Config state, if use `disable` the sampler will be closed. use `enable` to enable +TRACING_STATE= +# Config tracing sampler, suggest 0.01 +TRACING_SAMPLER= TRACING_ENDPOINT=http://localhost:14268/api/traces # Optional: Config Pyroscope # Decide whether to enable the service, support : enable, disable. diff --git a/src/constant/config/env.go b/src/constant/config/env.go index ec80023..de3bcd2 100644 --- a/src/constant/config/env.go +++ b/src/constant/config/env.go @@ -9,44 +9,46 @@ import ( var EnvCfg envConfig type envConfig struct { - ConsulAddr string `env:"CONSUL_ADDR" envDefault:"localhost:8500"` - LoggerLevel string `env:"LOGGER_LEVEL" envDefault:"INFO"` - LoggerWithTraceState string `env:"LOGGER_OUT_TRACING" envDefault:"disable"` - TiedLogging string `env:"TIED" envDefault:"NONE"` - PostgreSQLHost string `env:"POSTGRESQL_HOST"` - PostgreSQLPort string `env:"POSTGRESQL_PORT"` - PostgreSQLUser string `env:"POSTGRESQL_USER"` - PostgreSQLPassword string `env:"POSTGRESQL_PASSWORD"` - PostgreSQLDataBase string `env:"POSTGRESQL_DATABASE"` - StorageType string `env:"STORAGE_TYPE" envDefault:"fs"` - FileSystemStartPath string `env:"FS_PATH" envDefault:"/tmp"` - FileSystemBaseUrl string `env:"FS_BASEURL" envDefault:"http://localhost/"` - RedisAddr string `env:"REDIS_ADDR"` - RedisPassword string `env:"REDIS_PASSWORD" envDefault:""` - RedisDB int `env:"REDIS_DB" envDefault:"0"` - TracingEndPoint string `env:"TRACING_ENDPOINT"` - PyroscopeState string `env:"PYROSCOPE_STATE" envDefault:"false"` - PyroscopeAddr string `env:"PYROSCOPE_ADDR"` - RedisPrefix string `env:"REDIS_PREFIX" envDefault:"GUGUTIK"` - PostgreSQLSchema string `env:"POSTGRESQL_SCHEMA"` - RedisMaster string `env:"REDIS_MASTER"` - ConsulAnonymityPrefix string `env:"CONSUL_ANONYMITY_NAME" envDefault:""` - RabbitMQUsername string `env:"RABBITMQ_USERNAME" envDefault:"guest"` - RabbitMQPassword string `env:"RABBITMQ_PASSWORD" envDefault:"guest"` - RabbitMQAddr string `env:"RABBITMQ_ADDRESS" envDefault:"localhost"` - RabbitMQPort string `env:"RABBITMQ_PORT" envDefault:"5672"` - RabbitMQVhostPrefix string `env:"RABBITMQ_VHOST_PREFIX" envDefault:""` - ChatGPTAPIKEYS string `env:"CHATGPT_API_KEYS"` - PodIpAddr string `env:"POD_IP" envDefault:"localhost"` - GorseAddr string `env:"GORSE_ADDR"` - GorseApiKey string `env:"GORSE_APIKEY"` - MagicUserId uint32 `env:"MAGIC_USER_ID" envDefault:"1"` - ChatGptProxy string `env:"CHATGPT_PROXY"` - PostgreSQLPrefix string `env:"POSTGRESQL_PREFIX" envDefault:""` - PostgreSQLReplicaState string `env:"POSTGRESQL_REPLICA"` - PostgreSQLReplicaAddress string `env:"POSTGRESQL_REPLICA_ADDR"` - PostgreSQLReplicaUsername string `env:"POSTGRESQL_REPLICA_USER"` - PostgreSQLReplicaPassword string `env:"POSTGRESQL_REPLICA_PASSWORD"` + ConsulAddr string `env:"CONSUL_ADDR" envDefault:"localhost:8500"` + LoggerLevel string `env:"LOGGER_LEVEL" envDefault:"INFO"` + LoggerWithTraceState string `env:"LOGGER_OUT_TRACING" envDefault:"disable"` + TiedLogging string `env:"TIED" envDefault:"NONE"` + PostgreSQLHost string `env:"POSTGRESQL_HOST"` + PostgreSQLPort string `env:"POSTGRESQL_PORT"` + PostgreSQLUser string `env:"POSTGRESQL_USER"` + PostgreSQLPassword string `env:"POSTGRESQL_PASSWORD"` + PostgreSQLDataBase string `env:"POSTGRESQL_DATABASE"` + StorageType string `env:"STORAGE_TYPE" envDefault:"fs"` + FileSystemStartPath string `env:"FS_PATH" envDefault:"/tmp"` + FileSystemBaseUrl string `env:"FS_BASEURL" envDefault:"http://localhost/"` + RedisAddr string `env:"REDIS_ADDR"` + RedisPassword string `env:"REDIS_PASSWORD" envDefault:""` + RedisDB int `env:"REDIS_DB" envDefault:"0"` + TracingEndPoint string `env:"TRACING_ENDPOINT"` + PyroscopeState string `env:"PYROSCOPE_STATE" envDefault:"false"` + PyroscopeAddr string `env:"PYROSCOPE_ADDR"` + RedisPrefix string `env:"REDIS_PREFIX" envDefault:"GUGUTIK"` + PostgreSQLSchema string `env:"POSTGRESQL_SCHEMA" envDefault:""` + RedisMaster string `env:"REDIS_MASTER"` + ConsulAnonymityPrefix string `env:"CONSUL_ANONYMITY_NAME" envDefault:""` + RabbitMQUsername string `env:"RABBITMQ_USERNAME" envDefault:"guest"` + RabbitMQPassword string `env:"RABBITMQ_PASSWORD" envDefault:"guest"` + RabbitMQAddr string `env:"RABBITMQ_ADDRESS" envDefault:"localhost"` + RabbitMQPort string `env:"RABBITMQ_PORT" envDefault:"5672"` + RabbitMQVhostPrefix string `env:"RABBITMQ_VHOST_PREFIX" envDefault:""` + ChatGPTAPIKEYS string `env:"CHATGPT_API_KEYS"` + PodIpAddr string `env:"POD_IP" envDefault:"localhost"` + GorseAddr string `env:"GORSE_ADDR"` + GorseApiKey string `env:"GORSE_APIKEY"` + MagicUserId uint32 `env:"MAGIC_USER_ID" envDefault:"1"` + ChatGptProxy string `env:"CHATGPT_PROXY"` + PostgreSQLPrefix string `env:"POSTGRESQL_PREFIX" envDefault:""` + PostgreSQLReplicaState string `env:"POSTGRESQL_REPLICA"` + PostgreSQLReplicaAddress string `env:"POSTGRESQL_REPLICA_ADDR"` + PostgreSQLReplicaUsername string `env:"POSTGRESQL_REPLICA_USER"` + PostgreSQLReplicaPassword string `env:"POSTGRESQL_REPLICA_PASSWORD"` + OtelState string `env:"TRACING_STATE" envDefault:"enable"` + OtelSampler float64 `env:"TRACING_SAMPLER" envDefault:"0.01"` } func init() { diff --git a/src/extra/tracing/otel.go b/src/extra/tracing/otel.go index c2f63a9..406b4c7 100644 --- a/src/extra/tracing/otel.go +++ b/src/extra/tracing/otel.go @@ -30,6 +30,13 @@ func SetTraceProvider(name string) (*trace.TracerProvider, error) { return nil, err } + var sampler trace.Sampler + if config.EnvCfg.OtelState == "disable" { + sampler = trace.NeverSample() + } else { + sampler = trace.TraceIDRatioBased(config.EnvCfg.OtelSampler) + } + tp := trace.NewTracerProvider( trace.WithBatcher(exporter), trace.WithResource( @@ -38,7 +45,7 @@ func SetTraceProvider(name string) (*trace.TracerProvider, error) { semconv.ServiceNameKey.String(name), ), ), - trace.WithSampler(trace.AlwaysSample()), + trace.WithSampler(sampler), ) otel.SetTracerProvider(tp) otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))