Skip to content

Commit

Permalink
feat: sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
liaosunny123 committed Aug 31, 2023
1 parent 1480e75 commit 792c330
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
8 changes: 6 additions & 2 deletions .env.docker.compose
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/constant/config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
78 changes: 40 additions & 38 deletions src/constant/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 8 additions & 1 deletion src/extra/tracing/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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{}))
Expand Down

0 comments on commit 792c330

Please sign in to comment.