Skip to content

Commit

Permalink
[receiver/cloudfoundryreceiver] Remove tracing support on logs (it wi…
Browse files Browse the repository at this point in the history
…ll be a new PR)

Co-authored-by: Sam Clulow <sam.clulow@springernature.com>
  • Loading branch information
jriguera and m1rp committed May 31, 2024
1 parent e8c9ec3 commit 30932f9
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 373 deletions.
144 changes: 0 additions & 144 deletions receiver/cloudfoundryreceiver/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
package cloudfoundryreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/cloudfoundryreceiver"

import (
"encoding/hex"
"fmt"
"strings"
"time"
"unicode"

"code.cloudfoundry.org/go-loggregator/rpc/loggregator_v2"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -65,74 +61,9 @@ func convertEnvelopeToLogs(envelope *loggregator_v2.Envelope, logSlice plog.LogR
log.SetSeverityNumber(plog.SeverityNumberError)
}
copyEnvelopeAttributes(log.Attributes(), envelope)
if t, found := envelope.Tags[envelopeSourceTypeKey]; found && t == envelopeSourceTypeValueRTR {
traceID, spanID, flags, err := getTracingIDs(logLine)
if err != nil {
return err
}
if !pcommon.TraceID(traceID).IsEmpty() {
log.SetTraceID(traceID)
log.SetSpanID(spanID)
log.SetFlags(plog.LogRecordFlags(flags))
}
}
return nil
}

func getTracingIDs(logLine string) (traceID [16]byte, spanID [8]byte, flags byte, err error) {
var trace []byte
var span []byte
var hexFlags []byte
_, wordsMap := parseLogLine(logLine)
traceIDStr, foundW3C := wordsMap[logLineRTRW3CKey]
if foundW3C {
// Use W3C headers
traceW3C := strings.Split(traceIDStr, "-")
if len(traceW3C) != 4 || traceW3C[0] != traceW3CVersion {
err = fmt.Errorf(
"traceId W3C key %s with format %s not valid in log",
logLineRTRW3CKey, traceW3C[0])
return
}
trace = []byte(traceW3C[1])
span = []byte(traceW3C[2])
hexFlags = []byte(traceW3C[3])
} else {
// try Zipkin headers
traceIDStr, foundZk := wordsMap[logLineRTRZipkinKey]
if !foundZk {
// log line has no tracing headers
return
}
traceZk := strings.Split(traceIDStr, "-")
if len(traceZk) != 2 {
err = fmt.Errorf(
"traceId Zipkin key %s not valid in log",
logLineRTRZipkinKey)
return
}
trace = []byte(traceZk[0])
span = []byte(traceZk[1])
hexFlags = []byte("00")
}
traceDecoded := make([]byte, 16)
spanDecoded := make([]byte, 8)
flagsDecoded := make([]byte, 1)
if _, err = hex.Decode(traceDecoded, trace); err != nil {
return
}
if _, err = hex.Decode(spanDecoded, span); err != nil {
return
}
if _, err = hex.Decode(flagsDecoded, hexFlags); err != nil {
return
}
copy(traceID[:], traceDecoded)
copy(spanID[:], spanDecoded)
flags = flagsDecoded[0]
return
}

func copyEnvelopeAttributes(attributes pcommon.Map, envelope *loggregator_v2.Envelope) {
for key, value := range envelope.Tags {
attributes.PutStr(attributeNamePrefix+key, value)
Expand All @@ -144,78 +75,3 @@ func copyEnvelopeAttributes(attributes pcommon.Map, envelope *loggregator_v2.Env
attributes.PutStr(attributeNamePrefix+"instance_id", envelope.InstanceId)
}
}

func parseLogLine(s string) ([]string, map[string]string) {
wordList := make([]string, 0, 20)
sb := &strings.Builder{}
mapValue := &strings.Builder{}
timestamp := &strings.Builder{}
isTimeStamp := false
mapKey := ""
isMap := false
isQuoted := false
wordMap := make(map[string]string)
for _, ch := range s {
if ch == '"' {
isQuoted = !isQuoted
sb.WriteRune(ch)
continue
}
if isQuoted {
sb.WriteRune(ch)
if isMap {
mapValue.WriteRune(ch)
}
continue
}
if ch == '[' && sb.Len() == 0 {
// first char after space
isTimeStamp = true
continue
}
if ch == ']' && isTimeStamp {
wordList = append(wordList, timestamp.String())
timestamp.Reset()
isTimeStamp = false
continue
}
if isTimeStamp {
timestamp.WriteRune(ch)
continue
}
if unicode.IsSpace(ch) {
if sb.Len() > 0 {
word := sb.String()
if isMap {
wordMap[mapKey] = mapValue.String()
} else if strings.HasPrefix(word, `"`) && strings.HasSuffix(word, `"`) {
// remove " if the item is not a keyMap and starts and ends with it
word = strings.Trim(word, `"`)
}
wordList = append(wordList, word)
}
isMap = false
mapValue.Reset()
sb.Reset()
continue
}
if isMap {
mapValue.WriteRune(ch)
} else if ch == ':' {
mapKey = sb.String()
isMap = true
}
sb.WriteRune(ch)
}
if sb.Len() > 0 {
word := sb.String()
if isMap {
wordMap[mapKey] = mapValue.String()
} else if strings.HasPrefix(word, `"`) && strings.HasSuffix(word, `"`) {
// remove " if the item is not a keyMap and starts and ends with it
word = strings.Trim(word, `"`)
}
wordList = append(wordList, word)
}
return wordList, wordMap
}
Loading

0 comments on commit 30932f9

Please sign in to comment.