Skip to content

Commit 55f948a

Browse files
committed
marshal attributes
1 parent 8d93819 commit 55f948a

File tree

8 files changed

+1701
-97
lines changed

8 files changed

+1701
-97
lines changed

pkg/engine/engine.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,17 @@ func linkCaptureUsingPlan(capture *xcap.Capture, plan *physical.Plan) {
435435
if len(parents) > 0 {
436436
// TODO: This is assuming a single parent which is not always true.
437437
// Fix this when we have plans with multiple parents.
438+
439+
if parents[0].Type() == physical.NodeTypeParallelize {
440+
// Skip Parallelize nodes as they are just not actual work.
441+
pp := plan.Graph().Parents(parents[0])
442+
if len(pp) > 0 {
443+
parents = pp
444+
} else {
445+
return nil
446+
}
447+
}
448+
438449
idToParentID[n.ID().String()] = parents[0].ID().String()
439450
}
440451
return nil

pkg/xcap/exporter.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,10 @@ func createSpans(ctx context.Context, region *Region, parentToChildren map[ident
5858
opts = append(opts, trace.WithAttributes(observationToAttribute(key, obs)))
5959
}
6060

61-
if region.parentID.IsZero() {
62-
// Creates a new trace for the root region.
63-
opts = append(opts, trace.WithNewRoot())
64-
}
65-
6661
// Create the span
6762
ctx, span := tracer.Start(ctx, region.name, opts...)
6863

69-
// Get children directly from the map (O(1) lookup)
7064
children := parentToChildren[region.id]
71-
7265
// Recursively create spans for children
7366
for _, child := range children {
7467
if err := createSpans(ctx, child, parentToChildren); err != nil {
@@ -83,7 +76,7 @@ func createSpans(ctx context.Context, region *Region, parentToChildren map[ident
8376

8477
// observationToAttribute converts an observation to an OpenTelemetry attribute.
8578
// The attribute key is the statistic name, and the value depends on the data type.
86-
func observationToAttribute(key StatisticKey, obs AggregatedObservation) attribute.KeyValue {
79+
func observationToAttribute(key StatisticKey, obs *AggregatedObservation) attribute.KeyValue {
8780
attrKey := attribute.Key(key.Name)
8881

8982
switch key.DataType {

pkg/xcap/marshal.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package xcap
22

33
import (
44
"fmt"
5+
6+
"go.opentelemetry.io/otel/attribute"
57
)
68

79
// ToProtoCapture converts a Capture to its protobuf representation.
@@ -63,13 +65,24 @@ func toProtoRegion(region *Region, statsIndex map[StatisticKey]uint32) (*ProtoRe
6365
})
6466
}
6567

68+
// Convert attributes to proto attributes
69+
protoAttributes := make([]*ProtoAttribute, 0, len(region.attributes))
70+
for _, attr := range region.attributes {
71+
protoAttr, err := marshalAttribute(attr)
72+
if err != nil {
73+
return nil, fmt.Errorf("failed to marshal attribute %s: %w", attr.Key, err)
74+
}
75+
protoAttributes = append(protoAttributes, protoAttr)
76+
}
77+
6678
return &ProtoRegion{
6779
Name: region.name,
6880
StartTime: region.startTime,
6981
EndTime: region.endTime,
7082
Observations: protoObservations,
7183
Id: region.id[:],
7284
ParentId: region.parentID[:],
85+
Attributes: protoAttributes,
7386
}, nil
7487
}
7588

@@ -128,3 +141,30 @@ func marshalAggregationType(agg AggregationType) ProtoAggregationType {
128141
return PROTO_AGGREGATION_TYPE_INVALID
129142
}
130143
}
144+
145+
// marshalAttribute converts an OpenTelemetry attribute to its protobuf representation.
146+
func marshalAttribute(attr attribute.KeyValue) (*ProtoAttribute, error) {
147+
if !attr.Valid() {
148+
return nil, fmt.Errorf("invalid attribute")
149+
}
150+
151+
protoValue := &ProtoAttributeValue{}
152+
switch attr.Value.Type() {
153+
case attribute.STRING:
154+
protoValue.Kind = &ProtoAttributeValue_StringValue{StringValue: attr.Value.AsString()}
155+
case attribute.INT64:
156+
protoValue.Kind = &ProtoAttributeValue_IntValue{IntValue: attr.Value.AsInt64()}
157+
case attribute.FLOAT64:
158+
protoValue.Kind = &ProtoAttributeValue_FloatValue{FloatValue: attr.Value.AsFloat64()}
159+
case attribute.BOOL:
160+
protoValue.Kind = &ProtoAttributeValue_BoolValue{BoolValue: attr.Value.AsBool()}
161+
default:
162+
// For unsupported types (like slices), convert to string
163+
protoValue.Kind = &ProtoAttributeValue_StringValue{StringValue: attr.Value.Emit()}
164+
}
165+
166+
return &ProtoAttribute{
167+
Key: string(attr.Key),
168+
Value: protoValue,
169+
}, nil
170+
}

0 commit comments

Comments
 (0)