Skip to content

Commit e00f13a

Browse files
committed
fix(otel): samples-cpu conversion
1 parent 2f18f58 commit e00f13a

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ func TestDifferentServiceNames(t *testing.T) {
416416

417417
// Add sample types and period type
418418
otlpb.profile.SampleType = []*v1experimental.ValueType{{
419-
Type: otlpb.addstr("cpu"),
420-
Unit: otlpb.addstr("nanoseconds"),
419+
Type: otlpb.addstr("samples"),
420+
Unit: otlpb.addstr("count"),
421421
}}
422422
otlpb.profile.PeriodType = &v1experimental.ValueType{
423423
Type: otlpb.addstr("cpu"),
@@ -441,8 +441,8 @@ func TestDifferentServiceNames(t *testing.T) {
441441
require.Equal(t, 2, len(profiles))
442442

443443
expectedStacks := map[string]string{
444-
"service-a": " ||| serviceA_func2;serviceA_func1 100",
445-
"service-b": " ||| serviceB_func2;serviceB_func1 200",
444+
"service-a": " ||| serviceA_func2;serviceA_func1 1000000000",
445+
"service-b": " ||| serviceB_func2;serviceB_func1 2000000000",
446446
}
447447

448448
// Verify service names, stacktraces, and profile metadata in the profiles

0 commit comments

Comments
 (0)