github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/opentracing_log_encoder.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2021 3 4 package instana 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "strconv" 10 11 otlog "github.com/opentracing/opentracing-go/log" 12 ) 13 14 type stringWriter interface { 15 WriteString(string) (int, error) 16 } 17 18 type otLogEncoder struct { 19 buf stringWriter 20 } 21 22 func newOpenTracingLogEncoder(buf stringWriter) *otLogEncoder { 23 return &otLogEncoder{ 24 buf: buf, 25 } 26 } 27 28 // EmitString writes an opentracing/log.LogField containing string value to the buffer 29 func (enc *otLogEncoder) EmitString(key, value string) { 30 enc.writeField(key, strconv.Quote(value)) 31 } 32 33 // EmitBool writes an opentracing/log.LogField containing bool value to the buffer 34 func (enc *otLogEncoder) EmitBool(key string, value bool) { 35 enc.writeField(key, strconv.FormatBool(value)) 36 } 37 38 // EmitInt writes an opentracing/log.LogField containing int value to the buffer 39 func (enc *otLogEncoder) EmitInt(key string, value int) { 40 enc.writeField(key, strconv.FormatInt(int64(value), 10)) 41 } 42 43 // EmitInt32 writes an opentracing/log.LogField containing int32 value to the buffer 44 func (enc *otLogEncoder) EmitInt32(key string, value int32) { 45 enc.writeField(key, strconv.FormatInt(int64(value), 10)) 46 } 47 48 // EmitInt64 writes an opentracing/log.LogField containing int64 value to the buffer 49 func (enc *otLogEncoder) EmitInt64(key string, value int64) { 50 enc.writeField(key, strconv.FormatInt(value, 10)) 51 } 52 53 // EmitUint32 writes an opentracing/log.LogField containing uint32 value to the buffer 54 func (enc *otLogEncoder) EmitUint32(key string, value uint32) { 55 enc.writeField(key, strconv.FormatUint(uint64(value), 10)) 56 } 57 58 // EmitUint64 writes an opentracing/log.LogField containing uint64 value to the buffer 59 func (enc *otLogEncoder) EmitUint64(key string, value uint64) { 60 enc.writeField(key, strconv.FormatUint(value, 10)) 61 } 62 63 // EmitFloat32 writes an opentracing/log.LogField containing float32 value to the buffer 64 func (enc *otLogEncoder) EmitFloat32(key string, value float32) { 65 enc.writeField(key, strconv.FormatFloat(float64(value), 'g', -1, 32)) 66 } 67 68 // EmitFloat64 writes an opentracing/log.LogField containing float64 value to the buffer 69 func (enc *otLogEncoder) EmitFloat64(key string, value float64) { 70 enc.writeField(key, strconv.FormatFloat(float64(value), 'g', -1, 64)) 71 } 72 73 // EmitObject writes the JSON representation of an object value of opentracing/log.LogField to the buffer. 74 // In case json.Marshal() returns an error the object representation is replaced by the error message. 75 func (enc *otLogEncoder) EmitObject(key string, value interface{}) { 76 data, err := json.Marshal(value) 77 if err != nil { 78 enc.writeField(key, fmt.Sprintf("<JSON marshaling failed: %s>", err)) 79 return 80 } 81 82 enc.writeField(key, string(data)) 83 } 84 85 // EmitLazyLogger delegates value writing to the LazyLogger 86 func (enc *otLogEncoder) EmitLazyLogger(value otlog.LazyLogger) { 87 value(enc) 88 } 89 90 func (enc *otLogEncoder) writeField(key, value string) { 91 enc.buf.WriteString(key) 92 enc.buf.WriteString(": ") 93 enc.buf.WriteString(value) 94 }