From cd3d9e8a1e1391236816f0bd44481735ecf0e66b Mon Sep 17 00:00:00 2001 From: felipeversiane Date: Tue, 22 Oct 2024 22:33:38 -0300 Subject: [PATCH] refactor: refactoring otel service --- .env.example | 14 +++------- cmd/server/main.go | 9 ++----- internal/infra/config/config.go | 24 ++++++++++++++--- .../{otel.go => observability.go} | 26 +++++++------------ 4 files changed, 37 insertions(+), 36 deletions(-) rename internal/infra/services/observability/{otel.go => observability.go} (81%) diff --git a/.env.example b/.env.example index 8a19dae..387a0c0 100644 --- a/.env.example +++ b/.env.example @@ -15,13 +15,7 @@ POSTGRES_USER=your_postgres_user POSTGRES_PASSWORD=your_postgres_password POSTGRES_DB=your_postgres_db -#AWS Configuration -AWS_REGION=us-east-1 -AWS_ENDPOINT= http://minio:9000 -AWS_ACCESS_KEY_ID=minioadmin -AWS_SECRET_ACCESS_KEY=minioadmin - -#S3 Configuration -S3_BUCKET=donate-api -S3_ACL=public-read -S3_URL=http://localhost:9000 +#Observability Configuration +OTEL_SERVICE_NAME=service-name +OTEL_SERVICE_VERSION=0.0.1 +OTEL_EXPORTER_ENDPOINT=otel-collector:4317 \ No newline at end of file diff --git a/cmd/server/main.go b/cmd/server/main.go index 2d36f3c..abe8f8d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -17,13 +17,8 @@ func main() { logger.Configure() ctx := context.Background() - otel := observability.NewOtel(observability.OtelConfig{ - ServiceName: "go-otel", - ServiceVersion: "0.0.1", - OtelExporterOtlpEndpoint: "otel-collector:4317", - OtelExporterOtlpInsecure: true, - }) - otel.SetupOtel(ctx) + observer := observability.NewObserver(cfg.GetObservabilityConfig()) + observer.SetupOtel(ctx) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/internal/infra/config/config.go b/internal/infra/config/config.go index fb80c30..46de0fc 100644 --- a/internal/infra/config/config.go +++ b/internal/infra/config/config.go @@ -11,15 +11,17 @@ var ( ) type Config struct { - Database DatabaseConfig - Server ServerConfig - Log LogConfig + Database DatabaseConfig + Server ServerConfig + Log LogConfig + Observability ObservabilityConfig } type ConfigInterface interface { GetDatabaseConfig() DatabaseConfig GetServerConfig() ServerConfig GetLogConfig() LogConfig + GetObservabilityConfig() ObservabilityConfig } type DatabaseConfig struct { @@ -38,6 +40,13 @@ type LogConfig struct { Level string } +type ObservabilityConfig struct { + ServiceName string + ServiceVersion string + OtelExporterOtlpEndpoint string + OtelExporterOtlpInsecure bool +} + func NewConfig() ConfigInterface { var cfg *Config once.Do(func() { @@ -55,6 +64,12 @@ func NewConfig() ConfigInterface { Log: LogConfig{ Level: getEnvOrDie("LOG_LEVEL"), }, + Observability: ObservabilityConfig{ + ServiceName: getEnvOrDie("OTEL_SERVICE_NAME"), + ServiceVersion: getEnvOrDie("OTEL_SERVICE_VERSION"), + OtelExporterOtlpEndpoint: getEnvOrDie("OTEL_EXPORTER_ENDPOINT"), + OtelExporterOtlpInsecure: true, + }, } }) return cfg @@ -80,3 +95,6 @@ func (c *Config) GetLogConfig() LogConfig { return c.Log } +func (c *Config) GetObservabilityConfig() ObservabilityConfig { + return c.Observability +} diff --git a/internal/infra/services/observability/otel.go b/internal/infra/services/observability/observability.go similarity index 81% rename from internal/infra/services/observability/otel.go rename to internal/infra/services/observability/observability.go index c621dc1..7f37b70 100644 --- a/internal/infra/services/observability/otel.go +++ b/internal/infra/services/observability/observability.go @@ -5,6 +5,7 @@ import ( "errors" "time" + "github.com/felipeversiane/go-otel/internal/infra/config" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" @@ -14,26 +15,19 @@ import ( semconv "go.opentelemetry.io/otel/semconv/v1.21.0" ) -type Otel struct { - config OtelConfig +type observer struct { + config config.ObservabilityConfig } -type OtelConfig struct { - ServiceName string - ServiceVersion string - OtelExporterOtlpEndpoint string - OtelExporterOtlpInsecure bool -} - -type OtelInterface interface { +type ObserverInterface interface { SetupOtel(context.Context) (shutdown func(context.Context) error, err error) } -func NewOtel(config OtelConfig) OtelInterface { - return Otel{config} +func NewObserver(config config.ObservabilityConfig) ObserverInterface { + return observer{config} } -func (o Otel) SetupOtel(ctx context.Context) (shutdown func(context.Context) error, err error) { +func (o observer) SetupOtel(ctx context.Context) (shutdown func(context.Context) error, err error) { var shutdownFuncs []func(context.Context) error // shutdown calls cleanup functions registered via shutdownFuncs. @@ -81,7 +75,7 @@ func (o Otel) SetupOtel(ctx context.Context) (shutdown func(context.Context) err return shutdown, err } -func newResource(config OtelConfig) (*resource.Resource, error) { +func newResource(config config.ObservabilityConfig) (*resource.Resource, error) { return resource.Merge(resource.Default(), resource.NewWithAttributes(semconv.SchemaURL, semconv.ServiceName(config.ServiceName), @@ -89,7 +83,7 @@ func newResource(config OtelConfig) (*resource.Resource, error) { )) } -func newTraceProvider(ctx context.Context, config OtelConfig, res *resource.Resource) (*trace.TracerProvider, error) { +func newTraceProvider(ctx context.Context, config config.ObservabilityConfig, res *resource.Resource) (*trace.TracerProvider, error) { options := []otlptracegrpc.Option{} if config.OtelExporterOtlpEndpoint != "" { @@ -114,7 +108,7 @@ func newTraceProvider(ctx context.Context, config OtelConfig, res *resource.Reso return traceProvider, nil } -func newMeterProvider(ctx context.Context, config OtelConfig, res *resource.Resource) (*metric.MeterProvider, error) { +func newMeterProvider(ctx context.Context, config config.ObservabilityConfig, res *resource.Resource) (*metric.MeterProvider, error) { options := []otlpmetricgrpc.Option{} if config.OtelExporterOtlpEndpoint != "" {