From 086aadffe9cae94a64204fd43825c670b252c650 Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Tue, 29 Nov 2022 20:37:27 -0600 Subject: [PATCH 1/2] Make stackdriver compatible with ops agent --- sloggers/slogstackdriver/slogstackdriver.go | 9 +++++++-- sloggers/slogstackdriver/slogstackdriver_test.go | 5 +++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/sloggers/slogstackdriver/slogstackdriver.go b/sloggers/slogstackdriver/slogstackdriver.go index b49a97e..07c0089 100644 --- a/sloggers/slogstackdriver/slogstackdriver.go +++ b/sloggers/slogstackdriver/slogstackdriver.go @@ -36,11 +36,16 @@ type stackdriverSink struct { } func (s stackdriverSink) LogEntry(ctx context.Context, ent slog.SinkEntry) { + // Note that these documents are inconsistent, so we only use the special + // keys described by both. // https://cloud.google.com/logging/docs/agent/configuration#special-fields + // https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/configuration#special-fields e := slog.M( - slog.F("severity", sev(ent.Level)), + slog.F("logging.googleapis.com/severity", sev(ent.Level)), slog.F("message", ent.Message), - slog.F("timestamp", ent.Time), + // Unfortunately, both of these fields are required. + slog.F("timestampSeconds", ent.Time.Unix()), + slog.F("timestampNanos", ent.Time.UnixNano()%1e9), slog.F("logging.googleapis.com/sourceLocation", &logpb.LogEntrySourceLocation{ File: ent.File, Line: int64(ent.Line), diff --git a/sloggers/slogstackdriver/slogstackdriver_test.go b/sloggers/slogstackdriver/slogstackdriver_test.go index 2ef8e81..4f7f940 100644 --- a/sloggers/slogstackdriver/slogstackdriver_test.go +++ b/sloggers/slogstackdriver/slogstackdriver_test.go @@ -31,8 +31,9 @@ func TestStackdriver(t *testing.T) { projectID, _ := metadata.ProjectID() - j := entryjson.Filter(b.String(), "timestamp") - exp := fmt.Sprintf(`{"severity":"ERROR","message":"line1\n\nline2","logging.googleapis.com/sourceLocation":{"file":"%v","line":30,"function":"cdr.dev/slog/sloggers/slogstackdriver_test.TestStackdriver"},"logging.googleapis.com/operation":{"producer":"meow"},"logging.googleapis.com/trace":"projects/%v/traces/%v","logging.googleapis.com/spanId":"%v","logging.googleapis.com/trace_sampled":false,"wowow":"me\nyou"} + j := entryjson.Filter(b.String(), "timestampSeconds") + j = entryjson.Filter(j, "timestampNanos") + exp := fmt.Sprintf(`{"logging.googleapis.com/severity":"ERROR","message":"line1\n\nline2","logging.googleapis.com/sourceLocation":{"file":"%v","line":30,"function":"cdr.dev/slog/sloggers/slogstackdriver_test.TestStackdriver"},"logging.googleapis.com/operation":{"producer":"meow"},"logging.googleapis.com/trace":"projects/%v/traces/%v","logging.googleapis.com/spanId":"%v","logging.googleapis.com/trace_sampled":false,"wowow":"me\nyou"} `, slogstackdriverTestFile, projectID, s.SpanContext().TraceID, s.SpanContext().SpanID) assert.Equal(t, "entry", exp, j) } From 540ff92b982d7aa8478e31d3e1f0e2ed778a64a6 Mon Sep 17 00:00:00 2001 From: Ammar Bandukwala Date: Tue, 29 Nov 2022 20:37:27 -0600 Subject: [PATCH 2/2] Make stackdriver compatible with ops agent --- sloggers/slogjson/slogjson.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/sloggers/slogjson/slogjson.go b/sloggers/slogjson/slogjson.go index 8340767..5a5d4ae 100644 --- a/sloggers/slogjson/slogjson.go +++ b/sloggers/slogjson/slogjson.go @@ -2,19 +2,19 @@ // // Format // -// { -// "ts": "2019-09-10T20:19:07.159852-05:00", -// "level": "INFO", -// "logger_names": ["comp", "subcomp"], -// "msg": "hi", -// "caller": "slog/examples_test.go:62", -// "func": "cdr.dev/slog/sloggers/slogtest_test.TestExampleTest", -// "trace": "", -// "span": "", -// "fields": { -// "my_field": "field value" -// } -// } +// { +// "ts": "2019-09-10T20:19:07.159852-05:00", +// "level": "INFO", +// "logger_names": ["comp", "subcomp"], +// "msg": "hi", +// "caller": "slog/examples_test.go:62", +// "func": "cdr.dev/slog/sloggers/slogtest_test.TestExampleTest", +// "trace": "", +// "span": "", +// "fields": { +// "my_field": "field value" +// } +// } package slogjson // import "cdr.dev/slog/sloggers/slogjson" import (