Skip to content

Commit

Permalink
refactor: refactoring otel service
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeversiane committed Oct 23, 2024
1 parent 590a964 commit cd3d9e8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
14 changes: 4 additions & 10 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 2 additions & 7 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 21 additions & 3 deletions internal/infra/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -80,3 +95,6 @@ func (c *Config) GetLogConfig() LogConfig {
return c.Log
}

func (c *Config) GetObservabilityConfig() ObservabilityConfig {
return c.Observability
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -81,15 +75,15 @@ 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),
semconv.ServiceVersion(config.ServiceVersion),
))
}

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 != "" {
Expand All @@ -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 != "" {
Expand Down

0 comments on commit cd3d9e8

Please sign in to comment.