From 9c0287aaff3dfc2b769c480d919092a9f54efb44 Mon Sep 17 00:00:00 2001 From: Antoine Grondin Date: Wed, 25 Jan 2023 15:24:29 +0900 Subject: [PATCH] support OpenTelemetry's semantic conventions for time, msg, level --- handler.go | 13 ++++++++----- internal/pkg/config/config.go | 15 +++------------ test/cases/00005-otel/config.json | 11 +++++++++++ test/cases/00005-otel/input | 7 +++++++ test/cases/00005-otel/want | 7 +++++++ 5 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 test/cases/00005-otel/config.json create mode 100644 test/cases/00005-otel/input create mode 100644 test/cases/00005-otel/want diff --git a/handler.go b/handler.go index 62010bfc..7e0cca54 100644 --- a/handler.go +++ b/handler.go @@ -12,11 +12,14 @@ type Handler interface { logfmt.Handler } -var DefaultOptions = &HandlerOptions{ - TimeFields: []string{"time", "ts", "@timestamp", "timestamp"}, - MessageFields: []string{"message", "msg"}, - LevelFields: []string{"level", "lvl", "loglevel", "severity"}, -} +var DefaultOptions = func() *HandlerOptions { + opts := &HandlerOptions{ + TimeFields: []string{"time", "ts", "@timestamp", "timestamp", "Timestamp"}, + MessageFields: []string{"message", "msg", "Body"}, + LevelFields: []string{"level", "lvl", "loglevel", "severity", "SeverityLevel"}, + } + return opts +}() type HandlerOptions struct { TimeFields []string diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index fbf16597..909bf3bd 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -15,9 +14,9 @@ var DefaultConfig = Config{ Version: 1, Skip: ptr([]string{}), Keep: ptr([]string{}), - TimeFields: ptr([]string{"time", "ts", "@timestamp", "timestamp"}), - MessageFields: ptr([]string{"message", "msg"}), - LevelFields: ptr([]string{"level", "lvl", "loglevel", "severity"}), + TimeFields: ptr([]string{"time", "ts", "@timestamp", "timestamp", "Timestamp"}), + MessageFields: ptr([]string{"message", "msg", "Body"}), + LevelFields: ptr([]string{"level", "lvl", "loglevel", "severity", "SeverityText"}), SortLongest: ptr(true), SkipUnchanged: ptr(true), Truncates: ptr(true), @@ -66,14 +65,6 @@ func ReadConfigFile(path string, dflt *Config) (*Config, error) { if !errors.Is(err, os.ErrNotExist) { return nil, fmt.Errorf("opening config file %q: %v", path, err) } - - cfgContent, err := json.MarshalIndent(dflt, "", "\t") - if err != nil { - return nil, fmt.Errorf("marshaling default config file: %v", err) - } - if err := ioutil.WriteFile(path, cfgContent, 0600); err != nil { - return nil, fmt.Errorf("writing default to config file %q: %v", path, err) - } return dflt, nil } defer configFile.Close() diff --git a/test/cases/00005-otel/config.json b/test/cases/00005-otel/config.json new file mode 100644 index 00000000..76145548 --- /dev/null +++ b/test/cases/00005-otel/config.json @@ -0,0 +1,11 @@ +{ + "sort-longest": true, + "skip-unchanged": true, + "truncates": false, + "light-bg": false, + "color-mode": "off", + "truncate-length": 15, + "time-format": "Jan _2 15:04:05", + "palette": null + } + \ No newline at end of file diff --git a/test/cases/00005-otel/input b/test/cases/00005-otel/input new file mode 100644 index 00000000..731f7206 --- /dev/null +++ b/test/cases/00005-otel/input @@ -0,0 +1,7 @@ +Timestamp=2023-01-24T10:37:28.767905777Z SeverityText=INFO Body="starting consumerLoop..." deployment.environment=development sdk.name=sdk-telemetry sdk.version=1.24.0 service.instance.id=cf7r9p8j3ee3onr3dsk0 service.name=unknown_service:acme telemetry.sdk.name=opentelemetry +Timestamp=2023-01-24T10:37:29.3307514Z SeverityText=INFO Body="starting pooling" deployment.environment=development sdk.name=sdk-telemetry sdk.version=1.24.0 service.instance.id=cf7r9p8j3ee3onr3dsk0 service.name=unknown_service:acme telemetry.sdk.name=opentelemetry +Timestamp=2023-01-24T10:37:29.330831204Z SeverityText=INFO Body="looking for unassigned owner resource count" deployment.environment=development sdk.name=sdk-telemetry sdk.version=1.24.0 service.instance.id=cf7r9p8j3ee3onr3dsk0 service.name=unknown_service:acme telemetry.sdk.name=opentelemetry pool_size=0 +Timestamp=2023-01-24T10:37:29.330854306Z SeverityText=INFO Body="looking for unassigned owner resource count" deployment.environment=development sdk.name=sdk-telemetry sdk.version=1.24.0 service.instance.id=cf7r9p8j3ee3onr3dsk0 service.name=unknown_service:acme telemetry.sdk.name=opentelemetry pool_size=0 +Timestamp=2023-01-24T10:37:29.330985113Z SeverityText=INFO Body="server starting" deployment.environment=development sdk.name=sdk-telemetry sdk.version=1.24.0 service.instance.id=cf7r9p8j3ee3onr3dsk0 service.name=unknown_service:acme telemetry.sdk.name=opentelemetry server.addr=[::]:2206 +Timestamp=2023-01-24T10:37:29.332562199Z SeverityText=INFO Body="pool is full, nothing to do" deployment.environment=development sdk.name=sdk-telemetry sdk.version=1.24.0 service.instance.id=cf7r9p8j3ee3onr3dsk0 service.name=unknown_service:acme telemetry.sdk.name=opentelemetry pool_size=0 +Timestamp=2023-01-24T10:37:29.332597001Z SeverityText=INFO Body="pool is full, nothing to do" deployment.environment=development sdk.name=sdk-telemetry sdk.version=1.24.0 service.instance.id=cf7r9p8j3ee3onr3dsk0 service.name=unknown_service:acme telemetry.sdk.name=opentelemetry pool_size=0 \ No newline at end of file diff --git a/test/cases/00005-otel/want b/test/cases/00005-otel/want new file mode 100644 index 00000000..eb4742a4 --- /dev/null +++ b/test/cases/00005-otel/want @@ -0,0 +1,7 @@ +Jan 24 10:37:28 |INFO| starting consumerLoop... sdk.version=1.24.0 sdk.name=sdk-telemetry telemetry.sdk.name=opentelemetry service.name=unknown_service:acme deployment.environment=development service.instance.id=cf7r9p8j3ee3onr3dsk0 +Jan 24 10:37:29 |INFO| starting pooling +Jan 24 10:37:29 |INFO| looking for unassigned owner resource count pool_size=0 +Jan 24 10:37:29 |INFO| looking for unassigned owner resource count +Jan 24 10:37:29 |INFO| server starting server.addr=[::]:2206 +Jan 24 10:37:29 |INFO| pool is full, nothing to do pool_size=0 +Jan 24 10:37:29 |INFO| pool is full, nothing to do