Tempo: remove deprecated model package usage (remove replace) (#71687)

* Tempo: remove deprecated model package usage (remove replace)

* fix root test
This commit is contained in:
Charandas
2023-07-18 19:30:29 +03:00
committed by GitHub
parent 4a59022fe8
commit 614f1af190
6 changed files with 55 additions and 43 deletions
@@ -5,13 +5,13 @@ as soon as we can use agent as a dependency this can be refactored
package frontendlogging
import (
"encoding/hex"
"fmt"
"sort"
"strings"
"time"
om "github.com/wk8/go-ordered-map"
"go.opentelemetry.io/collector/model/otlp"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
)
@@ -148,7 +148,7 @@ type Traces struct {
// UnmarshalJSON unmarshals Traces model.
func (t *Traces) UnmarshalJSON(b []byte) error {
unmarshaler := otlp.NewJSONTracesUnmarshaler()
unmarshaler := ptrace.JSONUnmarshaler{}
td, err := unmarshaler.UnmarshalTraces(b)
if err != nil {
return err
@@ -159,7 +159,7 @@ func (t *Traces) UnmarshalJSON(b []byte) error {
// MarshalJSON marshals Traces model to json.
func (t Traces) MarshalJSON() ([]byte, error) {
marshaler := otlp.NewJSONTracesMarshaler()
marshaler := ptrace.JSONMarshaler{}
return marshaler.MarshalTraces(t.Traces)
}
@@ -182,6 +182,15 @@ func (t Traces) SpanSlice() []ptrace.Span {
// SpanToKeyVal returns KeyVal representation of a Span.
func SpanToKeyVal(s ptrace.Span) *KeyVal {
traceID := s.TraceID()
traceIDHex := hex.EncodeToString(traceID[:])
spanID := s.SpanID()
spanIDHex := hex.EncodeToString(spanID[:])
parentSpanID := s.ParentSpanID()
parentSpanIDHex := hex.EncodeToString(parentSpanID[:])
kv := NewKeyVal()
if s.StartTimestamp() > 0 {
KeyValAdd(kv, "timestamp", s.StartTimestamp().AsTime().String())
@@ -190,11 +199,11 @@ func SpanToKeyVal(s ptrace.Span) *KeyVal {
KeyValAdd(kv, "end_timestamp", s.StartTimestamp().AsTime().String())
}
KeyValAdd(kv, "kind", "span")
KeyValAdd(kv, "traceID", s.TraceID().HexString())
KeyValAdd(kv, "spanID", s.SpanID().HexString())
KeyValAdd(kv, "traceID", traceIDHex)
KeyValAdd(kv, "spanID", spanIDHex)
KeyValAdd(kv, "span_kind", s.Kind().String())
KeyValAdd(kv, "name", s.Name())
KeyValAdd(kv, "parent_spanID", s.ParentSpanID().HexString())
KeyValAdd(kv, "parent_spanID", parentSpanIDHex)
s.Attributes().Range(func(k string, v pcommon.Value) bool {
KeyValAdd(kv, "attr_"+k, fmt.Sprintf("%v", v))
return true
+3 -2
View File
@@ -10,7 +10,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/tsdb/tempo/kinds/dataquery"
"go.opentelemetry.io/collector/model/otlp"
"go.opentelemetry.io/collector/pdata/ptrace"
)
func (s *Service) getTrace(ctx context.Context, pCtx backend.PluginContext, query backend.DataQuery) (*backend.DataResponse, error) {
@@ -54,7 +54,8 @@ func (s *Service) getTrace(ctx context.Context, pCtx backend.PluginContext, quer
return result, nil
}
otTrace, err := otlp.NewProtobufTracesUnmarshaler().UnmarshalTraces(body)
pbUnmarshaler := ptrace.ProtoUnmarshaler{}
otTrace, err := pbUnmarshaler.UnmarshalTraces(body)
if err != nil {
return &backend.DataResponse{}, fmt.Errorf("failed to convert tempo response to Otlp: %w", err)
+28 -22
View File
@@ -1,12 +1,12 @@
package tempo
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/grafana/grafana-plugin-sdk-go/data"
"go.opentelemetry.io/collector/model/pdata"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/attribute"
@@ -118,12 +118,16 @@ func resourceSpansToRows(rs ptrace.ResourceSpans) ([][]interface{}, error) {
func spanToSpanRow(span ptrace.Span, libraryTags pcommon.InstrumentationScope, resource pcommon.Resource) ([]interface{}, error) {
// If the id representation changed from hexstring to something else we need to change the transformBase64IDToHexString in the frontend code
traceID := span.TraceID().HexString()
traceID = strings.TrimPrefix(traceID, strings.Repeat("0", 16))
traceID := span.TraceID()
traceIDHex := hex.EncodeToString(traceID[:])
traceIDHex = strings.TrimPrefix(traceIDHex, strings.Repeat("0", 16))
spanID := span.SpanID().HexString()
spanID := span.SpanID()
spanIDHex := hex.EncodeToString(spanID[:])
parentSpanID := span.ParentSpanID()
parentSpanIDHex := hex.EncodeToString(parentSpanID[:])
parentSpanID := span.ParentSpanID().HexString()
startTime := float64(span.StartTimestamp()) / 1_000_000
serviceName, serviceTags := resourceToProcess(resource)
@@ -158,9 +162,9 @@ func spanToSpanRow(span ptrace.Span, libraryTags pcommon.InstrumentationScope, r
// Order matters (look at dataframe order)
return []interface{}{
traceID,
spanID,
parentSpanID,
traceIDHex,
spanIDHex,
parentSpanIDHex,
span.Name(),
serviceName,
getSpanKind(span.Kind()),
@@ -188,7 +192,7 @@ func resourceToProcess(resource pcommon.Resource) (string, []*KeyValue) {
tags := make([]*KeyValue, 0, attrs.Len()-1)
attrs.Range(func(key string, attr pcommon.Value) bool {
if attribute.Key(key) == semconv.ServiceNameKey {
serviceName = attr.StringVal()
serviceName = attr.Str()
}
tags = append(tags, &KeyValue{Key: key, Value: getAttributeVal(attr)})
return true
@@ -199,14 +203,14 @@ func resourceToProcess(resource pcommon.Resource) (string, []*KeyValue) {
func getAttributeVal(attr pcommon.Value) interface{} {
switch attr.Type() {
case pcommon.ValueTypeString:
return attr.StringVal()
case pcommon.ValueTypeStr:
return attr.Str()
case pcommon.ValueTypeInt:
return attr.IntVal()
return attr.Int()
case pcommon.ValueTypeBool:
return attr.BoolVal()
return attr.Bool()
case pcommon.ValueTypeDouble:
return attr.DoubleVal()
return attr.Double()
case pcommon.ValueTypeMap, pcommon.ValueTypeSlice:
return attr.AsString()
default:
@@ -243,8 +247,8 @@ func getSpanKind(spanKind ptrace.SpanKind) string {
return tagStr
}
func getTraceState(traceState ptrace.TraceState) string {
return string(traceState)
func getTraceState(traceState pcommon.TraceState) string {
return traceState.AsRaw()
}
func spanEventsToLogs(events ptrace.SpanEventSlice) []*TraceLog {
@@ -275,7 +279,7 @@ func spanEventsToLogs(events ptrace.SpanEventSlice) []*TraceLog {
return logs
}
func spanLinksToReferences(links pdata.SpanLinkSlice) []*TraceReference {
func spanLinksToReferences(links ptrace.SpanLinkSlice) []*TraceReference {
if links.Len() == 0 {
return nil
}
@@ -284,10 +288,12 @@ func spanLinksToReferences(links pdata.SpanLinkSlice) []*TraceReference {
for i := 0; i < links.Len(); i++ {
link := links.At(i)
traceId := link.TraceID().HexString()
traceId = strings.TrimLeft(traceId, "0")
traceID := link.TraceID()
traceIDHex := hex.EncodeToString(traceID[:])
traceIDHex = strings.TrimLeft(traceIDHex, "0")
spanId := link.SpanID().HexString()
spanID := link.SpanID()
spanIDHex := hex.EncodeToString(spanID[:])
tags := make([]*KeyValue, 0, link.Attributes().Len())
link.Attributes().Range(func(key string, attr pcommon.Value) bool {
@@ -296,8 +302,8 @@ func spanLinksToReferences(links pdata.SpanLinkSlice) []*TraceReference {
})
references = append(references, &TraceReference{
TraceID: traceId,
SpanID: spanId,
TraceID: traceIDHex,
SpanID: spanIDHex,
Tags: tags,
})
}
+7 -6
View File
@@ -10,7 +10,6 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/model/otlp"
)
func TestTraceToFrame(t *testing.T) {
@@ -23,7 +22,8 @@ func TestTraceToFrame(t *testing.T) {
proto, err := os.ReadFile("testData/tempo_proto_response")
require.NoError(t, err)
otTrace, err := otlp.NewProtobufTracesUnmarshaler().UnmarshalTraces(proto)
pbUnmarshaler := ptrace.ProtoUnmarshaler{}
otTrace, err := pbUnmarshaler.UnmarshalTraces(proto)
require.NoError(t, err)
frame, err := TraceToFrame(otTrace)
@@ -58,7 +58,8 @@ func TestTraceToFrame(t *testing.T) {
proto, err := os.ReadFile("testData/tempo_proto_response")
require.NoError(t, err)
otTrace, err := otlp.NewProtobufTracesUnmarshaler().UnmarshalTraces(proto)
pbUnmarshaler := ptrace.ProtoUnmarshaler{}
otTrace, err := pbUnmarshaler.UnmarshalTraces(proto)
require.NoError(t, err)
var index int
@@ -66,10 +67,10 @@ func TestTraceToFrame(t *testing.T) {
rsp.ScopeSpans().RemoveIf(func(sp ptrace.ScopeSpans) bool {
sp.Spans().RemoveIf(func(span ptrace.Span) bool {
if index == 0 {
span.SetTraceID(pcommon.NewTraceID([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7}))
span.SetTraceID(pcommon.TraceID([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7}))
}
if index == 1 {
span.SetTraceID(pcommon.NewTraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7}))
span.SetTraceID(pcommon.TraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7}))
}
index++
return false
@@ -125,7 +126,7 @@ func (f *BetterFrame) FindRowWithValue(fieldName string, value interface{}) Row
}
func rootSpan(frame *BetterFrame) Row {
return frame.FindRowWithValue("parentSpanID", "")
return frame.FindRowWithValue("parentSpanID", "0000000000000000")
}
func fieldNames(frame *data.Frame) []string {