Debian Codegraph @

consumer/consumertest/sink.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package consumertest // import "go.opentelemetry.io/collector/consumer/consumertest"
5
6 import (
7 "context"
8 "sync"
9
10 "go.opentelemetry.io/collector/consumer"
11 "go.opentelemetry.io/collector/consumer/xconsumer"
12 "go.opentelemetry.io/collector/pdata/plog"
13 "go.opentelemetry.io/collector/pdata/pmetric"
14 "go.opentelemetry.io/collector/pdata/pprofile"
15 "go.opentelemetry.io/collector/pdata/ptrace"
16 )
17
18 // TracesSink is a consumer.Traces that acts like a sink that
19 // stores all traces and allows querying them for testing.
20 type TracesSink struct {
21 nonMutatingConsumer
22 mu sync.Mutex
23 traces []ptrace.Traces
24 contexts []context.Context
25 spanCount int
26 }
27
28 var _ consumer.Traces = (*TracesSink)(nil)
29
30 // ConsumeTraces stores traces to this sink.
31 func (ste *TracesSink) ConsumeTraces(ctx context.Context, td ptrace.Traces) error {
32 ste.mu.Lock()
33 defer ste.mu.Unlock()
34
35 ste.traces = append(ste.traces, td)
36 ste.contexts = append(ste.contexts, ctx)
37 ste.spanCount += td.SpanCount()
38
39 return nil
40 }
41
42 // AllTraces returns the traces stored by this sink since last Reset.
43 func (ste *TracesSink) AllTraces() []ptrace.Traces {
44 ste.mu.Lock()
45 defer ste.mu.Unlock()
46
47 copyTraces := make([]ptrace.Traces, len(ste.traces))
48 copy(copyTraces, ste.traces)
49 return copyTraces
50 }
51
52 // Contexts returns the contexts stored by this sink since last Reset.
53 func (ste *TracesSink) Contexts() []context.Context {
54 ste.mu.Lock()
55 defer ste.mu.Unlock()
56
57 copyContexts := make([]context.Context, len(ste.contexts))
58 copy(copyContexts, ste.contexts)
59 return copyContexts
60 }
61
62 // SpanCount returns the number of spans sent to this sink.
63 func (ste *TracesSink) SpanCount() int {
64 ste.mu.Lock()
65 defer ste.mu.Unlock()
66 return ste.spanCount
67 }
68
69 // Reset deletes any stored data.
70 func (ste *TracesSink) Reset() {
71 ste.mu.Lock()
72 defer ste.mu.Unlock()
73
74 ste.traces = nil
75 ste.contexts = nil
76 ste.spanCount = 0
77 }
78
79 // MetricsSink is a consumer.Metrics that acts like a sink that
80 // stores all metrics and allows querying them for testing.
81 type MetricsSink struct {
82 nonMutatingConsumer
83 mu sync.Mutex
84 metrics []pmetric.Metrics
85 contexts []context.Context
86 dataPointCount int
87 }
88
89 var _ consumer.Metrics = (*MetricsSink)(nil)
90
91 // ConsumeMetrics stores metrics to this sink.
92 func (sme *MetricsSink) ConsumeMetrics(ctx context.Context, md pmetric.Metrics) error {
93 sme.mu.Lock()
94 defer sme.mu.Unlock()
95
96 sme.metrics = append(sme.metrics, md)
97 sme.contexts = append(sme.contexts, ctx)
98 sme.dataPointCount += md.DataPointCount()
99
100 return nil
101 }
102
103 // AllMetrics returns the metrics stored by this sink since last Reset.
104 func (sme *MetricsSink) AllMetrics() []pmetric.Metrics {
105 sme.mu.Lock()
106 defer sme.mu.Unlock()
107
108 copyMetrics := make([]pmetric.Metrics, len(sme.metrics))
109 copy(copyMetrics, sme.metrics)
110 return copyMetrics
111 }
112
113 // Contexts returns the contexts stored by this sink since last Reset.
114 func (sme *MetricsSink) Contexts() []context.Context {
115 sme.mu.Lock()
116 defer sme.mu.Unlock()
117
118 copyContexts := make([]context.Context, len(sme.contexts))
119 copy(copyContexts, sme.contexts)
120 return copyContexts
121 }
122
123 // DataPointCount returns the number of metrics stored by this sink since last Reset.
124 func (sme *MetricsSink) DataPointCount() int {
125 sme.mu.Lock()
126 defer sme.mu.Unlock()
127 return sme.dataPointCount
128 }
129
130 // Reset deletes any stored data.
131 func (sme *MetricsSink) Reset() {
132 sme.mu.Lock()
133 defer sme.mu.Unlock()
134
135 sme.metrics = nil
136 sme.contexts = nil
137 sme.dataPointCount = 0
138 }
139
140 // LogsSink is a consumer.Logs that acts like a sink that
141 // stores all logs and allows querying them for testing.
142 type LogsSink struct {
143 nonMutatingConsumer
144 mu sync.Mutex
145 logs []plog.Logs
146 contexts []context.Context
147 logRecordCount int
148 }
149
150 var _ consumer.Logs = (*LogsSink)(nil)
151
152 // ConsumeLogs stores logs to this sink.
153 func (sle *LogsSink) ConsumeLogs(ctx context.Context, ld plog.Logs) error {
154 sle.mu.Lock()
155 defer sle.mu.Unlock()
156
157 sle.logs = append(sle.logs, ld)
158 sle.logRecordCount += ld.LogRecordCount()
159 sle.contexts = append(sle.contexts, ctx)
160 return nil
161 }
162
163 // AllLogs returns the logs stored by this sink since last Reset.
164 func (sle *LogsSink) AllLogs() []plog.Logs {
165 sle.mu.Lock()
166 defer sle.mu.Unlock()
167
168 copyLogs := make([]plog.Logs, len(sle.logs))
169 copy(copyLogs, sle.logs)
170 return copyLogs
171 }
172
173 // LogRecordCount returns the number of log records stored by this sink since last Reset.
174 func (sle *LogsSink) LogRecordCount() int {
175 sle.mu.Lock()
176 defer sle.mu.Unlock()
177 return sle.logRecordCount
178 }
179
180 // Reset deletes any stored data.
181 func (sle *LogsSink) Reset() {
182 sle.mu.Lock()
183 defer sle.mu.Unlock()
184
185 sle.logs = nil
186 sle.contexts = nil
187 sle.logRecordCount = 0
188 }
189
190 // Contexts returns the contexts stored by this sink since last Reset.
191 func (sle *LogsSink) Contexts() []context.Context {
192 sle.mu.Lock()
193 defer sle.mu.Unlock()
194
195 copyContexts := make([]context.Context, len(sle.contexts))
196 copy(copyContexts, sle.contexts)
197 return copyContexts
198 }
199
200 // ProfilesSink is a xconsumer.Profiles that acts like a sink that
201 // stores all profiles and allows querying them for testing.
202 type ProfilesSink struct {
203 nonMutatingConsumer
204 mu sync.Mutex
205 profiles []pprofile.Profiles
206 contexts []context.Context
207 sampleCount int
208 }
209
210 var _ xconsumer.Profiles = (*ProfilesSink)(nil)
211
212 // ConsumeProfiles stores profiles to this sink.
213 func (ste *ProfilesSink) ConsumeProfiles(ctx context.Context, td pprofile.Profiles) error {
214 ste.mu.Lock()
215 defer ste.mu.Unlock()
216
217 ste.profiles = append(ste.profiles, td)
218 ste.contexts = append(ste.contexts, ctx)
219 ste.sampleCount += td.SampleCount()
220
221 return nil
222 }
223
224 // AllProfiles returns the profiles stored by this sink since last Reset.
225 func (ste *ProfilesSink) AllProfiles() []pprofile.Profiles {
226 ste.mu.Lock()
227 defer ste.mu.Unlock()
228
229 copyProfiles := make([]pprofile.Profiles, len(ste.profiles))
230 copy(copyProfiles, ste.profiles)
231 return copyProfiles
232 }
233
234 // SampleCount returns the number of profiles stored by this sink since last Reset.
235 func (ste *ProfilesSink) SampleCount() int {
236 ste.mu.Lock()
237 defer ste.mu.Unlock()
238 return ste.sampleCount
239 }
240
241 // Reset deletes any stored data.
242 func (ste *ProfilesSink) Reset() {
243 ste.mu.Lock()
244 defer ste.mu.Unlock()
245
246 ste.profiles = nil
247 ste.contexts = nil
248 ste.sampleCount = 0
249 }
250
251 // Contexts returns the contexts stored by this sink since last Reset.
252 func (ste *ProfilesSink) Contexts() []context.Context {
253 ste.mu.Lock()
254 defer ste.mu.Unlock()
255
256 copyContexts := make([]context.Context, len(ste.contexts))
257 copy(copyContexts, ste.contexts)
258 return copyContexts
259 }