@@ -2,10 +2,11 @@ package otlp
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
- "strings"
5
+ "os"
7
6
"testing"
8
7
8
+ phlaremodel "github.com/grafana/pyroscope/pkg/model"
9
+
9
10
"github.com/grafana/pyroscope/pkg/distributor/model"
10
11
11
12
"github.com/prometheus/prometheus/util/testutil"
@@ -14,7 +15,7 @@ import (
14
15
15
16
v1experimental2 "github.com/grafana/pyroscope/api/otlp/collector/profiles/v1development"
16
17
v1experimental "github.com/grafana/pyroscope/api/otlp/profiles/v1development"
17
- "github.com/grafana/pyroscope/pkg/og/convert/pprof/bench "
18
+ "github.com/grafana/pyroscope/pkg/og/convert/pprof/strprofile "
18
19
"github.com/grafana/pyroscope/pkg/test/mocks/mockotlp"
19
20
20
21
"github.com/stretchr/testify/assert"
@@ -160,6 +161,12 @@ func TestAppendAttributesUnique(t *testing.T) {
160
161
}
161
162
}
162
163
164
+ func readJSONFile (t * testing.T , filename string ) string {
165
+ data , err := os .ReadFile (filename )
166
+ require .NoError (t , err )
167
+ return string (data )
168
+ }
169
+
163
170
func TestSymbolizedFunctionNames (t * testing.T ) {
164
171
// Create two unsymbolized locations at 0x1e0 and 0x2f0
165
172
// Expect both of them to be present in the converted pprof
@@ -191,6 +198,7 @@ func TestSymbolizedFunctionNames(t *testing.T) {
191
198
LocationsLength : 2 ,
192
199
Value : []int64 {0xef },
193
200
}}
201
+ otlpb .profile .TimeNanos = 239
194
202
req := & v1experimental2.ExportProfilesServiceRequest {
195
203
ResourceProfiles : []* v1experimental.ResourceProfiles {{
196
204
ScopeProfiles : []* v1experimental.ScopeProfiles {{
@@ -201,17 +209,15 @@ func TestSymbolizedFunctionNames(t *testing.T) {
201
209
h := NewOTLPIngestHandler (svc , logger , false )
202
210
_ , err := h .Export (context .Background (), req )
203
211
assert .NoError (t , err )
204
- require .Equal (t , 1 , len (profiles ))
212
+ assert .Equal (t , 1 , len (profiles ))
205
213
206
214
gp := profiles [0 ].Series [0 ].Samples [0 ].Profile .Profile
207
215
208
- ss := bench .StackCollapseProtoWithOptions (gp , bench.StackCollapseOptions {
209
- ValueIdx : 0 ,
210
- Scale : 1 ,
211
- WithLabels : true ,
212
- })
213
- require .Equal (t , 1 , len (ss ))
214
- require .Equal (t , " ||| file1.so 0x2f0;file1.so 0x1e0 239" , ss [0 ])
216
+ jsonStr , err := strprofile .Stringify (gp , strprofile.Options {})
217
+ assert .NoError (t , err )
218
+ expectedJSON := readJSONFile (t , "testdata/TestSymbolizedFunctionNames.json" )
219
+ assert .JSONEq (t , expectedJSON , jsonStr )
220
+
215
221
}
216
222
217
223
func TestSampleAttributes (t * testing.T ) {
@@ -276,6 +282,7 @@ func TestSampleAttributes(t *testing.T) {
276
282
},
277
283
},
278
284
}}
285
+ otlpb .profile .TimeNanos = 239
279
286
req := & v1experimental2.ExportProfilesServiceRequest {
280
287
ResourceProfiles : []* v1experimental.ResourceProfiles {{
281
288
ScopeProfiles : []* v1experimental.ScopeProfiles {{
@@ -299,15 +306,11 @@ func TestSampleAttributes(t *testing.T) {
299
306
300
307
gp := profiles [0 ].Series [0 ].Samples [0 ].Profile .Profile
301
308
302
- ss := bench .StackCollapseProtoWithOptions (gp , bench.StackCollapseOptions {
303
- ValueIdx : 0 ,
304
- Scale : 1 ,
305
- WithLabels : true ,
306
- })
307
- fmt .Printf ("%s \n " , strings .Join (ss , "\n " ))
308
- require .Equal (t , 2 , len (ss ))
309
- assert .Equal (t , "(process = chrome) ||| chrome.so 0x4e;chrome.so 0x3e 61423" , ss [0 ])
310
- assert .Equal (t , "(process = firefox) ||| firefox.so 0x2e;firefox.so 0x1e 239" , ss [1 ])
309
+ jsonStr , err := strprofile .Stringify (gp , strprofile.Options {})
310
+ assert .NoError (t , err )
311
+ expectedJSON := readJSONFile (t , "testdata/TestSampleAttributes.json" )
312
+ assert .Equal (t , expectedJSON , jsonStr )
313
+
311
314
}
312
315
313
316
func TestDifferentServiceNames (t * testing.T ) {
@@ -438,7 +441,7 @@ func TestDifferentServiceNames(t *testing.T) {
438
441
UnitStrindex : otlpb .addstr ("nanoseconds" ),
439
442
}
440
443
otlpb .profile .Period = 10000000 // 10ms
441
-
444
+ otlpb . profile . TimeNanos = 239
442
445
req := & v1experimental2.ExportProfilesServiceRequest {
443
446
ResourceProfiles : []* v1experimental.ResourceProfiles {{
444
447
ScopeProfiles : []* v1experimental.ScopeProfiles {{
@@ -453,22 +456,18 @@ func TestDifferentServiceNames(t *testing.T) {
453
456
454
457
require .Equal (t , 3 , len (profiles ))
455
458
456
- expectedStacks := map [string ]string {
457
- "service-a" : " ||| serviceA_func2;serviceA_func1 1000000000 " ,
458
- "service-b" : " ||| serviceB_func2;serviceB_func1 2000000000 " ,
459
- "unknown" : " ||| serviceC_func3;serviceC_func3 7000000000 " ,
459
+ expectedProfiles := map [string ]string {
460
+ "{__name__= \" process_cpu \" , __delta__= \" false \" , __otel__= \" true \" , pyroscope_spy= \" unknown \" , service_name= \" service-a\" }" : "testdata/TestDifferentServiceNames_service_a_profile.json " ,
461
+ "{__name__= \" process_cpu \" , __delta__= \" false \" , __otel__= \" true \" , pyroscope_spy= \" unknown \" , service_name= \" service-b\" }" : "testdata/TestDifferentServiceNames_service_b_profile.json " ,
462
+ "{__name__= \" process_cpu \" , __delta__= \" false \" , __otel__= \" true \" , pyroscope_spy= \" unknown \" , service_name= \" unknown \" }" : "testdata/TestDifferentServiceNames_unknown_profile.json " ,
460
463
}
461
464
462
465
for _ , p := range profiles {
463
466
require .Equal (t , 1 , len (p .Series ))
464
- seriesLabelsMap := make (map [string ]string )
465
- for _ , label := range p .Series [0 ].Labels {
466
- seriesLabelsMap [label .Name ] = label .Value
467
- }
468
-
469
- serviceName := seriesLabelsMap ["service_name" ]
470
- require .Contains (t , []string {"service-a" , "service-b" , "unknown" }, serviceName )
471
- assert .NotContains (t , seriesLabelsMap , "service.name" )
467
+ series := phlaremodel .Labels (p .Series [0 ].Labels ).ToPrometheusLabels ().String ()
468
+ assert .Contains (t , expectedProfiles , series )
469
+ expectedJsonPath := expectedProfiles [series ]
470
+ expectedJson := readJSONFile (t , expectedJsonPath )
472
471
473
472
gp := p .Series [0 ].Samples [0 ].Profile .Profile
474
473
@@ -481,14 +480,11 @@ func TestDifferentServiceNames(t *testing.T) {
481
480
assert .Equal (t , "nanoseconds" , gp .StringTable [gp .PeriodType .Unit ])
482
481
assert .Equal (t , int64 (10000000 ), gp .Period )
483
482
484
- ss := bench .StackCollapseProtoWithOptions (gp , bench.StackCollapseOptions {
485
- ValueIdx : 0 ,
486
- Scale : 1 ,
487
- WithLabels : true ,
488
- })
489
- require .Equal (t , 1 , len (ss ))
490
- assert .Equal (t , expectedStacks [serviceName ], ss [0 ])
491
- assert .NotContains (t , ss [0 ], "service.name" )
483
+ jsonStr , err := strprofile .Stringify (gp , strprofile.Options {})
484
+ assert .NoError (t , err )
485
+ assert .JSONEq (t , expectedJson , jsonStr )
486
+ assert .NotContains (t , jsonStr , "service.name" )
487
+
492
488
}
493
489
}
494
490
0 commit comments