Skip to content

Commit

Permalink
fix(otel): samples-cpu conversion (#3795)
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev authored Dec 31, 2024
1 parent c32e5a6 commit 29c8f6f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 18 additions & 0 deletions pkg/ingester/otlp/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type profileBuilder struct {
unsymbolziedFuncNameMap map[string]uint64
locationMap map[*otelProfile.Location]uint64
mappingMap map[*otelProfile.Mapping]uint64
cpuConversion bool
}

func newProfileBuilder(src *otelProfile.Profile) *profileBuilder {
Expand Down Expand Up @@ -68,7 +69,22 @@ func newProfileBuilder(src *otelProfile.Profile) *profileBuilder {
Unit: res.addstr("ms"),
}}
res.dst.DefaultSampleType = res.addstr("samples")
} else if len(res.dst.SampleType) == 1 && res.dst.PeriodType != nil && res.dst.Period != 0 {
profileType := fmt.Sprintf("%s:%s:%s:%s",
res.dst.StringTable[res.dst.SampleType[0].Type],
res.dst.StringTable[res.dst.SampleType[0].Unit],
res.dst.StringTable[res.dst.PeriodType.Type],
res.dst.StringTable[res.dst.PeriodType.Unit],
)
if profileType == "samples:count:cpu:nanoseconds" {
res.dst.SampleType = []*googleProfile.ValueType{{
Type: res.addstr("cpu"),
Unit: res.addstr("nanoseconds"),
}}
res.cpuConversion = true
}
}

if res.dst.TimeNanos == 0 {
res.dst.TimeNanos = time.Now().UnixNano()
}
Expand Down Expand Up @@ -191,6 +207,8 @@ func (p *profileBuilder) convertSampleBack(os *otelProfile.Sample) *googleProfil

if len(gs.Value) == 0 {
gs.Value = []int64{int64(len(os.TimestampsUnixNano))}
} else if len(gs.Value) == 1 && p.cpuConversion {
gs.Value[0] *= p.src.Period
}
p.convertSampleAttributesToLabelsBack(os, gs)

Expand Down
10 changes: 5 additions & 5 deletions pkg/ingester/otlp/ingest_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,8 @@ func TestDifferentServiceNames(t *testing.T) {
}}

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

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

for _, p := range profiles {
Expand Down

0 comments on commit 29c8f6f

Please sign in to comment.