|
| 1 | +//go:build integration && linux |
| 2 | + |
| 3 | +// Copyright The OpenTelemetry Authors |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package integrationtests |
| 7 | + |
| 8 | +import ( |
| 9 | + _ "embed" |
| 10 | + |
| 11 | + "context" |
| 12 | + "math" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | + "go.opentelemetry.io/ebpf-profiler/host" |
| 21 | + "go.opentelemetry.io/ebpf-profiler/libpf" |
| 22 | + "go.opentelemetry.io/ebpf-profiler/reporter" |
| 23 | + "go.opentelemetry.io/ebpf-profiler/tracer" |
| 24 | + tracertypes "go.opentelemetry.io/ebpf-profiler/tracer/types" |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + //go:embed pprof_1_23 |
| 29 | + pprof_1_23 []byte |
| 30 | + |
| 31 | + //go:embed pprof_1_24 |
| 32 | + pprof_1_24 []byte |
| 33 | + |
| 34 | + //go:embed pprof_1_24_cgo |
| 35 | + pprof_1_24_cgo []byte |
| 36 | +) |
| 37 | + |
| 38 | +type mockIntervals struct{} |
| 39 | + |
| 40 | +func (mockIntervals) MonitorInterval() time.Duration { return 1 * time.Second } |
| 41 | +func (mockIntervals) TracePollInterval() time.Duration { return 250 * time.Millisecond } |
| 42 | +func (mockIntervals) PIDCleanupInterval() time.Duration { return 1 * time.Second } |
| 43 | + |
| 44 | +type mockReporter struct{} |
| 45 | + |
| 46 | +func (mockReporter) ExecutableKnown(_ libpf.FileID) bool { return true } |
| 47 | +func (mockReporter) ExecutableMetadata(_ *reporter.ExecutableMetadataArgs) {} |
| 48 | + |
| 49 | +func isRoot() bool { |
| 50 | + return os.Geteuid() == 0 |
| 51 | +} |
| 52 | + |
| 53 | +func Test_Golabels(t *testing.T) { |
| 54 | + if !isRoot() { |
| 55 | + t.Skip("root privileges required") |
| 56 | + } |
| 57 | + |
| 58 | + tests := map[string]struct { |
| 59 | + bin []byte |
| 60 | + }{ |
| 61 | + "pprof_1_23": {bin: pprof_1_23}, |
| 62 | + "pprof_1_24": {bin: pprof_1_24}, |
| 63 | + "pprof_1_24_cgo": {bin: pprof_1_24_cgo}, |
| 64 | + } |
| 65 | + |
| 66 | + for name, tc := range tests { |
| 67 | + t.Run(name, func(t *testing.T) { |
| 68 | + exe, err := os.CreateTemp(t.TempDir(), name) |
| 69 | + if err != nil { |
| 70 | + t.Fatal(err) |
| 71 | + } |
| 72 | + defer os.Remove(exe.Name()) |
| 73 | + |
| 74 | + if _, err = exe.Write(tc.bin); err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + if err = exe.Close(); err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + |
| 81 | + if err = os.Chmod(exe.Name(), 0o755); err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + |
| 85 | + ctx, cancel := context.WithCancel(t.Context()) |
| 86 | + defer cancel() |
| 87 | + |
| 88 | + enabledTracers, _ := tracertypes.Parse("") |
| 89 | + enabledTracers.Enable(tracertypes.Labels) |
| 90 | + enabledTracers.Enable(tracertypes.GoTracer) |
| 91 | + |
| 92 | + trc, err := tracer.NewTracer(ctx, &tracer.Config{ |
| 93 | + Reporter: &mockReporter{}, |
| 94 | + Intervals: &mockIntervals{}, |
| 95 | + IncludeTracers: enabledTracers, |
| 96 | + SamplesPerSecond: 20, |
| 97 | + ProbabilisticInterval: 100, |
| 98 | + ProbabilisticThreshold: 100, |
| 99 | + OffCPUThreshold: uint32(math.MaxUint32 / 100), |
| 100 | + VerboseMode: true, |
| 101 | + }) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + trc.StartPIDEventProcessor(ctx) |
| 105 | + |
| 106 | + err = trc.AttachTracer() |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + t.Log("Attached tracer program") |
| 110 | + |
| 111 | + err = trc.EnableProfiling() |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + err = trc.AttachSchedMonitor() |
| 115 | + require.NoError(t, err) |
| 116 | + |
| 117 | + traceCh := make(chan *host.Trace) |
| 118 | + |
| 119 | + err = trc.StartMapMonitors(ctx, traceCh) |
| 120 | + require.NoError(t, err) |
| 121 | + |
| 122 | + go func() { |
| 123 | + if err := exec.CommandContext(ctx, exe.Name()).Run(); err != nil { |
| 124 | + t.Log(err) |
| 125 | + } |
| 126 | + }() |
| 127 | + |
| 128 | + for trace := range traceCh { |
| 129 | + if trace == nil { |
| 130 | + continue |
| 131 | + } |
| 132 | + if len(trace.CustomLabels) > 0 { |
| 133 | + hits := 0 |
| 134 | + for k, v := range trace.CustomLabels { |
| 135 | + if strings.HasPrefix(k, "l1") { |
| 136 | + require.Len(t, v, 22) |
| 137 | + require.True(t, strings.HasPrefix(v, "label1")) |
| 138 | + hits |= (1 << 0) |
| 139 | + } else if strings.HasPrefix(k, "l2") { |
| 140 | + require.Len(t, v, 30) |
| 141 | + require.True(t, strings.HasPrefix(v, "label2")) |
| 142 | + hits |= (1 << 1) |
| 143 | + } else if strings.HasPrefix(k, "l3") { |
| 144 | + require.Len(t, v, 47) |
| 145 | + require.True(t, strings.HasPrefix(v, "label3")) |
| 146 | + hits |= (1 << 2) |
| 147 | + } |
| 148 | + } |
| 149 | + if hits == (1<<0 | 1<<1 | 1<<2) { |
| 150 | + cancel() |
| 151 | + break |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments