Skip to content

Commit eb44580

Browse files
authored
Added new Log Enconder Config (#2927)
* Added new Log Enconder Config Signed-off-by: Yuri Sa <[email protected]> * Added new Log Enconder Config Signed-off-by: Yuri Sa <[email protected]> * Added new Log Enconder Config Signed-off-by: Yuri Sa <[email protected]> * Added new Log Enconder Config Signed-off-by: Yuri Sa <[email protected]> * Added new Log Enconder Config Signed-off-by: Yuri Sa <[email protected]> * Added new Log Enconder Config Signed-off-by: Yuri Sa <[email protected]> * Added new Debug doc Signed-off-by: Yuri Sa <[email protected]> --------- Signed-off-by: Yuri Sa <[email protected]>
1 parent 9913aec commit eb44580

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

.chloggen/customized-log-encoder.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: 'enhancement'
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: operator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Enabling new Logs Enconder Configuration parameters.
9+
10+
# One or more tracking issues related to the change
11+
issues: [268]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

DEBUG.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Debug tips to the OpenTelemetry Operator
2+
3+
A tip during a troubleshooting process is always welcome. Therefore, we prepared this documentation to help you identify possible issues and improve the application's reliability.
4+
5+
## Customizing Logs Output
6+
By the default the Operator's log format is console like you can see below:
7+
```bash
8+
2024-05-06T11:55:11+02:00 INFO setup Prometheus CRDs are installed, adding to scheme.
9+
2024-05-06T11:55:11+02:00 INFO setup Openshift CRDs are not installed, skipping adding to scheme.
10+
2024-05-06T11:55:11+02:00 INFO setup the env var WATCH_NAMESPACE isn't set, watching all namespaces
11+
2024-05-06T11:55:11+02:00 INFO Webhooks are disabled, operator is running an unsupported mode {"ENABLE_WEBHOOKS": "false"}
12+
2024-05-06T11:55:11+02:00 INFO setup starting manager
13+
```
14+
15+
If it is necessary to customize the log format, so you can use one of the following parameters:
16+
- `--zap-devel`: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error) (default false)
17+
- `--zap-encoder`: Zap log encoding (one of 'json' or 'console')
18+
- `--zap-log-level` Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error', or any integer value > 0 which corresponds to custom debug levels of increasing verbosity
19+
- `--zap-stacktrace-level` Zap Level at and above which stacktraces are captured (one of 'info', 'error', 'panic').
20+
- `--zap-time-encoding` Zap time encoding (one of 'epoch', 'millis', 'nano', 'iso8601', 'rfc3339' or 'rfc3339nano'). Defaults to 'epoch'.
21+
- The following parameters are effective only if the `--zap-encoder=json`:
22+
- `zap-message-key`: The message key to be used in the customized Log Encoder
23+
- `zap-level-key`: The level key to be used in the customized Log Encoder
24+
- `zap-time-key`: The time key to be used in the customized Log Encoder
25+
- `zap-level-format`: The level format to be used in the customized Log Encoder
26+
27+
Running the Operator with the parameters `--zap-encoder=json`, `--zap-message-key="msg"`, `zap-level-key="severity"`,`zap-time-key="timestamp"`,`zap-level-format="uppercase"` you should see the following output:
28+
```bash
29+
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"Prometheus CRDs are installed, adding to scheme."}
30+
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"Openshift CRDs are not installed, skipping adding to scheme."}
31+
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"the env var WATCH_NAMESPACE isn't set, watching all namespaces"}
32+
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","msg":"Webhooks are disabled, operator is running an unsupported mode","ENABLE_WEBHOOKS":"false"}
33+
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"starting manager"}
34+
```

internal/config/options.go

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"strings"
2020

2121
"github.com/go-logr/logr"
22+
"go.uber.org/zap/zapcore"
2223

2324
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
2425
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
@@ -255,3 +256,11 @@ func WithAnnotationFilters(annotationFilters []string) Option {
255256
o.annotationsFilter = filters
256257
}
257258
}
259+
260+
func WithEncodeLevelFormat(s string) zapcore.LevelEncoder {
261+
if s == "lowercase" {
262+
return zapcore.LowercaseLevelEncoder
263+
} else {
264+
return zapcore.CapitalLevelEncoder
265+
}
266+
}

main.go

+20
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
3030
"github.com/spf13/pflag"
3131
colfeaturegate "go.opentelemetry.io/collector/featuregate"
32+
"go.uber.org/zap/zapcore"
3233
networkingv1 "k8s.io/api/networking/v1"
3334
k8sruntime "k8s.io/apimachinery/pkg/runtime"
3435
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -132,6 +133,10 @@ func main() {
132133
annotationsFilter []string
133134
webhookPort int
134135
tlsOpt tlsConfig
136+
encodeMessageKey string
137+
encodeLevelKey string
138+
encodeTimeKey string
139+
encodeLevelFormat string
135140
)
136141

137142
pflag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
@@ -163,8 +168,19 @@ func main() {
163168
pflag.StringArrayVar(&annotationsFilter, "annotations-filter", []string{}, "Annotations to filter away from propagating onto deploys. It should be a string array containing patterns, which are literal strings optionally containing a * wildcard character. Example: --annotations-filter=.*filter.out will filter out annotations that looks like: annotation.filter.out: true")
164169
pflag.StringVar(&tlsOpt.minVersion, "tls-min-version", "VersionTLS12", "Minimum TLS version supported. Value must match version names from https://golang.org/pkg/crypto/tls/#pkg-constants.")
165170
pflag.StringSliceVar(&tlsOpt.cipherSuites, "tls-cipher-suites", nil, "Comma-separated list of cipher suites for the server. Values are from tls package constants (https://golang.org/pkg/crypto/tls/#pkg-constants). If omitted, the default Go cipher suites will be used")
171+
pflag.StringVar(&encodeMessageKey, "zap-message-key", "message", "The message key to be used in the customized Log Encoder")
172+
pflag.StringVar(&encodeLevelKey, "zap-level-key", "level", "The level key to be used in the customized Log Encoder")
173+
pflag.StringVar(&encodeTimeKey, "zap-time-key", "timestamp", "The time key to be used in the customized Log Encoder")
174+
pflag.StringVar(&encodeLevelFormat, "zap-level-format", "uppercase", "The level format to be used in the customized Log Encoder")
166175
pflag.Parse()
167176

177+
opts.EncoderConfigOptions = append(opts.EncoderConfigOptions, func(ec *zapcore.EncoderConfig) {
178+
ec.MessageKey = encodeMessageKey
179+
ec.LevelKey = encodeLevelKey
180+
ec.TimeKey = encodeTimeKey
181+
ec.EncodeLevel = config.WithEncodeLevelFormat(encodeLevelFormat)
182+
})
183+
168184
logger := zap.New(zap.UseFlagOptions(&opts))
169185
ctrl.SetLogger(logger)
170186

@@ -195,6 +211,10 @@ func main() {
195211
"enable-nginx-instrumentation", enableNginxInstrumentation,
196212
"enable-nodejs-instrumentation", enableNodeJSInstrumentation,
197213
"enable-java-instrumentation", enableJavaInstrumentation,
214+
"zap-message-key", encodeMessageKey,
215+
"zap-level-key", encodeLevelKey,
216+
"zap-time-key", encodeTimeKey,
217+
"zap-level-format", encodeLevelFormat,
198218
)
199219

200220
restConfig := ctrl.GetConfigOrDie()

0 commit comments

Comments
 (0)