Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Use Datadog reserved Status field, send error messages if no added fields #19

Merged
merged 5 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions pkg/analytics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
)

var datadogLogLevel = "_logLevel"
var datadogStatus = "status"
Copy link
Contributor

Choose a reason for hiding this comment

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

So how does status differ from _logLevel? From the docs, it seems like `status is the correct one to use so why send both?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeh I was confused by this in their docs initially also. So _loglevel allows you to specify whatever level you want and you can filter on it from the DD console. The status one though only allows the pre-defined (info, warn, error) and we can't modify that, but that causes the colorized fields in DD
image

So it's not entirely necessary since we filter on the _loglevel, but it gives you a quicker at a glance view since it will color the actual log entries instead of treating everything as info and blue by default.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we switch our usage of _loglevel to status? I don't think we really care about the levels it doesn't support.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we could, but it doesn't have things like debug/panic which currently i set to the closest relevant so debug -> info and panic -> error. If we're ok with losing granularity I can change that to just use status, but it didn't seem like we needed to consolidate and afaik it doesnt change costs in any way either so i left both.


func NewClient(properties map[string]interface{}) (*Client, error) {
result, err := getTrackingFileContents(analyticsFile)
Expand Down Expand Up @@ -81,16 +82,20 @@ func (t *Client) Debug(event string) {

func (t *Client) Warn(event string) {
t.Properties[datadogLogLevel] = Warn
t.Properties[datadogStatus] = Warn
Copy link
Contributor

Choose a reason for hiding this comment

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

were we always sending the logs, just classifying them as Info? I thought we send the "klotho compiling failed" as info, but also with the error logs and thats what was also missing

t.track(event)
}

func (t *Client) Error(event string) {
t.Properties[datadogLogLevel] = Error
t.Properties[datadogStatus] = Error
t.track(event)
}

func (t *Client) Panic(event string) {
t.Properties[datadogLogLevel] = Panic
// Using error since datadog does not support panic for the reserved status field
t.Properties[datadogStatus] = Error
t.track(event)
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/analytics/fields_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ func (fl *fieldListener) Write(entry zapcore.Entry, fields []zapcore.Field) erro
allFields = append(allFields, fields...)

safeLogsStr := logging.SanitizeFields(allFields, fl.client.Hash)

for _, f := range allFields {
if f.Key == logging.EntryMessageField {
safeLogsStr = entry.Message
}
}

switch entry.Level {
case zapcore.DebugLevel:
fl.client.Debug(safeLogsStr)
Expand Down
6 changes: 3 additions & 3 deletions pkg/cli/klothomain.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ func (km KlothoMain) Main() {
err := root.Execute()
if err != nil {
if cfg.internalDebug {
zap.S().Errorf("%+v", err)
zap.S().With(logging.SendEntryMessage).Errorf("%+v", err)
} else if !root.SilenceErrors {
zap.S().Errorf("%v", err)
zap.S().With(logging.SendEntryMessage).Errorf("%v", err)
}
zap.S().Error("Klotho compilation failed")
zap.S().With(logging.SendEntryMessage).Error("Klotho compilation failed")
os.Exit(1)
}
if hadWarnings.Load() && cfg.strict {
Expand Down
10 changes: 10 additions & 0 deletions pkg/logging/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"go.uber.org/zap/zapcore"
)

var EntryMessageField = "entryMessage"

type fileField struct {
f core.File
}
Expand Down Expand Up @@ -68,6 +70,14 @@ type astNodeField struct {
content string
}

type entryMessage struct{}

func (field entryMessage) MarshalLogObject(enc zapcore.ObjectEncoder) error {
return nil
}

var SendEntryMessage = zap.Object("entryMessage", entryMessage{})

// DescribeKlothoFields is intended for unit testing expected log lines.
//
// This returns a map whose keys are the field keys, and whose values are descriptions of the Klotho-provided zap fields.
Expand Down