Skip to content

Commit

Permalink
OpenTelemetry Logs and Access Logs
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Pollet <[email protected]>
  • Loading branch information
rtribotte and kevinpollet authored Dec 6, 2024
1 parent 33c1d70 commit 826a2b7
Show file tree
Hide file tree
Showing 33 changed files with 2,298 additions and 476 deletions.
21 changes: 18 additions & 3 deletions cmd/traefik/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"io"
stdlog "log"
"os"
Expand All @@ -20,12 +21,13 @@ func init() {
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
}

func setupLogger(staticConfiguration *static.Configuration) {
func setupLogger(staticConfiguration *static.Configuration) error {
// configure log format
w := getLogWriter(staticConfiguration)

// configure log level
logLevel := getLogLevel(staticConfiguration)
zerolog.SetGlobalLevel(logLevel)

// create logger
logCtx := zerolog.New(w).With().Timestamp()
Expand All @@ -34,20 +36,33 @@ func setupLogger(staticConfiguration *static.Configuration) {
}

log.Logger = logCtx.Logger().Level(logLevel)

if staticConfiguration.Log != nil && staticConfiguration.Log.OTLP != nil {
var err error
log.Logger, err = logs.SetupOTelLogger(log.Logger, staticConfiguration.Log.OTLP)
if err != nil {
return fmt.Errorf("setting up OpenTelemetry logger: %w", err)
}
}

zerolog.DefaultContextLogger = &log.Logger
zerolog.SetGlobalLevel(logLevel)

// Global logrus replacement (related to lib like go-rancher-metadata, docker, etc.)
logrus.StandardLogger().Out = logs.NoLevel(log.Logger, zerolog.DebugLevel)

// configure default standard log.
stdlog.SetFlags(stdlog.Lshortfile | stdlog.LstdFlags)
stdlog.SetOutput(logs.NoLevel(log.Logger, zerolog.DebugLevel))

return nil
}

func getLogWriter(staticConfiguration *static.Configuration) io.Writer {
var w io.Writer = os.Stdout
if staticConfiguration.Log != nil && staticConfiguration.Log.OTLP != nil {
return io.Discard
}

var w io.Writer = os.Stdout
if staticConfiguration.Log != nil && len(staticConfiguration.Log.FilePath) > 0 {
_, _ = os.OpenFile(staticConfiguration.Log.FilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666)
w = &lumberjack.Logger{
Expand Down
4 changes: 3 additions & 1 deletion cmd/traefik/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ Complete documentation is available at https://traefik.io`,
}

func runCmd(staticConfiguration *static.Configuration) error {
setupLogger(staticConfiguration)
if err := setupLogger(staticConfiguration); err != nil {
return fmt.Errorf("setting up logger: %w", err)
}

http.DefaultTransport.(*http.Transport).Proxy = http.ProxyFromEnvironment

Expand Down
6 changes: 6 additions & 0 deletions docs/content/migration/v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,9 @@ Please refer to the Forwarded headers [documentation](../routing/entrypoints.md#

In `v3.3`, the `acme.dnsChallenge.delaybeforecheck` and `acme.dnsChallenge.disablepropagationcheck` options of the ACME certificate resolver are deprecated,
please use respectively `acme.dnsChallenge.propagation.delayBeforeCheck` and `acme.dnsChallenge.propagation.disableAllChecks` options instead.

### Tracing Global Attributes

In `v3.3`, the `tracing.globalAttributes` option has been deprecated, please use the `tracing.resourceAttributes` option instead.
The `tracing.globalAttributes` option is misleading as its name does not reflect the operation of adding resource attributes to be sent to the collector,
and will be removed in the next major version.
Loading

0 comments on commit 826a2b7

Please sign in to comment.