Skip to content

Commit

Permalink
Zap Field Attributes Support
Browse files Browse the repository at this point in the history
  • Loading branch information
mirackara committed Apr 18, 2024
1 parent ef701fd commit 15fac4b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
12 changes: 10 additions & 2 deletions v3/integrations/logcontext-v2/nrzap/example/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"os"
"time"

Expand Down Expand Up @@ -29,7 +30,7 @@ func main() {
}

backgroundLogger := zap.New(backgroundCore)
backgroundLogger.Info("this is a background log message")
backgroundLogger.Info("this is a background log message with fields test", zap.Any("foo", 3.14))

txn := app.StartTransaction("nrzap example transaction")
txnCore, err := nrzap.WrapTransactionCore(core, txn)
Expand All @@ -38,7 +39,14 @@ func main() {
}

txnLogger := zap.New(txnCore)
txnLogger.Info("this is a transaction log message")
txnLogger.Info("this is a transaction log message",
zap.String("region", "nr-east"),
zap.Int("int", 123),
zap.Duration("duration", 1*time.Second),
)

err = errors.New("this is an error")
txnLogger.Error("this is an error log message", zap.Error(err))

txn.End()

Expand Down
44 changes: 41 additions & 3 deletions v3/integrations/logcontext-v2/nrzap/nrzap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package nrzap

import (
"errors"
"fmt"
"math"
"time"

"github.com/newrelic/go-agent/v3/internal"
"github.com/newrelic/go-agent/v3/newrelic"
Expand All @@ -24,12 +27,47 @@ type newrelicApplicationState struct {
txn *newrelic.Transaction
}

func convertField(fields []zap.Field) map[string]interface{} {
attributes := make(map[string]interface{})
for _, field := range fields {
switch field.Type {

case zapcore.BoolType:
attributes[field.Key] = field.Integer == 1
case zapcore.Float32Type:
attributes[field.Key] = math.Float32frombits(uint32(field.Integer))
case zapcore.Float64Type:
attributes[field.Key] = math.Float64frombits(uint64(field.Integer))
case zapcore.Int64Type, zapcore.Int32Type, zapcore.Int16Type, zapcore.Int8Type:
attributes[field.Key] = field.Integer
case zapcore.StringType:
attributes[field.Key] = field.String
case zapcore.Uint64Type, zapcore.Uint32Type, zapcore.Uint16Type, zapcore.Uint8Type:
attributes[field.Key] = uint64(field.Integer)
case zapcore.DurationType:
attributes[field.Key] = time.Duration(field.Integer)
case zapcore.TimeType:
attributes[field.Key] = time.Unix(0, field.Integer)
case zapcore.TimeFullType:
attributes[field.Key] = time.Unix(0, field.Integer).UTC()
case zapcore.ErrorType:
attributes[field.Key] = field.Interface.(error).Error()
case zapcore.BinaryType:
attributes[field.Key] = field.Interface
default:
attributes[field.Key] = fmt.Sprintf("%v", field.Interface)
}
}
return attributes
}

// internal handler function to manage writing a log to the new relic application
func (nr *newrelicApplicationState) recordLog(entry zapcore.Entry, fields []zap.Field) {
data := newrelic.LogData{
Timestamp: entry.Time.UnixMilli(),
Severity: entry.Level.String(),
Message: entry.Message,
Timestamp: entry.Time.UnixMilli(),
Severity: entry.Level.String(),
Message: entry.Message,
Attributes: convertField(fields),
}

if nr.txn != nil {
Expand Down

0 comments on commit 15fac4b

Please sign in to comment.