Skip to content

Commit

Permalink
Implementing feature flag for otel
Browse files Browse the repository at this point in the history
  • Loading branch information
franmoore05 committed Feb 5, 2024
1 parent 5ddac2d commit b788e5e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Config struct {
OTExporterOTLPEndpoint string `envconfig:"OTEL_EXPORTER_OTLP_ENDPOINT"`
OTServiceName string `envconfig:"OTEL_SERVICE_NAME"`
OTBatchTimeout time.Duration `envconfig:"OTEL_BATCH_TIMEOUT"`
OtelEnabled bool `envconfig:"OTEL_ENABLED"`
Auth authorisation.Config
}

Expand Down Expand Up @@ -179,6 +180,7 @@ func Get() (*Config, error) {
OTExporterOTLPEndpoint: "localhost:4317",
OTServiceName: "dp-api-router",
OTBatchTimeout: time.Second * 5,
OtelEnabled: false,
}

return cfg, envconfig.Process("", cfg)
Expand Down
14 changes: 13 additions & 1 deletion interceptor/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package interceptor

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -13,6 +14,7 @@ import (
"strconv"
"strings"

"github.com/ONSdigital/dp-api-router/config"
"github.com/ONSdigital/log.go/v2/log"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
Expand All @@ -29,7 +31,17 @@ var _ http.RoundTripper = &Transport{}

// NewRoundTripper creates a Transport instance with configured domain
func NewRoundTripper(domain, contextURL string, rt http.RoundTripper) *Transport {
return &Transport{domain, contextURL, otelhttp.NewTransport(rt)}
cfg, err := config.Get()
if err != nil {
log.Error(context.Background(), "Unable to retrieve config'", err)
}

if cfg.OtelEnabled {
return &Transport{domain, contextURL, otelhttp.NewTransport(rt)}

}

return &Transport{domain, contextURL, rt}
}

const (
Expand Down
29 changes: 15 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,23 @@ func run(ctx context.Context) error {
return errors.Wrap(err, "unable to retrieve service configuration")
}

// // Set up OpenTelemetry
otelConfig := dpotelgo.Config{
OtelServiceName: cfg.OTServiceName,
OtelExporterOtlpEndpoint: cfg.OTExporterOTLPEndpoint,
OtelBatchTimeout: cfg.OTBatchTimeout,
}
if cfg.OtelEnabled {
// // Set up OpenTelemetry
otelConfig := dpotelgo.Config{
OtelServiceName: cfg.OTServiceName,
OtelExporterOtlpEndpoint: cfg.OTExporterOTLPEndpoint,
OtelBatchTimeout: cfg.OTBatchTimeout,
}

otelShutdown, oErr := dpotelgo.SetupOTelSDK(ctx, otelConfig)
if oErr != nil {
log.Error(ctx, "error setting up OpenTelemetry - hint: ensure OTEL_EXPORTER_OTLP_ENDPOINT is set", oErr)
otelShutdown, oErr := dpotelgo.SetupOTelSDK(ctx, otelConfig)
if oErr != nil {
log.Error(ctx, "error setting up OpenTelemetry - hint: ensure OTEL_EXPORTER_OTLP_ENDPOINT is set", oErr)
}
// Handle shutdown properly so nothing leaks.
defer func() {
err = goerrors.Join(err, otelShutdown(context.Background()))
}()
}
// Handle shutdown properly so nothing leaks.
defer func() {
err = goerrors.Join(err, otelShutdown(context.Background()))
}()

// Run the service
svc, err := service.Run(ctx, cfg, svcList, BuildTime, GitCommit, Version, svcErrors)
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ func Run(ctx context.Context, cfg *config.Config, serviceList *ExternalServiceLi
r := CreateRouter(ctx, cfg)
otelhandler := otelhttp.NewHandler(r, "/")
m := svc.CreateMiddleware(cfg, r)
r.Use(otelmux.Middleware(cfg.OTServiceName))
svc.Server = dphttp.NewServer(cfg.BindAddr, m.Then(otelhandler))

if cfg.OtelEnabled {
r.Use(otelmux.Middleware(cfg.OTServiceName))
svc.Server = dphttp.NewServer(cfg.BindAddr, m.Then(otelhandler))
} else {
svc.Server = dphttp.NewServer(cfg.BindAddr, m.Then(r))
}

svc.Server.DefaultShutdownTimeout = cfg.GracefulShutdown
svc.Server.HandleOSSignals = false
Expand Down

0 comments on commit b788e5e

Please sign in to comment.