Skip to content

Commit

Permalink
enable gcp tracing env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal-Delange committed Mar 8, 2024
1 parent 6b59644 commit 82cada2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ FIREBASE_AUTH_EMULATOR_HOST="localhost:9099"
# - by the opentelemetry tracing agent to send traces to the collector
GOOGLE_CLOUD_PROJECT="tokyo-country-381508"

# Enable to activate GCP tracing
# If activated, you MUST have GCP application default credentials set up - see https://cloud.google.com/trace/docs/setup/go-ot
# Will send error logs (but the app will still run) if an unexisting project is specified in GOOGLE_CLOUD_PROJECT or
# if the runner does not have the correct permissions to use tracing on the project
ENABLE_GCP_TRACING=false

# configure the document storage backend with optional fake backends
GCS_INGESTION_BUCKET="data-ingestion-bucket"
Expand Down
51 changes: 26 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ type dependencies struct {
Authentication *api.Authentication
TokenHandler *api.TokenHandler
SegmentClient analytics.Client
OpenTelemetryTracer *trace.Tracer
OpenTelemetryTracer trace.Tracer
}

func initDependencies(conf AppConfiguration, signingKey *rsa.PrivateKey) (dependencies, error) {
tracer, err := tracing.Init(tracing.Configuration{
ApplicationName: "marble-backend",
Enabled: conf.enableGcpTracing,
ProjectID: conf.gcpProject,
})
if err != nil {
return dependencies{}, err
}

database, err := postgres.New(postgres.Configuration{
Host: conf.pgConfig.Hostname,
Port: conf.pgConfig.Port,
Expand All @@ -58,20 +67,10 @@ func initDependencies(conf AppConfiguration, signingKey *rsa.PrivateKey) (depend
segmentClient := analytics.New(conf.config.SegmentWriteKey)

deps := dependencies{
Authentication: api.NewAuthentication(tokenValidator),
TokenHandler: api.NewTokenHandler(tokenGenerator),
SegmentClient: segmentClient,
}

tracer, err := tracing.Init(tracing.Configuration{
Enabled: conf.env != "development",
ApplicationName: "marble-backend",
ProjectID: conf.gcpProject,
})
if err != nil {
fmt.Println("tracing.Init error: ", err)
} else {
deps.OpenTelemetryTracer = &tracer
Authentication: api.NewAuthentication(tokenValidator),
OpenTelemetryTracer: tracer,
SegmentClient: segmentClient,
TokenHandler: api.NewTokenHandler(tokenGenerator),
}

return deps, nil
Expand Down Expand Up @@ -137,20 +136,22 @@ func runServer(ctx context.Context, appConfig AppConfiguration) {
}

type AppConfiguration struct {
env string
port string
gcpProject string
pgConfig utils.PGConfig
config models.GlobalConfiguration
sentryDsn string
metabase models.MetabaseConfiguration
env string
port string
gcpProject string
enableGcpTracing bool
pgConfig utils.PGConfig
config models.GlobalConfiguration
sentryDsn string
metabase models.MetabaseConfiguration
}

func main() {
appConfig := AppConfiguration{
env: utils.GetEnv("ENV", "development"),
port: utils.GetRequiredEnv[string]("API_PORT"),
gcpProject: os.Getenv("GOOGLE_CLOUD_PROJECT"),
env: utils.GetEnv("ENV", "development"),
port: utils.GetRequiredEnv[string]("API_PORT"),
gcpProject: os.Getenv("GOOGLE_CLOUD_PROJECT"),
enableGcpTracing: utils.GetEnv("ENABLE_GCP_TRACING", false),
pgConfig: utils.PGConfig{
Database: "marble",
DbConnectWithSocket: utils.GetEnv("PG_CONNECT_WITH_SOCKET", false),
Expand Down
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func initRouter(ctx context.Context, conf AppConfiguration, deps dependencies) *
r.Use(utils.StoreLoggerInContextMiddleware(logger))
r.Use(utils.StoreSegmentClientInContextMiddleware(deps.SegmentClient))
if deps.OpenTelemetryTracer != nil {
r.Use(utils.StoreOpenTelemetryTracerInContextMiddleware(*deps.OpenTelemetryTracer))
r.Use(utils.StoreOpenTelemetryTracerInContextMiddleware(deps.OpenTelemetryTracer))
}

return r
Expand Down

0 comments on commit 82cada2

Please sign in to comment.