@@ -2,6 +2,8 @@ package xcap
22
33import (
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