Skip to content

Commit

Permalink
Added new Log Enconder Config (#2927)
Browse files Browse the repository at this point in the history
* Added new Log Enconder Config

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

* Added new Log Enconder Config

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

* Added new Log Enconder Config

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

* Added new Log Enconder Config

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

* Added new Log Enconder Config

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

* Added new Log Enconder Config

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

* Added new Debug doc

Signed-off-by: Yuri Sa <yurimsa@gmail.com>

---------

Signed-off-by: Yuri Sa <yurimsa@gmail.com>
  • Loading branch information
yuriolisa authored May 13, 2024
1 parent 9913aec commit eb44580
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/customized-log-encoder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Enabling new Logs Enconder Configuration parameters.

# One or more tracking issues related to the change
issues: [268]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
34 changes: 34 additions & 0 deletions DEBUG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Debug tips to the OpenTelemetry Operator

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.

## Customizing Logs Output
By the default the Operator's log format is console like you can see below:
```bash
2024-05-06T11:55:11+02:00 INFO setup Prometheus CRDs are installed, adding to scheme.
2024-05-06T11:55:11+02:00 INFO setup Openshift CRDs are not installed, skipping adding to scheme.
2024-05-06T11:55:11+02:00 INFO setup the env var WATCH_NAMESPACE isn't set, watching all namespaces
2024-05-06T11:55:11+02:00 INFO Webhooks are disabled, operator is running an unsupported mode {"ENABLE_WEBHOOKS": "false"}
2024-05-06T11:55:11+02:00 INFO setup starting manager
```
If it is necessary to customize the log format, so you can use one of the following parameters:
- `--zap-devel`: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error) (default false)
- `--zap-encoder`: Zap log encoding (one of 'json' or 'console')
- `--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
- `--zap-stacktrace-level` Zap Level at and above which stacktraces are captured (one of 'info', 'error', 'panic').
- `--zap-time-encoding` Zap time encoding (one of 'epoch', 'millis', 'nano', 'iso8601', 'rfc3339' or 'rfc3339nano'). Defaults to 'epoch'.
- The following parameters are effective only if the `--zap-encoder=json`:
- `zap-message-key`: The message key to be used in the customized Log Encoder
- `zap-level-key`: The level key to be used in the customized Log Encoder
- `zap-time-key`: The time key to be used in the customized Log Encoder
- `zap-level-format`: The level format to be used in the customized Log Encoder
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:
```bash
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"Prometheus CRDs are installed, adding to scheme."}
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"Openshift CRDs are not installed, skipping adding to scheme."}
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"the env var WATCH_NAMESPACE isn't set, watching all namespaces"}
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","msg":"Webhooks are disabled, operator is running an unsupported mode","ENABLE_WEBHOOKS":"false"}
{"severity":"INFO","timestamp":"2024-05-07T16:23:35+02:00","logger":"setup","msg":"starting manager"}
```
9 changes: 9 additions & 0 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"

"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"

"github.com/open-telemetry/opentelemetry-operator/internal/autodetect"
"github.com/open-telemetry/opentelemetry-operator/internal/autodetect/openshift"
Expand Down Expand Up @@ -255,3 +256,11 @@ func WithAnnotationFilters(annotationFilters []string) Option {
o.annotationsFilter = filters
}
}

func WithEncodeLevelFormat(s string) zapcore.LevelEncoder {
if s == "lowercase" {
return zapcore.LowercaseLevelEncoder
} else {
return zapcore.CapitalLevelEncoder
}
}
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"github.com/spf13/pflag"
colfeaturegate "go.opentelemetry.io/collector/featuregate"
"go.uber.org/zap/zapcore"
networkingv1 "k8s.io/api/networking/v1"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand Down Expand Up @@ -132,6 +133,10 @@ func main() {
annotationsFilter []string
webhookPort int
tlsOpt tlsConfig
encodeMessageKey string
encodeLevelKey string
encodeTimeKey string
encodeLevelFormat string
)

pflag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
Expand Down Expand Up @@ -163,8 +168,19 @@ func main() {
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")
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.")
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")
pflag.StringVar(&encodeMessageKey, "zap-message-key", "message", "The message key to be used in the customized Log Encoder")
pflag.StringVar(&encodeLevelKey, "zap-level-key", "level", "The level key to be used in the customized Log Encoder")
pflag.StringVar(&encodeTimeKey, "zap-time-key", "timestamp", "The time key to be used in the customized Log Encoder")
pflag.StringVar(&encodeLevelFormat, "zap-level-format", "uppercase", "The level format to be used in the customized Log Encoder")
pflag.Parse()

opts.EncoderConfigOptions = append(opts.EncoderConfigOptions, func(ec *zapcore.EncoderConfig) {
ec.MessageKey = encodeMessageKey
ec.LevelKey = encodeLevelKey
ec.TimeKey = encodeTimeKey
ec.EncodeLevel = config.WithEncodeLevelFormat(encodeLevelFormat)
})

logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)

Expand Down Expand Up @@ -195,6 +211,10 @@ func main() {
"enable-nginx-instrumentation", enableNginxInstrumentation,
"enable-nodejs-instrumentation", enableNodeJSInstrumentation,
"enable-java-instrumentation", enableJavaInstrumentation,
"zap-message-key", encodeMessageKey,
"zap-level-key", encodeLevelKey,
"zap-time-key", encodeTimeKey,
"zap-level-format", encodeLevelFormat,
)

restConfig := ctrl.GetConfigOrDie()
Expand Down

0 comments on commit eb44580

Please sign in to comment.