mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-08-07 19:18:02 +08:00
add history trace command
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
11125
util/otelutil/fixtures/bktraces.json
Normal file
11125
util/otelutil/fixtures/bktraces.json
Normal file
File diff suppressed because it is too large
Load Diff
9542
util/otelutil/fixtures/jaeger.json
Normal file
9542
util/otelutil/fixtures/jaeger.json
Normal file
File diff suppressed because it is too large
Load Diff
11127
util/otelutil/fixtures/otlp.json
Normal file
11127
util/otelutil/fixtures/otlp.json
Normal file
File diff suppressed because it is too large
Load Diff
45
util/otelutil/jaeger.go
Normal file
45
util/otelutil/jaeger.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package otelutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/buildx/util/otelutil/jaeger"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
)
|
||||
|
||||
type JaegerData struct {
|
||||
Data []jaeger.Trace `json:"data"`
|
||||
}
|
||||
|
||||
// JaegerData return Jaeger data compatible with ui import feature.
|
||||
// https://github.com/jaegertracing/jaeger-ui/issues/381#issuecomment-494150826
|
||||
func (s Spans) JaegerData() JaegerData {
|
||||
roSpans := s.ReadOnlySpans()
|
||||
|
||||
// fetch default service.name from default resource for backup
|
||||
var defaultServiceName string
|
||||
defaultResource := resource.Default()
|
||||
if value, exists := defaultResource.Set().Value(attribute.Key("service.name")); exists {
|
||||
defaultServiceName = value.AsString()
|
||||
}
|
||||
|
||||
data := jaeger.Trace{
|
||||
TraceID: jaeger.TraceID(roSpans[0].SpanContext().TraceID().String()),
|
||||
Processes: make(map[jaeger.ProcessID]jaeger.Process),
|
||||
Spans: []jaeger.Span{},
|
||||
}
|
||||
for i := range roSpans {
|
||||
ss := roSpans[i]
|
||||
pid := jaeger.ProcessID(fmt.Sprintf("p%d", i))
|
||||
data.Processes[pid] = jaeger.ResourceToProcess(ss.Resource(), defaultServiceName)
|
||||
span := jaeger.ConvertSpan(ss)
|
||||
span.Process = nil
|
||||
span.ProcessID = pid
|
||||
data.Spans = append(data.Spans, span)
|
||||
}
|
||||
|
||||
return JaegerData{
|
||||
Data: []jaeger.Trace{data},
|
||||
}
|
||||
}
|
224
util/otelutil/jaeger/convert.go
Normal file
224
util/otelutil/jaeger/convert.go
Normal file
@@ -0,0 +1,224 @@
|
||||
package jaeger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
const (
|
||||
keyInstrumentationLibraryName = "otel.library.name"
|
||||
keyInstrumentationLibraryVersion = "otel.library.version"
|
||||
keyError = "error"
|
||||
keySpanKind = "span.kind"
|
||||
keyStatusCode = "otel.status_code"
|
||||
keyStatusMessage = "otel.status_description"
|
||||
keyDroppedAttributeCount = "otel.event.dropped_attributes_count"
|
||||
keyEventName = "event"
|
||||
)
|
||||
|
||||
func ResourceToProcess(res *resource.Resource, defaultServiceName string) Process {
|
||||
var process Process
|
||||
var serviceName attribute.KeyValue
|
||||
if res != nil {
|
||||
for iter := res.Iter(); iter.Next(); {
|
||||
if iter.Attribute().Key == attribute.Key("service.name") {
|
||||
serviceName = iter.Attribute()
|
||||
// Don't convert service.name into tag.
|
||||
continue
|
||||
}
|
||||
if tag := keyValueToJaegerTag(iter.Attribute()); tag != nil {
|
||||
process.Tags = append(process.Tags, *tag)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no service.name is contained in a Span's Resource,
|
||||
// that field MUST be populated from the default Resource.
|
||||
if serviceName.Value.AsString() == "" {
|
||||
serviceName = attribute.Key("service.version").String(defaultServiceName)
|
||||
}
|
||||
process.ServiceName = serviceName.Value.AsString()
|
||||
|
||||
return process
|
||||
}
|
||||
|
||||
func ConvertSpan(ss tracesdk.ReadOnlySpan) Span {
|
||||
attr := ss.Attributes()
|
||||
tags := make([]KeyValue, 0, len(attr))
|
||||
for _, kv := range attr {
|
||||
tag := keyValueToJaegerTag(kv)
|
||||
if tag != nil {
|
||||
tags = append(tags, *tag)
|
||||
}
|
||||
}
|
||||
|
||||
if is := ss.InstrumentationScope(); is.Name != "" {
|
||||
tags = append(tags, getStringTag(keyInstrumentationLibraryName, is.Name))
|
||||
if is.Version != "" {
|
||||
tags = append(tags, getStringTag(keyInstrumentationLibraryVersion, is.Version))
|
||||
}
|
||||
}
|
||||
|
||||
if ss.SpanKind() != trace.SpanKindInternal {
|
||||
tags = append(tags,
|
||||
getStringTag(keySpanKind, ss.SpanKind().String()),
|
||||
)
|
||||
}
|
||||
|
||||
if ss.Status().Code != codes.Unset {
|
||||
switch ss.Status().Code {
|
||||
case codes.Ok:
|
||||
tags = append(tags, getStringTag(keyStatusCode, "OK"))
|
||||
case codes.Error:
|
||||
tags = append(tags, getBoolTag(keyError, true))
|
||||
tags = append(tags, getStringTag(keyStatusCode, "ERROR"))
|
||||
}
|
||||
if ss.Status().Description != "" {
|
||||
tags = append(tags, getStringTag(keyStatusMessage, ss.Status().Description))
|
||||
}
|
||||
}
|
||||
|
||||
var logs []Log
|
||||
for _, a := range ss.Events() {
|
||||
nTags := len(a.Attributes)
|
||||
if a.Name != "" {
|
||||
nTags++
|
||||
}
|
||||
if a.DroppedAttributeCount != 0 {
|
||||
nTags++
|
||||
}
|
||||
fields := make([]KeyValue, 0, nTags)
|
||||
if a.Name != "" {
|
||||
// If an event contains an attribute with the same key, it needs
|
||||
// to be given precedence and overwrite this.
|
||||
fields = append(fields, getStringTag(keyEventName, a.Name))
|
||||
}
|
||||
for _, kv := range a.Attributes {
|
||||
tag := keyValueToJaegerTag(kv)
|
||||
if tag != nil {
|
||||
fields = append(fields, *tag)
|
||||
}
|
||||
}
|
||||
if a.DroppedAttributeCount != 0 {
|
||||
fields = append(fields, getInt64Tag(keyDroppedAttributeCount, int64(a.DroppedAttributeCount)))
|
||||
}
|
||||
logs = append(logs, Log{
|
||||
Timestamp: timeAsEpochMicroseconds(a.Time),
|
||||
Fields: fields,
|
||||
})
|
||||
}
|
||||
|
||||
var refs []Reference
|
||||
for _, link := range ss.Links() {
|
||||
refs = append(refs, Reference{
|
||||
RefType: FollowsFrom,
|
||||
TraceID: TraceID(link.SpanContext.TraceID().String()),
|
||||
SpanID: SpanID(link.SpanContext.SpanID().String()),
|
||||
})
|
||||
}
|
||||
refs = append(refs, Reference{
|
||||
RefType: ChildOf,
|
||||
TraceID: TraceID(ss.Parent().TraceID().String()),
|
||||
SpanID: SpanID(ss.Parent().SpanID().String()),
|
||||
})
|
||||
|
||||
return Span{
|
||||
TraceID: TraceID(ss.SpanContext().TraceID().String()),
|
||||
SpanID: SpanID(ss.SpanContext().SpanID().String()),
|
||||
Flags: uint32(ss.SpanContext().TraceFlags()),
|
||||
OperationName: ss.Name(),
|
||||
References: refs,
|
||||
StartTime: timeAsEpochMicroseconds(ss.StartTime()),
|
||||
Duration: durationAsMicroseconds(ss.EndTime().Sub(ss.StartTime())),
|
||||
Tags: tags,
|
||||
Logs: logs,
|
||||
}
|
||||
}
|
||||
|
||||
func keyValueToJaegerTag(keyValue attribute.KeyValue) *KeyValue {
|
||||
var tag *KeyValue
|
||||
switch keyValue.Value.Type() {
|
||||
case attribute.STRING:
|
||||
s := keyValue.Value.AsString()
|
||||
tag = &KeyValue{
|
||||
Key: string(keyValue.Key),
|
||||
Type: StringType,
|
||||
Value: s,
|
||||
}
|
||||
case attribute.BOOL:
|
||||
b := keyValue.Value.AsBool()
|
||||
tag = &KeyValue{
|
||||
Key: string(keyValue.Key),
|
||||
Type: BoolType,
|
||||
Value: b,
|
||||
}
|
||||
case attribute.INT64:
|
||||
i := keyValue.Value.AsInt64()
|
||||
tag = &KeyValue{
|
||||
Key: string(keyValue.Key),
|
||||
Type: Int64Type,
|
||||
Value: i,
|
||||
}
|
||||
case attribute.FLOAT64:
|
||||
f := keyValue.Value.AsFloat64()
|
||||
tag = &KeyValue{
|
||||
Key: string(keyValue.Key),
|
||||
Type: Float64Type,
|
||||
Value: f,
|
||||
}
|
||||
case attribute.BOOLSLICE,
|
||||
attribute.INT64SLICE,
|
||||
attribute.FLOAT64SLICE,
|
||||
attribute.STRINGSLICE:
|
||||
data, _ := json.Marshal(keyValue.Value.AsInterface())
|
||||
a := (string)(data)
|
||||
tag = &KeyValue{
|
||||
Key: string(keyValue.Key),
|
||||
Type: StringType,
|
||||
Value: a,
|
||||
}
|
||||
}
|
||||
return tag
|
||||
}
|
||||
|
||||
func getInt64Tag(k string, i int64) KeyValue {
|
||||
return KeyValue{
|
||||
Key: k,
|
||||
Type: Int64Type,
|
||||
Value: i,
|
||||
}
|
||||
}
|
||||
|
||||
func getStringTag(k, s string) KeyValue {
|
||||
return KeyValue{
|
||||
Key: k,
|
||||
Type: StringType,
|
||||
Value: s,
|
||||
}
|
||||
}
|
||||
|
||||
func getBoolTag(k string, b bool) KeyValue {
|
||||
return KeyValue{
|
||||
Key: k,
|
||||
Type: BoolType,
|
||||
Value: b,
|
||||
}
|
||||
}
|
||||
|
||||
// timeAsEpochMicroseconds converts time.Time to microseconds since epoch,
|
||||
// which is the format the StartTime field is stored in the Span.
|
||||
func timeAsEpochMicroseconds(t time.Time) uint64 {
|
||||
return uint64(t.UnixNano() / 1000)
|
||||
}
|
||||
|
||||
// durationAsMicroseconds converts time.Duration to microseconds,
|
||||
// which is the format the Duration field is stored in the Span.
|
||||
func durationAsMicroseconds(d time.Duration) uint64 {
|
||||
return uint64(d.Nanoseconds() / 1000)
|
||||
}
|
102
util/otelutil/jaeger/model.go
Normal file
102
util/otelutil/jaeger/model.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package jaeger
|
||||
|
||||
// ReferenceType is the reference type of one span to another
|
||||
type ReferenceType string
|
||||
|
||||
// TraceID is the shared trace ID of all spans in the trace.
|
||||
type TraceID string
|
||||
|
||||
// SpanID is the id of a span
|
||||
type SpanID string
|
||||
|
||||
// ProcessID is a hashed value of the Process struct that is unique within the trace.
|
||||
type ProcessID string
|
||||
|
||||
// ValueType is the type of a value stored in KeyValue struct.
|
||||
type ValueType string
|
||||
|
||||
const (
|
||||
// ChildOf means a span is the child of another span
|
||||
ChildOf ReferenceType = "CHILD_OF"
|
||||
// FollowsFrom means a span follows from another span
|
||||
FollowsFrom ReferenceType = "FOLLOWS_FROM"
|
||||
|
||||
// StringType indicates a string value stored in KeyValue
|
||||
StringType ValueType = "string"
|
||||
// BoolType indicates a Boolean value stored in KeyValue
|
||||
BoolType ValueType = "bool"
|
||||
// Int64Type indicates a 64bit signed integer value stored in KeyValue
|
||||
Int64Type ValueType = "int64"
|
||||
// Float64Type indicates a 64bit float value stored in KeyValue
|
||||
Float64Type ValueType = "float64"
|
||||
// BinaryType indicates an arbitrary byte array stored in KeyValue
|
||||
BinaryType ValueType = "binary"
|
||||
)
|
||||
|
||||
// Trace is a list of spans
|
||||
type Trace struct {
|
||||
TraceID TraceID `json:"traceID"`
|
||||
Spans []Span `json:"spans"`
|
||||
Processes map[ProcessID]Process `json:"processes"`
|
||||
Warnings []string `json:"warnings"`
|
||||
}
|
||||
|
||||
// Span is a span denoting a piece of work in some infrastructure
|
||||
// When converting to UI model, ParentSpanID and Process should be dereferenced into
|
||||
// References and ProcessID, respectively.
|
||||
// When converting to ES model, ProcessID and Warnings should be omitted. Even if
|
||||
// included, ES with dynamic settings off will automatically ignore unneeded fields.
|
||||
type Span struct {
|
||||
TraceID TraceID `json:"traceID"`
|
||||
SpanID SpanID `json:"spanID"`
|
||||
ParentSpanID SpanID `json:"parentSpanID,omitempty"` // deprecated
|
||||
Flags uint32 `json:"flags,omitempty"`
|
||||
OperationName string `json:"operationName"`
|
||||
References []Reference `json:"references"`
|
||||
StartTime uint64 `json:"startTime"` // microseconds since Unix epoch
|
||||
Duration uint64 `json:"duration"` // microseconds
|
||||
Tags []KeyValue `json:"tags"`
|
||||
Logs []Log `json:"logs"`
|
||||
ProcessID ProcessID `json:"processID,omitempty"`
|
||||
Process *Process `json:"process,omitempty"`
|
||||
Warnings []string `json:"warnings"`
|
||||
}
|
||||
|
||||
// Reference is a reference from one span to another
|
||||
type Reference struct {
|
||||
RefType ReferenceType `json:"refType"`
|
||||
TraceID TraceID `json:"traceID"`
|
||||
SpanID SpanID `json:"spanID"`
|
||||
}
|
||||
|
||||
// Process is the process emitting a set of spans
|
||||
type Process struct {
|
||||
ServiceName string `json:"serviceName"`
|
||||
Tags []KeyValue `json:"tags"`
|
||||
}
|
||||
|
||||
// Log is a log emitted in a span
|
||||
type Log struct {
|
||||
Timestamp uint64 `json:"timestamp"`
|
||||
Fields []KeyValue `json:"fields"`
|
||||
}
|
||||
|
||||
// KeyValue is a key-value pair with typed value.
|
||||
type KeyValue struct {
|
||||
Key string `json:"key"`
|
||||
Type ValueType `json:"type,omitempty"`
|
||||
Value interface{} `json:"value"`
|
||||
}
|
||||
|
||||
// DependencyLink shows dependencies between services
|
||||
type DependencyLink struct {
|
||||
Parent string `json:"parent"`
|
||||
Child string `json:"child"`
|
||||
CallCount uint64 `json:"callCount"`
|
||||
}
|
||||
|
||||
// Operation defines the data in the operation response when query operation by service and span kind
|
||||
type Operation struct {
|
||||
Name string `json:"name"`
|
||||
SpanKind string `json:"spanKind"`
|
||||
}
|
27
util/otelutil/jaeger_test.go
Normal file
27
util/otelutil/jaeger_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package otelutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const jaegerFixture = "./fixtures/jaeger.json"
|
||||
|
||||
func TestJaegerData(t *testing.T) {
|
||||
dt, err := os.ReadFile(bktracesFixture)
|
||||
require.NoError(t, err)
|
||||
|
||||
spanStubs, err := ParseSpanStubs(bytes.NewReader(dt))
|
||||
require.NoError(t, err)
|
||||
|
||||
trace := spanStubs.JaegerData()
|
||||
dtJaegerTrace, err := json.MarshalIndent(trace, "", " ")
|
||||
require.NoError(t, err)
|
||||
dtJaeger, err := os.ReadFile(jaegerFixture)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, string(dtJaeger), string(dtJaegerTrace))
|
||||
}
|
491
util/otelutil/span.go
Normal file
491
util/otelutil/span.go
Normal file
@@ -0,0 +1,491 @@
|
||||
package otelutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/sdk/instrumentation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Span is a type similar to otel's SpanStub, but with the correct types needed
|
||||
// for handle marshaling and unmarshalling.
|
||||
type Span struct {
|
||||
// Name is the name of a specific span
|
||||
Name string
|
||||
// SpanContext is the unique SpanContext that identifies the span
|
||||
SpanContext trace.SpanContext
|
||||
// Parten is the unique SpanContext that identifies the parent of the span.
|
||||
// If the span has no parent, this span context will be invalid.
|
||||
Parent trace.SpanContext
|
||||
// SpanKind is the role the span plays in a Trace
|
||||
SpanKind trace.SpanKind
|
||||
// StartTime is the time the span started recording
|
||||
StartTime time.Time
|
||||
// EndTime returns the time the span stopped recording
|
||||
EndTime time.Time
|
||||
// Attributes are the defining attributes of a span
|
||||
Attributes []attribute.KeyValue
|
||||
// Events are all the events that occurred within the span
|
||||
Events []tracesdk.Event
|
||||
// Links are all the links the span has to other spans
|
||||
Links []tracesdk.Link
|
||||
// Status is that span status
|
||||
Status tracesdk.Status
|
||||
// DroppedAttributes is the number of attributes dropped by the span due to
|
||||
// a limit being reached
|
||||
DroppedAttributes int
|
||||
// DroppedEvents is the number of attributes dropped by the span due to a
|
||||
// limit being reached
|
||||
DroppedEvents int
|
||||
// DroppedLinks is the number of links dropped by the span due to a limit
|
||||
// being reached
|
||||
DroppedLinks int
|
||||
// ChildSpanCount is the count of spans that consider the span a direct
|
||||
// parent
|
||||
ChildSpanCount int
|
||||
// Resource is the information about the entity that produced the span
|
||||
// We have to change this type from the otel type to make this struct
|
||||
// marshallable
|
||||
Resource []attribute.KeyValue
|
||||
// InstrumentationLibrary is information about the library that produced
|
||||
// the span
|
||||
//nolint:staticcheck
|
||||
InstrumentationLibrary instrumentation.Library
|
||||
}
|
||||
|
||||
type Spans []Span
|
||||
|
||||
// Len return the length of the Spans.
|
||||
func (s Spans) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
|
||||
// ReadOnlySpans return a list of tracesdk.ReadOnlySpan from span stubs.
|
||||
func (s Spans) ReadOnlySpans() []tracesdk.ReadOnlySpan {
|
||||
roSpans := make([]tracesdk.ReadOnlySpan, len(s))
|
||||
for i := range s {
|
||||
roSpans[i] = s[i].Snapshot()
|
||||
}
|
||||
return roSpans
|
||||
}
|
||||
|
||||
// ParseSpanStubs parses BuildKit trace data into a list of SpanStubs.
|
||||
func ParseSpanStubs(rdr io.Reader) (Spans, error) {
|
||||
var spanStubs []Span
|
||||
decoder := json.NewDecoder(rdr)
|
||||
for {
|
||||
var span Span
|
||||
if err := decoder.Decode(&span); err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, errors.Wrapf(err, "error decoding JSON")
|
||||
}
|
||||
spanStubs = append(spanStubs, span)
|
||||
}
|
||||
return spanStubs, nil
|
||||
}
|
||||
|
||||
// spanData is data that we need to unmarshal in custom ways.
|
||||
type spanData struct {
|
||||
Name string
|
||||
SpanContext spanContext
|
||||
Parent spanContext
|
||||
SpanKind trace.SpanKind
|
||||
StartTime time.Time
|
||||
EndTime time.Time
|
||||
Attributes []keyValue
|
||||
Events []event
|
||||
Links []link
|
||||
Status tracesdk.Status
|
||||
DroppedAttributes int
|
||||
DroppedEvents int
|
||||
DroppedLinks int
|
||||
ChildSpanCount int
|
||||
Resource []keyValue // change this type from the otel type to make this struct marshallable
|
||||
//nolint:staticcheck
|
||||
InstrumentationLibrary instrumentation.Library
|
||||
}
|
||||
|
||||
// spanContext is a custom type used to unmarshal otel SpanContext correctly.
|
||||
type spanContext struct {
|
||||
TraceID string
|
||||
SpanID string
|
||||
TraceFlags string
|
||||
TraceState string // TODO: implement, currently dropped
|
||||
Remote bool
|
||||
}
|
||||
|
||||
// event is a custom type used to unmarshal otel Event correctly.
|
||||
type event struct {
|
||||
Name string
|
||||
Attributes []keyValue
|
||||
DroppedAttributeCount int
|
||||
Time time.Time
|
||||
}
|
||||
|
||||
// link is a custom type used to unmarshal otel Link correctly.
|
||||
type link struct {
|
||||
SpanContext spanContext
|
||||
Attributes []keyValue
|
||||
DroppedAttributeCount int
|
||||
}
|
||||
|
||||
// keyValue is a custom type used to unmarshal otel KeyValue correctly.
|
||||
type keyValue struct {
|
||||
Key string
|
||||
Value value
|
||||
}
|
||||
|
||||
// value is a custom type used to unmarshal otel Value correctly.
|
||||
type value struct {
|
||||
Type string
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler for Span which allows correctly
|
||||
// retrieving attribute.KeyValue values.
|
||||
func (s *Span) UnmarshalJSON(data []byte) error {
|
||||
var sd spanData
|
||||
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&sd); err != nil {
|
||||
return errors.Wrap(err, "unable to decode to spanData")
|
||||
}
|
||||
|
||||
s.Name = sd.Name
|
||||
s.SpanKind = sd.SpanKind
|
||||
s.StartTime = sd.StartTime
|
||||
s.EndTime = sd.EndTime
|
||||
s.Status = sd.Status
|
||||
s.DroppedAttributes = sd.DroppedAttributes
|
||||
s.DroppedEvents = sd.DroppedEvents
|
||||
s.DroppedLinks = sd.DroppedLinks
|
||||
s.ChildSpanCount = sd.ChildSpanCount
|
||||
s.InstrumentationLibrary = sd.InstrumentationLibrary
|
||||
|
||||
spanCtx, err := sd.SpanContext.asTraceSpanContext()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to decode spanCtx")
|
||||
}
|
||||
s.SpanContext = spanCtx
|
||||
|
||||
parent, err := sd.Parent.asTraceSpanContext()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to decode parent")
|
||||
}
|
||||
s.Parent = parent
|
||||
|
||||
var attributes []attribute.KeyValue
|
||||
for _, a := range sd.Attributes {
|
||||
kv, err := a.asAttributeKeyValue()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to decode attribute (%s)", a.Key)
|
||||
}
|
||||
attributes = append(attributes, kv)
|
||||
}
|
||||
s.Attributes = attributes
|
||||
|
||||
var events []tracesdk.Event
|
||||
for _, e := range sd.Events {
|
||||
var eventAttributes []attribute.KeyValue
|
||||
for _, a := range e.Attributes {
|
||||
kv, err := a.asAttributeKeyValue()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to decode event attribute (%s)", a.Key)
|
||||
}
|
||||
eventAttributes = append(eventAttributes, kv)
|
||||
}
|
||||
events = append(events, tracesdk.Event{
|
||||
Name: e.Name,
|
||||
Attributes: eventAttributes,
|
||||
DroppedAttributeCount: e.DroppedAttributeCount,
|
||||
Time: e.Time,
|
||||
})
|
||||
}
|
||||
s.Events = events
|
||||
|
||||
var links []tracesdk.Link
|
||||
for _, l := range sd.Links {
|
||||
linkSpanCtx, err := l.SpanContext.asTraceSpanContext()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "unable to decode linkSpanCtx")
|
||||
}
|
||||
var linkAttributes []attribute.KeyValue
|
||||
for _, a := range l.Attributes {
|
||||
kv, err := a.asAttributeKeyValue()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to decode link attribute (%s)", a.Key)
|
||||
}
|
||||
linkAttributes = append(linkAttributes, kv)
|
||||
}
|
||||
links = append(links, tracesdk.Link{
|
||||
SpanContext: linkSpanCtx,
|
||||
Attributes: linkAttributes,
|
||||
DroppedAttributeCount: l.DroppedAttributeCount,
|
||||
})
|
||||
}
|
||||
s.Links = links
|
||||
|
||||
var resources []attribute.KeyValue
|
||||
for _, r := range sd.Resource {
|
||||
kv, err := r.asAttributeKeyValue()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to decode resource (%s)", r.Key)
|
||||
}
|
||||
resources = append(resources, kv)
|
||||
}
|
||||
s.Resource = resources
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// asTraceSpanContext converts the internal spanContext representation to an
|
||||
// otel one.
|
||||
func (sc *spanContext) asTraceSpanContext() (trace.SpanContext, error) {
|
||||
traceID, err := traceIDFromHex(sc.TraceID)
|
||||
if err != nil {
|
||||
return trace.SpanContext{}, errors.Wrap(err, "unable to parse trace id")
|
||||
}
|
||||
spanID, err := spanIDFromHex(sc.SpanID)
|
||||
if err != nil {
|
||||
return trace.SpanContext{}, errors.Wrap(err, "unable to parse span id")
|
||||
}
|
||||
traceFlags := trace.TraceFlags(0x00)
|
||||
if sc.TraceFlags == "01" {
|
||||
traceFlags = trace.TraceFlags(0x01)
|
||||
}
|
||||
config := trace.SpanContextConfig{
|
||||
TraceID: traceID,
|
||||
SpanID: spanID,
|
||||
TraceFlags: traceFlags,
|
||||
Remote: sc.Remote,
|
||||
}
|
||||
return trace.NewSpanContext(config), nil
|
||||
}
|
||||
|
||||
// asAttributeKeyValue converts the internal keyValue representation to an
|
||||
// otel one.
|
||||
func (kv *keyValue) asAttributeKeyValue() (attribute.KeyValue, error) {
|
||||
// value types get encoded as string
|
||||
switch kv.Value.Type {
|
||||
case attribute.INVALID.String():
|
||||
return attribute.KeyValue{}, errors.New("invalid value type")
|
||||
case attribute.BOOL.String():
|
||||
return attribute.Bool(kv.Key, kv.Value.Value.(bool)), nil
|
||||
case attribute.INT64.String():
|
||||
// value could be int64 or float64, so handle both cases (float64 comes
|
||||
// from json unmarshal)
|
||||
var v int64
|
||||
switch i := kv.Value.Value.(type) {
|
||||
case int64:
|
||||
v = i
|
||||
case float64:
|
||||
v = int64(i)
|
||||
}
|
||||
return attribute.Int64(kv.Key, v), nil
|
||||
case attribute.FLOAT64.String():
|
||||
return attribute.Float64(kv.Key, kv.Value.Value.(float64)), nil
|
||||
case attribute.STRING.String():
|
||||
return attribute.String(kv.Key, kv.Value.Value.(string)), nil
|
||||
case attribute.BOOLSLICE.String():
|
||||
return attribute.BoolSlice(kv.Key, kv.Value.Value.([]bool)), nil
|
||||
case attribute.INT64SLICE.String():
|
||||
// handle both float64 and int64 (float64 comes from json unmarshal)
|
||||
var v []int64
|
||||
switch sli := kv.Value.Value.(type) {
|
||||
case []int64:
|
||||
v = sli
|
||||
case []float64:
|
||||
for i := range sli {
|
||||
v = append(v, int64(sli[i]))
|
||||
}
|
||||
}
|
||||
return attribute.Int64Slice(kv.Key, v), nil
|
||||
case attribute.FLOAT64SLICE.String():
|
||||
return attribute.Float64Slice(kv.Key, kv.Value.Value.([]float64)), nil
|
||||
case attribute.STRINGSLICE.String():
|
||||
var strSli []string
|
||||
// sometimes we can get an []interface{} instead of a []string, so
|
||||
// always cast to []string if that happens.
|
||||
switch sli := kv.Value.Value.(type) {
|
||||
case []string:
|
||||
strSli = sli
|
||||
case []interface{}:
|
||||
for i := range sli {
|
||||
var v string
|
||||
// best case we have a string, otherwise, cast it using
|
||||
// fmt.Sprintf
|
||||
if str, ok := sli[i].(string); ok {
|
||||
v = str
|
||||
} else {
|
||||
v = fmt.Sprintf("%v", sli[i])
|
||||
}
|
||||
// add the string to the slice
|
||||
strSli = append(strSli, v)
|
||||
}
|
||||
default:
|
||||
return attribute.KeyValue{}, errors.Errorf("got unsupported type %q for %s", reflect.ValueOf(kv.Value.Value).Kind(), attribute.STRINGSLICE.String())
|
||||
}
|
||||
return attribute.StringSlice(kv.Key, strSli), nil
|
||||
default:
|
||||
return attribute.KeyValue{}, errors.Errorf("unknown value type %s", kv.Value.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// traceIDFromHex returns a TraceID from a hex string if it is compliant with
|
||||
// the W3C trace-context specification and removes the validity check.
|
||||
// https://www.w3.org/TR/trace-context/#trace-id
|
||||
func traceIDFromHex(h string) (trace.TraceID, error) {
|
||||
t := trace.TraceID{}
|
||||
if len(h) != 32 {
|
||||
return t, errors.New("unable to parse trace id")
|
||||
}
|
||||
if err := decodeHex(h, t[:]); err != nil {
|
||||
return t, err
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// spanIDFromHex returns a SpanID from a hex string if it is compliant with the
|
||||
// W3C trace-context specification and removes the validity check.
|
||||
// https://www.w3.org/TR/trace-context/#parent-id
|
||||
func spanIDFromHex(h string) (trace.SpanID, error) {
|
||||
s := trace.SpanID{}
|
||||
if len(h) != 16 {
|
||||
return s, errors.New("unable to parse span id of length: %d")
|
||||
}
|
||||
if err := decodeHex(h, s[:]); err != nil {
|
||||
return s, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// decodeHex decodes hex in a manner compliant with otel.
|
||||
func decodeHex(h string, b []byte) error {
|
||||
for _, r := range h {
|
||||
switch {
|
||||
case 'a' <= r && r <= 'f':
|
||||
continue
|
||||
case '0' <= r && r <= '9':
|
||||
continue
|
||||
default:
|
||||
return errors.New("unable to parse hex id")
|
||||
}
|
||||
}
|
||||
decoded, err := hex.DecodeString(h)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
copy(b, decoded)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Snapshot turns a Span into a ReadOnlySpan which is exportable by otel.
|
||||
func (s *Span) Snapshot() tracesdk.ReadOnlySpan {
|
||||
return spanSnapshot{
|
||||
name: s.Name,
|
||||
spanContext: s.SpanContext,
|
||||
parent: s.Parent,
|
||||
spanKind: s.SpanKind,
|
||||
startTime: s.StartTime,
|
||||
endTime: s.EndTime,
|
||||
attributes: s.Attributes,
|
||||
events: s.Events,
|
||||
links: s.Links,
|
||||
status: s.Status,
|
||||
droppedAttributes: s.DroppedAttributes,
|
||||
droppedEvents: s.DroppedEvents,
|
||||
droppedLinks: s.DroppedLinks,
|
||||
childSpanCount: s.ChildSpanCount,
|
||||
resource: resource.NewSchemaless(s.Resource...),
|
||||
instrumentationScope: s.InstrumentationLibrary,
|
||||
}
|
||||
}
|
||||
|
||||
// spanSnapshot is a helper type for transforming a Span into a ReadOnlySpan.
|
||||
type spanSnapshot struct {
|
||||
// Embed the interface to implement the private method.
|
||||
tracesdk.ReadOnlySpan
|
||||
|
||||
name string
|
||||
spanContext trace.SpanContext
|
||||
parent trace.SpanContext
|
||||
spanKind trace.SpanKind
|
||||
startTime time.Time
|
||||
endTime time.Time
|
||||
attributes []attribute.KeyValue
|
||||
events []tracesdk.Event
|
||||
links []tracesdk.Link
|
||||
status tracesdk.Status
|
||||
droppedAttributes int
|
||||
droppedEvents int
|
||||
droppedLinks int
|
||||
childSpanCount int
|
||||
resource *resource.Resource
|
||||
instrumentationScope instrumentation.Scope
|
||||
}
|
||||
|
||||
// Name returns the Name of the snapshot
|
||||
func (s spanSnapshot) Name() string { return s.name }
|
||||
|
||||
// SpanContext returns the SpanContext of the snapshot
|
||||
func (s spanSnapshot) SpanContext() trace.SpanContext { return s.spanContext }
|
||||
|
||||
// Parent returns the Parent of the snapshot
|
||||
func (s spanSnapshot) Parent() trace.SpanContext { return s.parent }
|
||||
|
||||
// SpanKind returns the SpanKind of the snapshot
|
||||
func (s spanSnapshot) SpanKind() trace.SpanKind { return s.spanKind }
|
||||
|
||||
// StartTime returns the StartTime of the snapshot
|
||||
func (s spanSnapshot) StartTime() time.Time { return s.startTime }
|
||||
|
||||
// EndTime returns the EndTime of the snapshot
|
||||
func (s spanSnapshot) EndTime() time.Time { return s.endTime }
|
||||
|
||||
// Attributes returns the Attributes of the snapshot
|
||||
func (s spanSnapshot) Attributes() []attribute.KeyValue { return s.attributes }
|
||||
|
||||
// Links returns the Links of the snapshot
|
||||
func (s spanSnapshot) Links() []tracesdk.Link { return s.links }
|
||||
|
||||
// Events return the Events of the snapshot
|
||||
func (s spanSnapshot) Events() []tracesdk.Event { return s.events }
|
||||
|
||||
// Status returns the Status of the snapshot
|
||||
func (s spanSnapshot) Status() tracesdk.Status { return s.status }
|
||||
|
||||
// DroppedAttributes returns the DroppedAttributes of the snapshot
|
||||
func (s spanSnapshot) DroppedAttributes() int { return s.droppedAttributes }
|
||||
|
||||
// DroppedLinks returns the DroppedLinks of the snapshot
|
||||
func (s spanSnapshot) DroppedLinks() int { return s.droppedLinks }
|
||||
|
||||
// DroppedEvents returns the DroppedEvents of the snapshot
|
||||
func (s spanSnapshot) DroppedEvents() int { return s.droppedEvents }
|
||||
|
||||
// ChildSpanCount returns the ChildSpanCount of the snapshot
|
||||
func (s spanSnapshot) ChildSpanCount() int { return s.childSpanCount }
|
||||
|
||||
// Resource returns the Resource of the snapshot
|
||||
func (s spanSnapshot) Resource() *resource.Resource { return s.resource }
|
||||
|
||||
// InstrumentationScope returns the InstrumentationScope of the snapshot
|
||||
func (s spanSnapshot) InstrumentationScope() instrumentation.Scope {
|
||||
return s.instrumentationScope
|
||||
}
|
||||
|
||||
// InstrumentationLibrary returns the InstrumentationLibrary of the snapshot
|
||||
//
|
||||
//nolint:staticcheck
|
||||
func (s spanSnapshot) InstrumentationLibrary() instrumentation.Library {
|
||||
return s.instrumentationScope
|
||||
}
|
159
util/otelutil/span_test.go
Normal file
159
util/otelutil/span_test.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package otelutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
|
||||
)
|
||||
|
||||
// curl -s --unix-socket /tmp/docker-desktop-build-dev.sock http://localhost/blobs/default/default?digest=sha256:3103104e9fa908087bd47572da6ad9a5a7bf973608f736536d18d635a7da0140 -X GET > ./fixtures/bktraces.json
|
||||
const bktracesFixture = "./fixtures/bktraces.json"
|
||||
|
||||
const otlpFixture = "./fixtures/otlp.json"
|
||||
|
||||
func TestParseSpanStubs(t *testing.T) {
|
||||
dt, err := os.ReadFile(bktracesFixture)
|
||||
require.NoError(t, err)
|
||||
|
||||
spanStubs, err := ParseSpanStubs(bytes.NewReader(dt))
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 73, len(spanStubs))
|
||||
|
||||
dtSpanStubs, err := json.MarshalIndent(spanStubs, "", " ")
|
||||
require.NoError(t, err)
|
||||
dtotel, err := os.ReadFile(otlpFixture)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, string(dtotel), string(dtSpanStubs))
|
||||
|
||||
exp, err := stdouttrace.New(stdouttrace.WithPrettyPrint())
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, exp.ExportSpans(context.Background(), spanStubs.ReadOnlySpans()))
|
||||
}
|
||||
|
||||
func TestAsAttributeKeyValue(t *testing.T) {
|
||||
type args struct {
|
||||
Type string
|
||||
value any
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want attribute.KeyValue
|
||||
}{
|
||||
{
|
||||
name: "string",
|
||||
args: args{
|
||||
Type: attribute.STRING.String(),
|
||||
value: "value",
|
||||
},
|
||||
want: attribute.String("key", "value"),
|
||||
},
|
||||
{
|
||||
name: "int64 (int64)",
|
||||
args: args{
|
||||
Type: attribute.INT64.String(),
|
||||
value: int64(1),
|
||||
},
|
||||
want: attribute.Int64("key", 1),
|
||||
},
|
||||
{
|
||||
name: "int64 (float64)",
|
||||
args: args{
|
||||
Type: attribute.INT64.String(),
|
||||
value: float64(1.0),
|
||||
},
|
||||
want: attribute.Int64("key", 1),
|
||||
},
|
||||
{
|
||||
name: "bool",
|
||||
args: args{
|
||||
Type: attribute.BOOL.String(),
|
||||
value: true,
|
||||
},
|
||||
want: attribute.Bool("key", true),
|
||||
},
|
||||
{
|
||||
name: "float64",
|
||||
args: args{
|
||||
Type: attribute.FLOAT64.String(),
|
||||
value: float64(1.0),
|
||||
},
|
||||
want: attribute.Float64("key", 1.0),
|
||||
},
|
||||
{
|
||||
name: "float64slice",
|
||||
args: args{
|
||||
Type: attribute.FLOAT64SLICE.String(),
|
||||
value: []float64{1.0, 2.0},
|
||||
},
|
||||
want: attribute.Float64Slice("key", []float64{1.0, 2.0}),
|
||||
},
|
||||
{
|
||||
name: "int64slice (int64)",
|
||||
args: args{
|
||||
Type: attribute.INT64SLICE.String(),
|
||||
value: []int64{1, 2},
|
||||
},
|
||||
want: attribute.Int64Slice("key", []int64{1, 2}),
|
||||
},
|
||||
{
|
||||
name: "int64slice (float64)",
|
||||
args: args{
|
||||
Type: attribute.INT64SLICE.String(),
|
||||
value: []float64{1.0, 2.0},
|
||||
},
|
||||
want: attribute.Int64Slice("key", []int64{1, 2}),
|
||||
},
|
||||
{
|
||||
name: "boolslice",
|
||||
args: args{
|
||||
Type: attribute.BOOLSLICE.String(),
|
||||
value: []bool{true, false},
|
||||
},
|
||||
want: attribute.BoolSlice("key", []bool{true, false}),
|
||||
},
|
||||
{
|
||||
name: "stringslice (strings)",
|
||||
args: args{
|
||||
Type: attribute.STRINGSLICE.String(),
|
||||
value: []string{"value1", "value2"},
|
||||
},
|
||||
want: attribute.StringSlice("key", []string{"value1", "value2"}),
|
||||
},
|
||||
{
|
||||
name: "stringslice (interface of string)",
|
||||
args: args{
|
||||
Type: attribute.STRINGSLICE.String(),
|
||||
value: []interface{}{"value1", "value2"},
|
||||
},
|
||||
want: attribute.StringSlice("key", []string{"value1", "value2"}),
|
||||
},
|
||||
{
|
||||
name: "stringslice (interface mixed)",
|
||||
args: args{
|
||||
Type: attribute.STRINGSLICE.String(),
|
||||
value: []interface{}{"value1", 2},
|
||||
},
|
||||
want: attribute.StringSlice("key", []string{"value1", "2"}),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
kv := keyValue{
|
||||
Key: "key",
|
||||
Value: value{Type: tt.args.Type, Value: tt.args.value},
|
||||
}
|
||||
attr, err := kv.asAttributeKeyValue()
|
||||
require.NoError(t, err, "failed to convert key value to attribute key value")
|
||||
assert.Equal(t, tt.want, attr, "attribute key value mismatch")
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user