This repository has been archived by the owner on Jun 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
observability.go
57 lines (51 loc) · 2.39 KB
/
observability.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Package observability contains common setup and utilities for metrics, logging, and tracing.
package observability
import (
"context"
"os"
"go.uber.org/zap"
"knative.dev/pkg/configmap/informer"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/logging"
"knative.dev/pkg/metrics"
"knative.dev/pkg/profiling"
"knative.dev/pkg/tracing"
tracingconfig "knative.dev/pkg/tracing/config"
)
// SetupDynamicConfigOrDie sets up logging, metrics, and tracing by watching observability
// configmaps. Returns an updated context with logging and function to flush telemetry which should
// be called before exit.
// The input context should have KubeClient injected.
func SetupDynamicConfigOrDie(ctx context.Context, componentName string, metricNamespace string) (context.Context, *informer.InformedWatcher, *profiling.Handler, func()) {
metrics.MemStatsOrDie(ctx)
// Set up our logger.
logger, atomicLevel := sharedmain.SetupLoggerOrDie(ctx, componentName)
// Watch the logging config map and dynamically update logging levels.
configMapWatcher := sharedmain.SetupConfigMapWatchOrDie(ctx, logger)
// Watch the logging config map and dynamically update logging levels.
sharedmain.WatchLoggingConfigOrDie(ctx, configMapWatcher, logger, atomicLevel, componentName)
// Watch the observability config map
ph := profiling.NewHandler(logger, false)
sharedmain.WatchObservabilityConfigOrDie(ctx, configMapWatcher, ph, logger, metricNamespace)
// Watch the tracing config map
setupTracingOrDie(configMapWatcher, logger, componentName)
configMapCtx, cancel := context.WithCancel(ctx)
// configMapWatcher does not block, so start it first.
logger.Info("Starting configuration manager...")
if err := configMapWatcher.Start(configMapCtx.Done()); err != nil {
logger.Fatal("Failed to start ConfigMap watcher", zap.Error(err))
}
return logging.WithLogger(ctx, logger), configMapWatcher, ph, func() { cancel(); flushExporters(logger) }
}
func setupTracingOrDie(configMapWatcher *informer.InformedWatcher, logger *zap.SugaredLogger, componentName string) {
if err := tracing.SetupDynamicPublishing(logger, configMapWatcher, componentName, tracingconfig.ConfigName); err != nil {
logger.With(zap.Error(err)).Fatalf("Error reading ConfigMap %q", tracingconfig.ConfigName)
}
}
// TODO: flush tracers
func flushExporters(logger *zap.SugaredLogger) {
logger.Sync()
metrics.FlushExporter()
os.Stdout.Sync()
os.Stderr.Sync()
}