Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion cla-backend-go/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import (

"github.com/linuxfoundation/easycla/cla-backend-go/api_logs"
"github.com/linuxfoundation/easycla/cla-backend-go/signatures"
"github.com/linuxfoundation/easycla/cla-backend-go/telemetry"
v2Signatures "github.com/linuxfoundation/easycla/cla-backend-go/v2/signatures"

ini "github.com/linuxfoundation/easycla/cla-backend-go/init"
Expand Down Expand Up @@ -146,6 +147,45 @@ type combinedRepo struct {
projects_cla_groups.Repository
}

const (
envDDBAPILogging = "DDB_API_LOGGING"
envOtelDatadogAPILogging = "OTEL_DATADOG_API_LOGGING"
)

// parseBoolish parses common "boolean-ish" env var values.
// Returns (value, ok). ok=false means "unknown/invalid".
func parseBoolish(v string) (bool, bool) {
s := strings.TrimSpace(strings.ToLower(v))
switch s {
case "1", "true", "yes", "y", "on":
return true, true
case "0", "false", "no", "n", "off":
return false, true
default:
return false, false
}
}

// enabledByEnvOrStage implements:
// - if env var set to true/1/yes -> enabled
// - if env var set to false/0/no -> disabled
// - if env var unset/empty -> defaultByStage[idx]
// - idx 0 = dev/default stage, index 1 = prod stage
func enabledByEnvOrStage(envVar, stage string, defaultByStage [2]bool) bool {
if raw, ok := os.LookupEnv(envVar); ok && strings.TrimSpace(raw) != "" {
if b, ok2 := parseBoolish(raw); ok2 {
return b
}
log.Warnf("LG:api-log-flag-invalid:%s value=%q (falling back to STAGE default)", envVar, raw)
}
st := strings.TrimSpace(strings.ToLower(stage))
if st == "prod" {
return defaultByStage[1]
}
// dev and all non-prod stages default to enabled
return defaultByStage[0]
}

// apiPathLoggerWithDB creates a middleware that logs API requests to DynamoDB
func apiPathLoggerWithDB(apiLogsRepo api_logs.Repository) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
Expand Down Expand Up @@ -177,6 +217,8 @@ func apiPathLoggerWithDB(apiLogsRepo api_logs.Repository) func(http.Handler) htt
}

// server function called by environment specific server functions
//
//nolint:gocyclo
func server(localMode bool) http.Handler {
f := logrus.Fields{
"functionName": "cmd.server",
Expand Down Expand Up @@ -208,6 +250,28 @@ func server(localMode bool) http.Handler {

stage := viper.GetString("STAGE")
dynamodbRegion := ini.GetProperty("DYNAMODB_AWS_REGION")
ddbAPILoggingEnabled := enabledByEnvOrStage(envDDBAPILogging, stage, [2]bool{true, false})
otelDatadogEnabled := enabledByEnvOrStage(envOtelDatadogAPILogging, stage, [2]bool{true, true})

// Initialize OTel SDK -> Datadog Lambda Extension (OTLP) once at cold start.
// If init fails, disable OTel logging but never fail startup.
if otelDatadogEnabled {
version := strings.TrimSpace(Commit)
if strings.TrimSpace(version) == "" {
version = Version
}
if strings.TrimSpace(version) == "" {
version = "unknown"
}
if er := telemetry.InitDatadogOTel(telemetry.DatadogOTelConfig{
Stage: stage,
Service: "easycla-backend",
Version: version,
}); er != nil {
log.Infof("LG:otel-datadog-disabled err=%v", er)
otelDatadogEnabled = false
}
}

log.WithFields(f).Infof("Service %s starting...", ini.ServiceName)

Expand All @@ -221,6 +285,8 @@ func server(localMode bool) http.Handler {
log.Infof("Golang OS : %s", runtime.GOOS)
log.Infof("Golang Arch : %s", runtime.GOARCH)
log.Infof("DYANAMODB_AWS_REGION : %s", dynamodbRegion)
log.Infof("DDB_API_LOGGING : %t", ddbAPILoggingEnabled)
log.Infof("OTEL_DATADOG_API_LOGGING: %t", otelDatadogEnabled)
log.Infof("GH_ORG_VALIDATION : %t", githubOrgValidation)
log.Infof("COMPANY_USER_VALIDATION : %t", companyUserValidation)
log.Infof("STAGE : %s", stage)
Expand All @@ -239,6 +305,8 @@ func server(localMode bool) http.Handler {
f["companyUserValidation"] = companyUserValidation
f["stage"] = stage
f["serviceHost"] = host
f["ddbAPILogging"] = ddbAPILoggingEnabled
f["otelDatadog"] = otelDatadogEnabled
log.WithFields(f).Info("config")
}

Expand Down Expand Up @@ -305,7 +373,14 @@ func server(localMode bool) http.Handler {
approvalListRepo := approval_list.NewRepository(awsSession, stage)
v1CompanyRepo := v1Company.NewRepository(awsSession, stage)
eventsRepo := events.NewRepository(awsSession, stage)
apiLogsRepo := api_logs.NewRepository(stage, dynamodb.New(awsSession))

var apiLogsRepo api_logs.Repository
if ddbAPILoggingEnabled {
apiLogsRepo = api_logs.NewRepository(stage, dynamodb.New(awsSession))
} else {
apiLogsRepo = nil
}

v1ProjectClaGroupRepo := projects_cla_groups.NewRepository(awsSession, stage)
v1CLAGroupRepo := repository.NewRepository(awsSession, stage, gitV1Repository, gerritRepo, v1ProjectClaGroupRepo)
metricsRepo := metrics.NewRepository(awsSession, stage, configFile.APIGatewayURL, v1ProjectClaGroupRepo)
Expand Down Expand Up @@ -497,6 +572,12 @@ func server(localMode bool) http.Handler {
v2API.Serve(middlewareSetupfunc), v2SwaggerSpec.BasePath()),
configFile.AllowedOrigins)
}

// OTel/Datadog (OTLP -> Datadog Lambda Extension) - enabled by flag
if otelDatadogEnabled {
apiHandler = telemetry.WrapHTTPHandler(apiHandler)
}

return apiHandler
}

Expand Down
43 changes: 30 additions & 13 deletions cla-backend-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ require (
github.com/gofrs/uuid v4.0.0+incompatible
github.com/golang/mock v1.6.0
github.com/google/go-github/v37 v37.0.0
github.com/google/uuid v1.1.4
github.com/google/uuid v1.6.0
github.com/gorilla/sessions v1.2.1 // indirect
github.com/imroc/req v0.3.0
github.com/jessevdk/go-flags v1.4.0
github.com/jinzhu/copier v0.0.0-20190924061706-b57f9002281a
github.com/jmoiron/sqlx v1.2.0
github.com/juju/mempool v0.0.0-20160205104927-24974d6c264f // indirect
github.com/juju/zip v0.0.0-20160205105221-f6b1e93fa2e2
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mitchellh/mapstructure v1.5.0
Expand All @@ -51,45 +51,55 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.11.1
github.com/verdverm/frisby v0.0.0-20170604211311-b16556248a9a
github.com/xanzy/go-gitlab v0.50.1
go.uber.org/ratelimit v0.1.0
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/crypto v0.47.0 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/net v0.8.0
golang.org/x/oauth2 v0.6.0
golang.org/x/sync v0.2.0
golang.org/x/sys v0.33.0 // indirect
golang.org/x/net v0.49.0
golang.org/x/oauth2 v0.34.0
golang.org/x/sync v0.19.0
golang.org/x/sys v0.40.0 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
)

require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
github.com/bradleyfalzon/ghinstallation/v2 v2.2.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang-jwt/jwt/v4 v4.5.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.65.0
go.opentelemetry.io/otel v1.40.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.40.0
go.opentelemetry.io/otel/sdk v1.40.0
)

require (
github.com/ProtonMail/go-crypto v0.0.0-20230321155629-9a39f2531310 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/circl v1.3.2 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-github/v50 v50.2.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.7 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -110,16 +120,23 @@ require (
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
go.mongodb.org/mongo-driver v1.10.1 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.40.0 // indirect
go.opentelemetry.io/otel/metric v1.40.0 // indirect
go.opentelemetry.io/otel/trace v1.40.0 // indirect
go.opentelemetry.io/proto/otlp v1.9.0 // indirect
golang.org/x/text v0.33.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260209200024-4cfbd4190f57 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260209200024-4cfbd4190f57 // indirect
google.golang.org/grpc v1.78.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading
Loading