Skip to content

Commit 7c313ab

Browse files
authored
Make stackdriver compatible with ops agent (#157)
1 parent 18dce5c commit 7c313ab

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

sloggers/slogjson/slogjson.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
//
33
// Format
44
//
5-
// {
6-
// "ts": "2019-09-10T20:19:07.159852-05:00",
7-
// "level": "INFO",
8-
// "logger_names": ["comp", "subcomp"],
9-
// "msg": "hi",
10-
// "caller": "slog/examples_test.go:62",
11-
// "func": "cdr.dev/slog/sloggers/slogtest_test.TestExampleTest",
12-
// "trace": "<traceid>",
13-
// "span": "<spanid>",
14-
// "fields": {
15-
// "my_field": "field value"
16-
// }
17-
// }
5+
// {
6+
// "ts": "2019-09-10T20:19:07.159852-05:00",
7+
// "level": "INFO",
8+
// "logger_names": ["comp", "subcomp"],
9+
// "msg": "hi",
10+
// "caller": "slog/examples_test.go:62",
11+
// "func": "cdr.dev/slog/sloggers/slogtest_test.TestExampleTest",
12+
// "trace": "<traceid>",
13+
// "span": "<spanid>",
14+
// "fields": {
15+
// "my_field": "field value"
16+
// }
17+
// }
1818
package slogjson // import "cdr.dev/slog/sloggers/slogjson"
1919

2020
import (

sloggers/slogstackdriver/slogstackdriver.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,16 @@ type stackdriverSink struct {
3636
}
3737

3838
func (s stackdriverSink) LogEntry(ctx context.Context, ent slog.SinkEntry) {
39+
// Note that these documents are inconsistent, so we only use the special
40+
// keys described by both.
3941
// https://cloud.google.com/logging/docs/agent/configuration#special-fields
42+
// https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/configuration#special-fields
4043
e := slog.M(
41-
slog.F("severity", sev(ent.Level)),
44+
slog.F("logging.googleapis.com/severity", sev(ent.Level)),
4245
slog.F("message", ent.Message),
43-
slog.F("timestamp", ent.Time),
46+
// Unfortunately, both of these fields are required.
47+
slog.F("timestampSeconds", ent.Time.Unix()),
48+
slog.F("timestampNanos", ent.Time.UnixNano()%1e9),
4449
slog.F("logging.googleapis.com/sourceLocation", &logpb.LogEntrySourceLocation{
4550
File: ent.File,
4651
Line: int64(ent.Line),

sloggers/slogstackdriver/slogstackdriver_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func TestStackdriver(t *testing.T) {
3131

3232
projectID, _ := metadata.ProjectID()
3333

34-
j := entryjson.Filter(b.String(), "timestamp")
35-
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"}
34+
j := entryjson.Filter(b.String(), "timestampSeconds")
35+
j = entryjson.Filter(j, "timestampNanos")
36+
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"}
3637
`, slogstackdriverTestFile, projectID, s.SpanContext().TraceID, s.SpanContext().SpanID)
3738
assert.Equal(t, "entry", exp, j)
3839
}

0 commit comments

Comments
 (0)