Skip to content

Make stackdriver compatible with ops agent #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions sloggers/slogjson/slogjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<traceid>",
// "span": "<spanid>",
// "fields": {
// "my_field": "field value"
// }
// }
// {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this gofmt change has been driving us crazy at @terrastruct

We refuse to use go 1.19 for now because of it. Ruined all our ascii diagrams lol

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, I was quite confused by this.

// "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": "<traceid>",
// "span": "<spanid>",
// "fields": {
// "my_field": "field value"
// }
// }
package slogjson // import "cdr.dev/slog/sloggers/slogjson"

import (
Expand Down
9 changes: 7 additions & 2 deletions sloggers/slogstackdriver/slogstackdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions sloggers/slogstackdriver/slogstackdriver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down