Skip to content

Commit a982681

Browse files
committed
fix(otel): samples-cpu conversion
1 parent 2752bae commit a982681

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

pkg/ingester/otlp/convert.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type profileBuilder struct {
3939
unsymbolziedFuncNameMap map[string]uint64
4040
locationMap map[*otelProfile.Location]uint64
4141
mappingMap map[*otelProfile.Mapping]uint64
42+
cpuConversion bool
4243
}
4344

4445
func newProfileBuilder(src *otelProfile.Profile) *profileBuilder {
@@ -68,7 +69,22 @@ func newProfileBuilder(src *otelProfile.Profile) *profileBuilder {
6869
Unit: res.addstr("ms"),
6970
}}
7071
res.dst.DefaultSampleType = res.addstr("samples")
72+
} else if len(res.dst.SampleType) == 1 && res.dst.PeriodType != nil && res.dst.Period != 0 {
73+
profileType := fmt.Sprintf("%s:%s:%s:%s",
74+
res.dst.StringTable[res.dst.SampleType[0].Type],
75+
res.dst.StringTable[res.dst.SampleType[0].Unit],
76+
res.dst.StringTable[res.dst.PeriodType.Type],
77+
res.dst.StringTable[res.dst.PeriodType.Unit],
78+
)
79+
if profileType == "samples:count:cpu:nanoseconds" {
80+
res.dst.SampleType = []*googleProfile.ValueType{{
81+
Type: res.addstr("cpu"),
82+
Unit: res.addstr("nanoseconds"),
83+
}}
84+
res.cpuConversion = true
85+
}
7186
}
87+
7288
if res.dst.TimeNanos == 0 {
7389
res.dst.TimeNanos = time.Now().UnixNano()
7490
}
@@ -191,6 +207,8 @@ func (p *profileBuilder) convertSampleBack(os *otelProfile.Sample) *googleProfil
191207

192208
if len(gs.Value) == 0 {
193209
gs.Value = []int64{int64(len(os.TimestampsUnixNano))}
210+
} else if len(gs.Value) == 1 && p.cpuConversion {
211+
gs.Value[0] *= p.src.Period
194212
}
195213
p.convertSampleAttributesToLabelsBack(os, gs)
196214

pkg/ingester/otlp/ingest_handler_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ func TestDifferentServiceNames(t *testing.T) {
430430
}}
431431

432432
otlpb.profile.SampleType = []*v1experimental.ValueType{{
433-
Type: otlpb.addstr("cpu"),
434-
Unit: otlpb.addstr("nanoseconds"),
433+
Type: otlpb.addstr("samples"),
434+
Unit: otlpb.addstr("count"),
435435
}}
436436
otlpb.profile.PeriodType = &v1experimental.ValueType{
437437
Type: otlpb.addstr("cpu"),
@@ -454,9 +454,9 @@ func TestDifferentServiceNames(t *testing.T) {
454454
require.Equal(t, 3, len(profiles))
455455

456456
expectedStacks := map[string]string{
457-
"service-a": " ||| serviceA_func2;serviceA_func1 100",
458-
"service-b": " ||| serviceB_func2;serviceB_func1 200",
459-
"unknown": " ||| serviceC_func3;serviceC_func3 700",
457+
"service-a": " ||| serviceA_func2;serviceA_func1 1000000000",
458+
"service-b": " ||| serviceB_func2;serviceB_func1 2000000000",
459+
"unknown": " ||| serviceC_func3;serviceC_func3 7000000000",
460460
}
461461

462462
for _, p := range profiles {

0 commit comments

Comments
 (0)