@@ -30,6 +30,15 @@ func ConvertOtelToGoogle(src *otelProfile.Profile) *googleProfile.Profile {
30
30
Comment : src .Comment ,
31
31
Mapping : convertMappingsBack (src .Mapping ),
32
32
}
33
+ stringmap := make (map [string ]int )
34
+ addstr := func (s string ) int64 {
35
+ if _ , ok := stringmap [s ]; ! ok {
36
+ stringmap [s ] = len (dst .StringTable )
37
+ dst .StringTable = append (dst .StringTable , s )
38
+ }
39
+ return int64 (stringmap [s ])
40
+ }
41
+ addstr ("" )
33
42
34
43
if dst .TimeNanos == 0 {
35
44
dst .TimeNanos = time .Now ().UnixNano ()
@@ -48,49 +57,44 @@ func ConvertOtelToGoogle(src *otelProfile.Profile) *googleProfile.Profile {
48
57
gf .Id = uint64 (i + 1 )
49
58
dst .Function = append (dst .Function , gf )
50
59
}
60
+ funcmap := map [string ]uint64 {}
61
+ addfunc := func (s string ) uint64 {
62
+ if _ , ok := funcmap [s ]; ! ok {
63
+ funcmap [s ] = uint64 (len (dst .Function )) + 1
64
+ dst .Function = append (dst .Function , & googleProfile.Function {
65
+ Id : funcmap [s ],
66
+ Name : addstr (s ),
67
+ })
68
+ }
69
+ return funcmap [s ]
70
+ }
51
71
52
- functionOffset := uint64 (len (dst .Function )) + 1
53
72
dst .Location = []* googleProfile.Location {}
54
- locationMappingIndexAddressMap := make (map [uint64 ]uint64 )
55
73
// Convert locations and mappings
56
74
for i , loc := range src .Location {
57
75
gl := convertLocationBack (loc )
58
76
gl .Id = uint64 (i + 1 )
59
77
if len (gl .Line ) == 0 {
78
+ m := src .Mapping [loc .MappingIndex ]
60
79
gl .Line = append (gl .Line , & googleProfile.Line {
61
- FunctionId : loc . MappingIndex + functionOffset ,
80
+ FunctionId : addfunc ( fmt . Sprintf ( "%s 0x%x" , src . StringTable [ m . Filename ], loc . Address )) ,
62
81
})
63
82
}
64
83
dst .Location = append (dst .Location , gl )
65
- locationMappingIndexAddressMap [loc .MappingIndex ] = loc .Address
66
- }
67
-
68
- for _ , m := range src .Mapping {
69
- address := locationMappingIndexAddressMap [m .Id ]
70
- addressStr := fmt .Sprintf ("%s 0x%x" , dst .StringTable [m .Filename ], address )
71
- dst .StringTable = append (dst .StringTable , addressStr )
72
- // i == 0 function_id = functionOffset
73
- id := uint64 (len (dst .Function )) + 1
74
- dst .Function = append (dst .Function , & googleProfile.Function {
75
- Id : id ,
76
- Name : int64 (len (dst .StringTable ) - 1 ),
77
- })
78
84
}
79
85
80
86
// Convert samples
81
87
for _ , sample := range src .Sample {
82
- gs := convertSampleBack (sample , src .LocationIndices )
88
+ gs := convertSampleBack (src , sample , src .LocationIndices , addstr )
83
89
dst .Sample = append (dst .Sample , gs )
84
90
}
85
91
86
92
if len (dst .SampleType ) == 0 {
87
- dst .StringTable = append (dst .StringTable , "samples" )
88
- dst .StringTable = append (dst .StringTable , "ms" )
89
93
dst .SampleType = []* googleProfile.ValueType {{
90
- Type : int64 ( len ( dst . StringTable ) - 2 ),
91
- Unit : int64 ( len ( dst . StringTable ) - 1 ),
94
+ Type : addstr ( "samples" ),
95
+ Unit : addstr ( "ms" ),
92
96
}}
93
- dst .DefaultSampleType = int64 ( len ( dst . StringTable ) - 2 )
97
+ dst .DefaultSampleType = addstr ( "samples" )
94
98
}
95
99
96
100
return dst
@@ -147,14 +151,15 @@ func convertFunctionBack(of *otelProfile.Function) *googleProfile.Function {
147
151
}
148
152
}
149
153
150
- func convertSampleBack (os * otelProfile.Sample , locationIndexes []int64 ) * googleProfile.Sample {
154
+ func convertSampleBack (p * otelProfile. Profile , os * otelProfile.Sample , locationIndexes []int64 , addstr func ( s string ) int64 ) * googleProfile.Sample {
151
155
gs := & googleProfile.Sample {
152
156
Value : os .Value ,
153
157
}
154
158
155
159
if len (gs .Value ) == 0 {
156
160
gs .Value = []int64 {int64 (len (os .TimestampsUnixNano ))}
157
161
}
162
+ convertSampleAttributesToLabelsBack (p , os , gs , addstr )
158
163
159
164
for i := os .LocationsStartIndex ; i < os .LocationsStartIndex + os .LocationsLength ; i ++ {
160
165
gs .LocationId = append (gs .LocationId , uint64 (locationIndexes [i ]+ 1 ))
@@ -163,6 +168,19 @@ func convertSampleBack(os *otelProfile.Sample, locationIndexes []int64) *googleP
163
168
return gs
164
169
}
165
170
171
+ func convertSampleAttributesToLabelsBack (p * otelProfile.Profile , os * otelProfile.Sample , gs * googleProfile.Sample , addstr func (s string ) int64 ) {
172
+ gs .Label = make ([]* googleProfile.Label , 0 , len (os .Attributes ))
173
+ for _ , attribute := range os .Attributes {
174
+ att := p .AttributeTable [attribute ]
175
+ if att .Value .GetStringValue () != "" {
176
+ gs .Label = append (gs .Label , & googleProfile.Label {
177
+ Key : addstr (att .Key ),
178
+ Str : addstr (att .Value .GetStringValue ()),
179
+ })
180
+ }
181
+ }
182
+ }
183
+
166
184
// convertMappingsBack converts a slice of OpenTelemetry Mapping entries to Google Mapping entries.
167
185
func convertMappingsBack (otelMappings []* otelProfile.Mapping ) []* googleProfile.Mapping {
168
186
googleMappings := make ([]* googleProfile.Mapping , len (otelMappings ))
0 commit comments