| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package 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 |
|
| 19 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 63 |
func (ste *TracesSink) SpanCount() int { |
| 64 |
ste.mu.Lock() |
| 65 |
defer ste.mu.Unlock() |
| 66 |
return ste.spanCount |
| 67 |
} |
| 68 |
|
| 69 |
|
| 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 |
|
| 80 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 124 |
func (sme *MetricsSink) DataPointCount() int { |
| 125 |
sme.mu.Lock() |
| 126 |
defer sme.mu.Unlock() |
| 127 |
return sme.dataPointCount |
| 128 |
} |
| 129 |
|
| 130 |
|
| 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 |
|
| 141 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 174 |
func (sle *LogsSink) LogRecordCount() int { |
| 175 |
sle.mu.Lock() |
| 176 |
defer sle.mu.Unlock() |
| 177 |
return sle.logRecordCount |
| 178 |
} |
| 179 |
|
| 180 |
|
| 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 |
|
| 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 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 235 |
func (ste *ProfilesSink) SampleCount() int { |
| 236 |
ste.mu.Lock() |
| 237 |
defer ste.mu.Unlock() |
| 238 |
return ste.sampleCount |
| 239 |
} |
| 240 |
|
| 241 |
|
| 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 |
|
| 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 |
} |