Debian Codegraph @

instrumentation/runtime/runtime_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package runtime // import "go.opentelemetry.io/contrib/instrumentation/runtime"
5
6 import (
7 "math"
8 "runtime/debug"
9 "testing"
10 "time"
11
12 "github.com/stretchr/testify/assert"
13 "github.com/stretchr/testify/require"
14 "go.opentelemetry.io/otel/attribute"
15 "go.opentelemetry.io/otel/sdk/instrumentation"
16 "go.opentelemetry.io/otel/sdk/metric"
17 "go.opentelemetry.io/otel/sdk/metric/metricdata"
18 "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
19 "go.opentelemetry.io/otel/semconv/v1.40.0/goconv"
20 )
21
22 func TestRefreshGoCollector(t *testing.T) {
23 // buffer for allocating memory
24 var buffer [][]byte
25 collector := newCollector(10*time.Second, runtimeMetrics)
26 testClock := newClock()
27 collector.now = testClock.now
28 // before the first refresh, all counters are zero
29 assert.Zero(t, collector.getInt(goMemoryAllocations))
30 // after the first refresh, counters are non-zero
31 buffer = allocateMemory(buffer)
32 collector.refresh()
33 initialAllocations := collector.getInt(goMemoryAllocations)
34 assert.NotZero(t, initialAllocations)
35 // if less than the refresh time has elapsed, the value is not updated
36 // on refresh.
37 testClock.increment(9 * time.Second)
38 collector.refresh()
39 buffer = allocateMemory(buffer)
40 assert.Equal(t, initialAllocations, collector.getInt(goMemoryAllocations))
41 // if greater than the refresh time has elapsed, the value changes.
42 testClock.increment(2 * time.Second)
43 collector.refresh()
44 _ = allocateMemory(buffer)
45 assert.NotEqual(t, initialAllocations, collector.getInt(goMemoryAllocations))
46 }
47
48 func newClock() *clock {
49 return &clock{current: time.Now()}
50 }
51
52 type clock struct {
53 current time.Time
54 }
55
56 func (c *clock) now() time.Time { return c.current }
57
58 func (c *clock) increment(d time.Duration) { c.current = c.current.Add(d) }
59
60 func TestRuntimeWithLimit(t *testing.T) {
61 // buffer for allocating memory
62 var buffer [][]byte
63 _ = allocateMemory(buffer)
64 debug.SetMemoryLimit(1234567890)
65 // reset to default
66 defer debug.SetMemoryLimit(math.MaxInt64)
67
68 reader := metric.NewManualReader()
69 mp := metric.NewMeterProvider(metric.WithReader(reader))
70 err := Start(WithMeterProvider(mp))
71 assert.NoError(t, err)
72 rm := metricdata.ResourceMetrics{}
73 err = reader.Collect(t.Context(), &rm)
74 assert.NoError(t, err)
75 require.Len(t, rm.ScopeMetrics, 1)
76 require.Len(t, rm.ScopeMetrics[0].Metrics, 8)
77
78 expectedScopeMetric := metricdata.ScopeMetrics{
79 Scope: instrumentation.Scope{
80 Name: "go.opentelemetry.io/contrib/instrumentation/runtime",
81 Version: Version,
82 },
83 Metrics: []metricdata.Metrics{
84 {
85 Name: goconv.MemoryUsed{}.Name(),
86 Description: goconv.MemoryUsed{}.Description(),
87 Unit: goconv.MemoryUsed{}.Unit(),
88 Data: metricdata.Sum[int64]{
89 Temporality: metricdata.CumulativeTemporality,
90 IsMonotonic: false,
91 DataPoints: []metricdata.DataPoint[int64]{
92 {
93 Attributes: attribute.NewSet(
94 goconv.MemoryUsed{}.AttrMemoryType(goconv.MemoryTypeStack),
95 ),
96 },
97 {
98 Attributes: attribute.NewSet(
99 goconv.MemoryUsed{}.AttrMemoryType(goconv.MemoryTypeOther),
100 ),
101 },
102 },
103 },
104 },
105 {
106 Name: goconv.MemoryLimit{}.Name(),
107 Description: goconv.MemoryLimit{}.Description(),
108 Unit: goconv.MemoryLimit{}.Unit(),
109 Data: metricdata.Sum[int64]{
110 Temporality: metricdata.CumulativeTemporality,
111 IsMonotonic: false,
112 DataPoints: []metricdata.DataPoint[int64]{{}},
113 },
114 },
115 {
116 Name: goconv.MemoryAllocated{}.Name(),
117 Description: goconv.MemoryAllocated{}.Description(),
118 Unit: goconv.MemoryAllocated{}.Unit(),
119 Data: metricdata.Sum[int64]{
120 Temporality: metricdata.CumulativeTemporality,
121 IsMonotonic: true,
122 DataPoints: []metricdata.DataPoint[int64]{{}},
123 },
124 },
125 {
126 Name: goconv.MemoryAllocations{}.Name(),
127 Description: goconv.MemoryAllocations{}.Description(),
128 Unit: goconv.MemoryAllocations{}.Unit(),
129 Data: metricdata.Sum[int64]{
130 Temporality: metricdata.CumulativeTemporality,
131 IsMonotonic: true,
132 DataPoints: []metricdata.DataPoint[int64]{{}},
133 },
134 },
135 {
136 Name: goconv.MemoryGCGoal{}.Name(),
137 Description: goconv.MemoryGCGoal{}.Description(),
138 Unit: goconv.MemoryGCGoal{}.Unit(),
139 Data: metricdata.Sum[int64]{
140 Temporality: metricdata.CumulativeTemporality,
141 IsMonotonic: false,
142 DataPoints: []metricdata.DataPoint[int64]{{}},
143 },
144 },
145 {
146 Name: goconv.GoroutineCount{}.Name(),
147 Description: goconv.GoroutineCount{}.Description(),
148 Unit: goconv.GoroutineCount{}.Unit(),
149 Data: metricdata.Sum[int64]{
150 Temporality: metricdata.CumulativeTemporality,
151 IsMonotonic: false,
152 DataPoints: []metricdata.DataPoint[int64]{{}},
153 },
154 },
155 {
156 Name: goconv.ProcessorLimit{}.Name(),
157 Description: goconv.ProcessorLimit{}.Description(),
158 Unit: goconv.ProcessorLimit{}.Unit(),
159 Data: metricdata.Sum[int64]{
160 Temporality: metricdata.CumulativeTemporality,
161 IsMonotonic: false,
162 DataPoints: []metricdata.DataPoint[int64]{{}},
163 },
164 },
165 {
166 Name: goconv.ConfigGogc{}.Name(),
167 Description: goconv.ConfigGogc{}.Description(),
168 Unit: goconv.ConfigGogc{}.Unit(),
169 Data: metricdata.Sum[int64]{
170 Temporality: metricdata.CumulativeTemporality,
171 IsMonotonic: false,
172 DataPoints: []metricdata.DataPoint[int64]{{}},
173 },
174 },
175 },
176 }
177 metricdatatest.AssertEqual(t, expectedScopeMetric, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue())
178 assertNonZeroValues(t, rm.ScopeMetrics[0])
179 }
180
181 func assertNonZeroValues(t *testing.T, sm metricdata.ScopeMetrics) {
182 for _, m := range sm.Metrics {
183 switch a := m.Data.(type) {
184 case metricdata.Sum[int64]:
185 for _, dp := range a.DataPoints {
186 assert.Positivef(t, dp.Value, "Metric %q should have a non-zero value for point with attributes %+v", m.Name, dp.Attributes)
187 }
188 default:
189 t.Fatalf("unexpected data type %v", a)
190 }
191 }
192 }
193
194 func allocateMemory(buffer [][]byte) [][]byte {
195 return append(buffer, make([]byte, 1000000))
196 }