Skip to content

Commit

Permalink
support OpenTelemetry's semantic conventions for time, msg, level
Browse files Browse the repository at this point in the history
  • Loading branch information
aybabtme authored and Antoine Grondin committed Jan 25, 2023
1 parent 5c2bb4d commit 9c0287a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
13 changes: 8 additions & 5 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 3 additions & 12 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand All @@ -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),
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions test/cases/00005-otel/config.json
Original file line number Diff line number Diff line change
@@ -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
}

7 changes: 7 additions & 0 deletions test/cases/00005-otel/input
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/cases/00005-otel/want
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9c0287a

Please sign in to comment.