|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//go:build amd64 && !integration |
| 5 | + |
| 6 | +package rtld_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + "unsafe" |
| 13 | + |
| 14 | + "github.com/coreos/pkg/dlopen" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "go.opentelemetry.io/ebpf-profiler/metrics" |
| 17 | + "go.opentelemetry.io/ebpf-profiler/support" |
| 18 | + "go.opentelemetry.io/ebpf-profiler/testutils" |
| 19 | + "go.opentelemetry.io/ebpf-profiler/tracer" |
| 20 | + tracertypes "go.opentelemetry.io/ebpf-profiler/tracer/types" |
| 21 | +) |
| 22 | + |
| 23 | +func TestIntegration(t *testing.T) { |
| 24 | + if !testutils.IsRoot() { |
| 25 | + t.Skip("This test requires root privileges") |
| 26 | + } |
| 27 | + |
| 28 | + // Create a context for the tracer |
| 29 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 30 | + defer cancel() |
| 31 | + |
| 32 | + // Start the tracer with all tracers enabled |
| 33 | + traceCh, trc := testutils.StartTracer(ctx, t, |
| 34 | + tracertypes.AllTracers(), |
| 35 | + &testutils.MockReporter{}, |
| 36 | + false) |
| 37 | + defer trc.Close() |
| 38 | + |
| 39 | + // Consume traces to prevent blocking |
| 40 | + go func() { |
| 41 | + for { |
| 42 | + select { |
| 43 | + case <-ctx.Done(): |
| 44 | + return |
| 45 | + case <-traceCh: |
| 46 | + // Discard traces |
| 47 | + } |
| 48 | + } |
| 49 | + }() |
| 50 | + |
| 51 | + // retry a few times to get the metric, our process has to be detected and |
| 52 | + // the rtld interpreter has to attach. |
| 53 | + require.Eventually(t, func() bool { |
| 54 | + // Get the initial metric value |
| 55 | + initialCount := getEBPFMetricValue(trc, metrics.IDRtldMapCompleteHits) |
| 56 | + t.Logf("Initial rtld:map_complete metric count: %d", initialCount) |
| 57 | + |
| 58 | + // Use dlopen to load a shared library |
| 59 | + // libm is a standard math library that's always present |
| 60 | + lib, err := dlopen.GetHandle([]string{ |
| 61 | + "/lib/x86_64-linux-gnu/libm.so.6", |
| 62 | + "libm.so.6", |
| 63 | + }) |
| 64 | + require.NoError(t, err, "Failed to open libm.so.6") |
| 65 | + defer lib.Close() |
| 66 | + |
| 67 | + // Get the metrics after dlopen |
| 68 | + finalCount := getEBPFMetricValue(trc, metrics.IDRtldMapCompleteHits) |
| 69 | + t.Logf("Final rtld:map_complete metric count: %d", finalCount) |
| 70 | + |
| 71 | + // Check that the metric was incremented |
| 72 | + return finalCount > initialCount |
| 73 | + }, 10*time.Second, 50*time.Millisecond) |
| 74 | +} |
| 75 | + |
| 76 | +func getEBPFMetricValue(trc *tracer.Tracer, metricID metrics.MetricID) uint64 { |
| 77 | + // Access the eBPF maps directly using the public method |
| 78 | + ebpfMaps := trc.GetEbpfMaps() |
| 79 | + metricsMap, ok := ebpfMaps["metrics"] |
| 80 | + if !ok { |
| 81 | + return 0 |
| 82 | + } |
| 83 | + |
| 84 | + // Find the eBPF metric ID that corresponds to our metrics.MetricID |
| 85 | + var ebpfMetricID uint32 |
| 86 | + for ebpfID, id := range support.MetricsTranslation { |
| 87 | + if id == metricID { |
| 88 | + ebpfMetricID = uint32(ebpfID) |
| 89 | + break |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Read the per-CPU values |
| 94 | + var perCPUValues []uint64 |
| 95 | + if err := metricsMap.Lookup(unsafe.Pointer(&ebpfMetricID), &perCPUValues); err != nil { |
| 96 | + return 0 |
| 97 | + } |
| 98 | + |
| 99 | + // Sum all per-CPU values |
| 100 | + var total uint64 |
| 101 | + for _, val := range perCPUValues { |
| 102 | + total += val |
| 103 | + } |
| 104 | + return total |
| 105 | +} |
0 commit comments