Skip to content

Commit

Permalink
consolidate code for attribute JSON creation
Browse files Browse the repository at this point in the history
  • Loading branch information
iamemilio committed Apr 17, 2024
1 parent 8659a49 commit ef701fd
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 45 deletions.
44 changes: 0 additions & 44 deletions v3/newrelic/attributes_from_internal.go
Expand Up @@ -452,50 +452,6 @@ func addUserAttribute(a *attributes, key string, val interface{}, d destinationS
}

func writeAttributeValueJSON(w *jsonFieldsWriter, key string, val interface{}) {
switch v := val.(type) {
case string:
w.stringField(key, v)
case bool:
if v {
w.rawField(key, `true`)
} else {
w.rawField(key, `false`)
}
case uint8:
w.intField(key, int64(v))
case uint16:
w.intField(key, int64(v))
case uint32:
w.intField(key, int64(v))
case uint64:
w.intField(key, int64(v))
case uint:
w.intField(key, int64(v))
case uintptr:
w.intField(key, int64(v))
case int8:
w.intField(key, int64(v))
case int16:
w.intField(key, int64(v))
case int32:
w.intField(key, int64(v))
case int64:
w.intField(key, v)
case int:
w.intField(key, int64(v))
case float32:
w.floatField(key, float64(v))
case float64:
w.floatField(key, v)
default:
w.stringField(key, fmt.Sprintf("%T", v))
}
}

// This is capable of consuming maps and structs, but this is expensive.
// If possible, pass them already stringified.
// note that other than the additional support for complex structs, this is a 1:1 clone of writeAgentAttributeValues()
func writeLogAttributeJSON(w *jsonFieldsWriter, key string, val any) {
switch v := val.(type) {
case string:
w.stringField(key, v)
Expand Down
2 changes: 1 addition & 1 deletion v3/newrelic/log_event.go
Expand Up @@ -61,7 +61,7 @@ func (e *logEvent) WriteJSON(buf *bytes.Buffer) {
buf.WriteString(`,"attributes":{`)
w := jsonFieldsWriter{buf: buf}
for key, val := range e.atributes {
writeLogAttributeJSON(&w, key, val)
writeAttributeValueJSON(&w, key, val)
}
buf.WriteByte('}')
}
Expand Down

0 comments on commit ef701fd

Please sign in to comment.