Skip to content

Commit

Permalink
chore(self-monitoring): let otel.go handle shutdown functions
Browse files Browse the repository at this point in the history
  • Loading branch information
basti1302 committed Sep 19, 2024
1 parent 4ef4140 commit 1fed614
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
9 changes: 4 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ var (
deploymentSelfReference *appsv1.Deployment
envVars environmentVariables

metricNamePrefix = fmt.Sprintf("%s.", meterName)
meter otelmetric.Meter
otelShutdownFunctions []func(ctx context.Context) error
metricNamePrefix = fmt.Sprintf("%s.", meterName)
meter otelmetric.Meter
)

func init() {
Expand Down Expand Up @@ -205,7 +204,7 @@ func main() {
os.Exit(1)
}

meter, otelShutdownFunctions =
meter =
common.InitOTelSdk(
ctx,
meterName,
Expand Down Expand Up @@ -335,7 +334,7 @@ func startOperatorManager(
}
// ^mgr.Start(...) blocks. It only returns when the manager is terminating.

common.ShutDownOTelSdk(ctx, otelShutdownFunctions)
common.ShutDownOTelSdk(ctx)

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion helm-chart/dash0-operator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ provided explicitly.
Note that by default, Kubernetes secrets are stored _unencrypted_, and anyone with API access to the Kubernetes cluster
will be able to read the value.
Additional steps are required to make sure secret values are encrypted, if that is desired.
See https://kubernetes.io/docs/concepts/configur**ation/secret/ for more information on Kubernetes secrets.
See https://kubernetes.io/docs/concepts/configuration/secret/ for more information on Kubernetes secrets.

### Dash0 Dataset Configuration

Expand Down
4 changes: 2 additions & 2 deletions images/configreloader/src/configreloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
done := make(chan bool, 1)
signal.Notify(shutdown, syscall.SIGTERM)

meter, selfMonitoringShutdownFunctions := common.InitOTelSdk(ctx, meterName, nil)
meter := common.InitOTelSdk(ctx, meterName, nil)
initializeSelfMonitoringMetrics(meter)

go func() {
Expand All @@ -96,7 +96,7 @@ func main() {

<-done

common.ShutDownOTelSdk(ctx, selfMonitoringShutdownFunctions)
common.ShutDownOTelSdk(ctx)
}

func initializeHashes(configurationFilePaths []string) error {
Expand Down
4 changes: 2 additions & 2 deletions images/filelogoffsetsynch/src/filelogoffsetsynch.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func main() {
log.Fatalf("Cannot create the Kube API client: %v\n", err)
}

meter, selfMonitoringShutdownFunctions := common.InitOTelSdk(ctx, meterName, nil)
meter := common.InitOTelSdk(ctx, meterName, nil)
initializeSelfMonitoringMetrics(meter)

// creates the clientset
Expand Down Expand Up @@ -138,7 +138,7 @@ func main() {
}
}

common.ShutDownOTelSdk(ctx, selfMonitoringShutdownFunctions)
common.ShutDownOTelSdk(ctx)
}

func initOffsets(ctx context.Context, settings *Settings) (int, error) {
Expand Down
29 changes: 15 additions & 14 deletions images/pkg/common/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import (
)

var (
meterProvider otelmetric.MeterProvider
meterProvider otelmetric.MeterProvider
shutdownFunctions []func(ctx context.Context) error
)

func InitOTelSdk(
ctx context.Context,
meterName string,
extraResourceAttributes map[string]string,
) (otelmetric.Meter, []func(ctx context.Context) error) {
) otelmetric.Meter {
podUid, isSet := os.LookupEnv("K8S_POD_UID")
if !isSet {
log.Println("Env var 'K8S_POD_UID' is not set")
Expand All @@ -42,8 +43,6 @@ func InitOTelSdk(
daemonSetUid := os.Getenv("K8S_DAEMONSET_UID")
deploymentUid := os.Getenv("K8S_DEPLOYMENT_UID")

var doMeterShutdown func(ctx context.Context) error

if _, isSet = os.LookupEnv("OTEL_EXPORTER_OTLP_ENDPOINT"); isSet {
var metricExporter sdkmetric.Exporter

Expand Down Expand Up @@ -100,26 +99,28 @@ func InitOTelSdk(
)

meterProvider = sdkMeterProvider
doMeterShutdown = sdkMeterProvider.Shutdown
shutdownFunctions = []func(ctx context.Context) error{
sdkMeterProvider.Shutdown,
}
} else {
meterProvider = metricnoop.MeterProvider{}
doMeterShutdown = func(ctx context.Context) error { return nil }
}

otel.SetMeterProvider(meterProvider)

return meterProvider.Meter(meterName), []func(ctx context.Context) error{
doMeterShutdown,
}
return meterProvider.Meter(meterName)
}

func ShutDownOTelSdk(ctx context.Context, shutdownFunctions []func(ctx context.Context) error) {
var err error
func ShutDownOTelSdk(ctx context.Context) {
if len(shutdownFunctions) == 0 {
return
}

timeoutCtx, cancelFun := context.WithTimeout(ctx, time.Second)
defer cancelFun()
for _, shutdownFunction := range shutdownFunctions {
timeoutCtx, cancelFun := context.WithTimeout(ctx, time.Second)
if err = shutdownFunction(timeoutCtx); err != nil {
if err := shutdownFunction(timeoutCtx); err != nil {
log.Printf("Failed to shutdown self monitoring, telemetry may have been lost:%v\n", err)
}
cancelFun()
}
}

0 comments on commit 1fed614

Please sign in to comment.