Skip to content

Commit

Permalink
Explicitly call hex.EncodeAsString for trace/span ID when clear (#16247)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Nov 11, 2022
1 parent cdaed74 commit e21926d
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
5 changes: 3 additions & 2 deletions exporter/awscloudwatchlogsexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package awscloudwatchlogsexporter // import "github.com/open-telemetry/opentelem

import (
"context"
"encoding/hex"
"encoding/json"
"errors"
"time"
Expand Down Expand Up @@ -192,10 +193,10 @@ func logToCWLog(resourceAttrs map[string]interface{}, log plog.LogRecord) (*clou
Flags: uint32(log.Flags()),
}
if traceID := log.TraceID(); !traceID.IsEmpty() {
body.TraceID = traceID.HexString()
body.TraceID = hex.EncodeToString(traceID[:])
}
if spanID := log.SpanID(); !spanID.IsEmpty() {
body.SpanID = spanID.HexString()
body.SpanID = hex.EncodeToString(spanID[:])
}
body.Attributes = attrsValue(log.Attributes())
body.Resource = resourceAttrs
Expand Down
11 changes: 7 additions & 4 deletions exporter/lokiexporter/legacy_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bufio"
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -337,14 +338,16 @@ func (l *lokiExporter) convertLogBodyToEntry(lr plog.LogRecord, res pcommon.Reso
b.WriteString(strconv.Itoa(int(lr.SeverityNumber())))
b.WriteRune(' ')
}
if _, ok := l.config.Labels.RecordAttributes["traceID"]; !ok && !lr.TraceID().IsEmpty() {
traceID := lr.TraceID()
if _, ok := l.config.Labels.RecordAttributes["traceID"]; !ok && !traceID.IsEmpty() {
b.WriteString("traceID=")
b.WriteString(lr.TraceID().HexString())
b.WriteString(hex.EncodeToString(traceID[:]))
b.WriteRune(' ')
}
if _, ok := l.config.Labels.RecordAttributes["spanID"]; !ok && !lr.SpanID().IsEmpty() {
spanID := lr.SpanID()
if _, ok := l.config.Labels.RecordAttributes["spanID"]; !ok && !spanID.IsEmpty() {
b.WriteString("spanID=")
b.WriteString(lr.SpanID().HexString())
b.WriteString(hex.EncodeToString(spanID[:]))
b.WriteRune(' ')
}

Expand Down
11 changes: 5 additions & 6 deletions exporter/mezmoexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package mezmoexporter // import "github.com/open-telemetry/opentelemetry-collect

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -102,14 +103,12 @@ func (m *mezmoExporter) logDataToMezmo(ld plog.Logs) error {
attrs["hostname"] = resourceHostName.AsString()
}

traceID := log.TraceID().HexString()
if traceID != "" {
attrs["trace.id"] = traceID
if traceID := log.TraceID(); !traceID.IsEmpty() {
attrs["trace.id"] = hex.EncodeToString(traceID[:])
}

spanID := log.SpanID().HexString()
if spanID != "" {
attrs["span.id"] = spanID
if spanID := log.SpanID(); !spanID.IsEmpty() {
attrs["span.id"] = hex.EncodeToString(spanID[:])
}

log.Attributes().Range(func(k string, v pcommon.Value) bool {
Expand Down
9 changes: 5 additions & 4 deletions exporter/skywalkingexporter/logrecord_to_logdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package skywalkingexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/skywalkingexporter"

import (
"encoding/hex"
"strconv"
"time"

Expand Down Expand Up @@ -141,14 +142,14 @@ func mapLogRecordToLogData(lr plog.LogRecord, logData *logpb.LogData) {
})
}

if traceID := lr.TraceID().HexString(); traceID != "" {
logData.TraceContext = &logpb.TraceContext{TraceId: traceID}
if traceID := lr.TraceID(); !traceID.IsEmpty() {
logData.TraceContext = &logpb.TraceContext{TraceId: hex.EncodeToString(traceID[:])}
}

if spanID := lr.SpanID().HexString(); spanID != "" {
if spanID := lr.SpanID(); !spanID.IsEmpty() {
logData.Tags.Data = append(logData.Tags.Data, &common.KeyStringValuePair{
Key: spanIDField,
Value: spanID,
Value: hex.EncodeToString(spanID[:]),
})
}
}
11 changes: 5 additions & 6 deletions pkg/translator/loki/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package loki // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/loki"

import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -70,14 +71,12 @@ func Encode(lr plog.LogRecord, res pcommon.Resource) (string, error) {
func EncodeLogfmt(lr plog.LogRecord, res pcommon.Resource) (string, error) {
keyvals := bodyToKeyvals(lr.Body())

traceID := lr.TraceID().HexString()
if traceID != "" {
keyvals = keyvalsReplaceOrAppend(keyvals, "traceID", traceID)
if traceID := lr.TraceID(); !traceID.IsEmpty() {
keyvals = keyvalsReplaceOrAppend(keyvals, "traceID", hex.EncodeToString(traceID[:]))
}

spanID := lr.SpanID().HexString()
if spanID != "" {
keyvals = keyvalsReplaceOrAppend(keyvals, "spanID", spanID)
if spanID := lr.SpanID(); !spanID.IsEmpty() {
keyvals = keyvalsReplaceOrAppend(keyvals, "spanID", hex.EncodeToString(spanID[:]))
}

severity := lr.SeverityText()
Expand Down
9 changes: 5 additions & 4 deletions pkg/translator/prometheusremotewrite/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package prometheusremotewrite // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite"

import (
"encoding/hex"
"fmt"
"log"
"math"
Expand Down Expand Up @@ -364,17 +365,17 @@ func getPromExemplars(pt pmetric.HistogramDataPoint) []prompb.Exemplar {
Value: exemplar.DoubleValue(),
Timestamp: timestamp.FromTime(exemplar.Timestamp().AsTime()),
}
if !exemplar.TraceID().IsEmpty() {
val := exemplar.TraceID().HexString()
if traceID := exemplar.TraceID(); !traceID.IsEmpty() {
val := hex.EncodeToString(traceID[:])
exemplarRunes += utf8.RuneCountInString(traceIDKey) + utf8.RuneCountInString(val)
promLabel := prompb.Label{
Name: traceIDKey,
Value: val,
}
promExemplar.Labels = append(promExemplar.Labels, promLabel)
}
if !exemplar.SpanID().IsEmpty() {
val := exemplar.SpanID().HexString()
if spanID := exemplar.SpanID(); !spanID.IsEmpty() {
val := hex.EncodeToString(spanID[:])
exemplarRunes += utf8.RuneCountInString(spanIDKey) + utf8.RuneCountInString(val)
promLabel := prompb.Label{
Name: spanIDKey,
Expand Down

0 comments on commit e21926d

Please sign in to comment.