| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/otlp/otlpmetric/otest/client.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
// Package otest provides testing functionliaty for the otlpmetric exporters. |
| 8 |
package otest // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal/otest" |
| 9 |
|
| 10 |
import ( |
| 11 |
"context" |
| 12 |
"testing" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/google/go-cmp/cmp" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
"google.golang.org/protobuf/proto" |
| 19 |
|
| 20 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp/internal" |
| 21 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 22 |
collpb "go.opentelemetry.io/proto/otlp/collector/metrics/v1" |
| 23 |
cpb "go.opentelemetry.io/proto/otlp/common/v1" |
| 24 |
mpb "go.opentelemetry.io/proto/otlp/metrics/v1" |
| 25 |
rpb "go.opentelemetry.io/proto/otlp/resource/v1" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
// Sat Jan 01 2000 00:00:00 GMT+0000. |
| 30 |
start = time.Date(2000, time.January, 0o1, 0, 0, 0, 0, time.FixedZone("GMT", 0)) |
| 31 |
end = start.Add(30 * time.Second) |
| 32 |
|
| 33 |
kvAlice = &cpb.KeyValue{Key: "user", Value: &cpb.AnyValue{ |
| 34 |
Value: &cpb.AnyValue_StringValue{StringValue: "alice"}, |
| 35 |
}} |
| 36 |
kvBob = &cpb.KeyValue{Key: "user", Value: &cpb.AnyValue{ |
| 37 |
Value: &cpb.AnyValue_StringValue{StringValue: "bob"}, |
| 38 |
}} |
| 39 |
kvSrvName = &cpb.KeyValue{Key: "service.name", Value: &cpb.AnyValue{ |
| 40 |
Value: &cpb.AnyValue_StringValue{StringValue: "test server"}, |
| 41 |
}} |
| 42 |
kvSrvVer = &cpb.KeyValue{Key: "service.version", Value: &cpb.AnyValue{ |
| 43 |
Value: &cpb.AnyValue_StringValue{StringValue: "v0.1.0"}, |
| 44 |
}} |
| 45 |
|
| 46 |
mi, ma, sum = 2.0, 4.0, 90.0 |
| 47 |
hdp = []*mpb.HistogramDataPoint{ |
| 48 |
{ |
| 49 |
Attributes: []*cpb.KeyValue{kvAlice}, |
| 50 |
StartTimeUnixNano: uint64(start.UnixNano()), |
| 51 |
TimeUnixNano: uint64(end.UnixNano()), |
| 52 |
Count: 30, |
| 53 |
Sum: &sum, |
| 54 |
ExplicitBounds: []float64{1, 5}, |
| 55 |
BucketCounts: []uint64{0, 30, 0}, |
| 56 |
Min: &mi, |
| 57 |
Max: &ma, |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
hist = &mpb.Histogram{ |
| 62 |
AggregationTemporality: mpb.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, |
| 63 |
DataPoints: hdp, |
| 64 |
} |
| 65 |
|
| 66 |
dPtsInt64 = []*mpb.NumberDataPoint{ |
| 67 |
{ |
| 68 |
Attributes: []*cpb.KeyValue{kvAlice}, |
| 69 |
StartTimeUnixNano: uint64(start.UnixNano()), |
| 70 |
TimeUnixNano: uint64(end.UnixNano()), |
| 71 |
Value: &mpb.NumberDataPoint_AsInt{AsInt: 1}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
Attributes: []*cpb.KeyValue{kvBob}, |
| 75 |
StartTimeUnixNano: uint64(start.UnixNano()), |
| 76 |
TimeUnixNano: uint64(end.UnixNano()), |
| 77 |
Value: &mpb.NumberDataPoint_AsInt{AsInt: 2}, |
| 78 |
}, |
| 79 |
} |
| 80 |
dPtsFloat64 = []*mpb.NumberDataPoint{ |
| 81 |
{ |
| 82 |
Attributes: []*cpb.KeyValue{kvAlice}, |
| 83 |
StartTimeUnixNano: uint64(start.UnixNano()), |
| 84 |
TimeUnixNano: uint64(end.UnixNano()), |
| 85 |
Value: &mpb.NumberDataPoint_AsDouble{AsDouble: 1.0}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
Attributes: []*cpb.KeyValue{kvBob}, |
| 89 |
StartTimeUnixNano: uint64(start.UnixNano()), |
| 90 |
TimeUnixNano: uint64(end.UnixNano()), |
| 91 |
Value: &mpb.NumberDataPoint_AsDouble{AsDouble: 2.0}, |
| 92 |
}, |
| 93 |
} |
| 94 |
|
| 95 |
sumInt64 = &mpb.Sum{ |
| 96 |
AggregationTemporality: mpb.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, |
| 97 |
IsMonotonic: true, |
| 98 |
DataPoints: dPtsInt64, |
| 99 |
} |
| 100 |
sumFloat64 = &mpb.Sum{ |
| 101 |
AggregationTemporality: mpb.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, |
| 102 |
IsMonotonic: false, |
| 103 |
DataPoints: dPtsFloat64, |
| 104 |
} |
| 105 |
|
| 106 |
gaugeInt64 = &mpb.Gauge{DataPoints: dPtsInt64} |
| 107 |
gaugeFloat64 = &mpb.Gauge{DataPoints: dPtsFloat64} |
| 108 |
|
| 109 |
metrics = []*mpb.Metric{ |
| 110 |
{ |
| 111 |
Name: "int64-gauge", |
| 112 |
Description: "Gauge with int64 values", |
| 113 |
Unit: "1", |
| 114 |
Data: &mpb.Metric_Gauge{Gauge: gaugeInt64}, |
| 115 |
}, |
| 116 |
{ |
| 117 |
Name: "float64-gauge", |
| 118 |
Description: "Gauge with float64 values", |
| 119 |
Unit: "1", |
| 120 |
Data: &mpb.Metric_Gauge{Gauge: gaugeFloat64}, |
| 121 |
}, |
| 122 |
{ |
| 123 |
Name: "int64-sum", |
| 124 |
Description: "Sum with int64 values", |
| 125 |
Unit: "1", |
| 126 |
Data: &mpb.Metric_Sum{Sum: sumInt64}, |
| 127 |
}, |
| 128 |
{ |
| 129 |
Name: "float64-sum", |
| 130 |
Description: "Sum with float64 values", |
| 131 |
Unit: "1", |
| 132 |
Data: &mpb.Metric_Sum{Sum: sumFloat64}, |
| 133 |
}, |
| 134 |
{ |
| 135 |
Name: "histogram", |
| 136 |
Description: "Histogram", |
| 137 |
Unit: "1", |
| 138 |
Data: &mpb.Metric_Histogram{Histogram: hist}, |
| 139 |
}, |
| 140 |
} |
| 141 |
|
| 142 |
scope = &cpb.InstrumentationScope{ |
| 143 |
Name: "test/code/path", |
| 144 |
Version: "v0.1.0", |
| 145 |
} |
| 146 |
scopeMetrics = []*mpb.ScopeMetrics{ |
| 147 |
{ |
| 148 |
Scope: scope, |
| 149 |
Metrics: metrics, |
| 150 |
SchemaUrl: semconv.SchemaURL, |
| 151 |
}, |
| 152 |
} |
| 153 |
|
| 154 |
res = &rpb.Resource{ |
| 155 |
Attributes: []*cpb.KeyValue{kvSrvName, kvSrvVer}, |
| 156 |
} |
| 157 |
resourceMetrics = &mpb.ResourceMetrics{ |
| 158 |
Resource: res, |
| 159 |
ScopeMetrics: scopeMetrics, |
| 160 |
SchemaUrl: semconv.SchemaURL, |
| 161 |
} |
| 162 |
) |
| 163 |
|
| 164 |
type Client interface { |
| 165 |
UploadMetrics(context.Context, *mpb.ResourceMetrics) error |
| 166 |
ForceFlush(context.Context) error |
| 167 |
Shutdown(context.Context) error |
| 168 |
} |
| 169 |
|
| 170 |
// ClientFactory is a function that when called returns a |
| 171 |
// Client implementation that is connected to also returned |
| 172 |
// Collector implementation. The Client is ready to upload metric data to the |
| 173 |
// Collector which is ready to store that data. |
| 174 |
// |
| 175 |
// If resultCh is not nil, the returned Collector needs to use the responses |
| 176 |
// from that channel to send back to the client for every export request. |
| 177 |
type ClientFactory func(resultCh <-chan ExportResult) (Client, Collector) |
| 178 |
|
| 179 |
// RunClientTests runs a suite of Client integration tests. For example: |
| 180 |
// |
| 181 |
// t.Run("Integration", RunClientTests(factory)) |
| 182 |
func RunClientTests(f ClientFactory) func(*testing.T) { |
| 183 |
return func(t *testing.T) { |
| 184 |
t.Run("ClientHonorsContextErrors", func(t *testing.T) { |
| 185 |
t.Run("Shutdown", testCtxErrs(func() func(context.Context) error { |
| 186 |
c, _ := f(nil) |
| 187 |
return c.Shutdown |
| 188 |
})) |
| 189 |
|
| 190 |
t.Run("ForceFlush", testCtxErrs(func() func(context.Context) error { |
| 191 |
c, _ := f(nil) |
| 192 |
return c.ForceFlush |
| 193 |
})) |
| 194 |
|
| 195 |
t.Run("UploadMetrics", testCtxErrs(func() func(context.Context) error { |
| 196 |
c, _ := f(nil) |
| 197 |
return func(ctx context.Context) error { |
| 198 |
return c.UploadMetrics(ctx, nil) |
| 199 |
} |
| 200 |
})) |
| 201 |
}) |
| 202 |
|
| 203 |
t.Run("ForceFlushFlushes", func(t *testing.T) { |
| 204 |
ctx := context.Background() |
| 205 |
client, collector := f(nil) |
| 206 |
require.NoError(t, client.UploadMetrics(ctx, resourceMetrics)) |
| 207 |
|
| 208 |
require.NoError(t, client.ForceFlush(ctx)) |
| 209 |
rm := collector.Collect().Dump() |
| 210 |
// Data correctness is not important, just it was received. |
| 211 |
require.NotEmpty(t, rm, "no data uploaded") |
| 212 |
|
| 213 |
require.NoError(t, client.Shutdown(ctx)) |
| 214 |
rm = collector.Collect().Dump() |
| 215 |
assert.Empty(t, rm, "client did not flush all data") |
| 216 |
}) |
| 217 |
|
| 218 |
t.Run("UploadMetrics", func(t *testing.T) { |
| 219 |
ctx := context.Background() |
| 220 |
client, coll := f(nil) |
| 221 |
|
| 222 |
require.NoError(t, client.UploadMetrics(ctx, resourceMetrics)) |
| 223 |
require.NoError(t, client.Shutdown(ctx)) |
| 224 |
got := coll.Collect().Dump() |
| 225 |
require.Len(t, got, 1, "upload of one ResourceMetrics") |
| 226 |
diff := cmp.Diff(got[0], resourceMetrics, cmp.Comparer(proto.Equal)) |
| 227 |
if diff != "" { |
| 228 |
t.Fatalf("unexpected ResourceMetrics:\n%s", diff) |
| 229 |
} |
| 230 |
}) |
| 231 |
|
| 232 |
t.Run("PartialSuccess", func(t *testing.T) { |
| 233 |
const n, msg = 2, "bad data" |
| 234 |
rCh := make(chan ExportResult, 3) |
| 235 |
rCh <- ExportResult{ |
| 236 |
Response: &collpb.ExportMetricsServiceResponse{ |
| 237 |
PartialSuccess: &collpb.ExportMetricsPartialSuccess{ |
| 238 |
RejectedDataPoints: n, |
| 239 |
ErrorMessage: msg, |
| 240 |
}, |
| 241 |
}, |
| 242 |
} |
| 243 |
rCh <- ExportResult{ |
| 244 |
Response: &collpb.ExportMetricsServiceResponse{ |
| 245 |
PartialSuccess: &collpb.ExportMetricsPartialSuccess{ |
| 246 |
// Should not be logged. |
| 247 |
RejectedDataPoints: 0, |
| 248 |
ErrorMessage: "", |
| 249 |
}, |
| 250 |
}, |
| 251 |
} |
| 252 |
rCh <- ExportResult{ |
| 253 |
Response: &collpb.ExportMetricsServiceResponse{}, |
| 254 |
} |
| 255 |
|
| 256 |
ctx := context.Background() |
| 257 |
client, _ := f(rCh) |
| 258 |
|
| 259 |
wantErr := internal.MetricPartialSuccessError(0, "") |
| 260 |
assert.ErrorIs(t, client.UploadMetrics(ctx, resourceMetrics), wantErr) |
| 261 |
assert.NoError(t, client.UploadMetrics(ctx, resourceMetrics)) |
| 262 |
assert.NoError(t, client.UploadMetrics(ctx, resourceMetrics)) |
| 263 |
assert.NoError(t, client.Shutdown(ctx)) |
| 264 |
}) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
func testCtxErrs(factory func() func(context.Context) error) func(t *testing.T) { |
| 269 |
return func(t *testing.T) { |
| 270 |
t.Helper() |
| 271 |
ctx, cancel := context.WithCancel(context.Background()) |
| 272 |
t.Cleanup(cancel) |
| 273 |
|
| 274 |
t.Run("DeadlineExceeded", func(t *testing.T) { |
| 275 |
innerCtx, innerCancel := context.WithTimeout(ctx, time.Nanosecond) |
| 276 |
t.Cleanup(innerCancel) |
| 277 |
<-innerCtx.Done() |
| 278 |
|
| 279 |
f := factory() |
| 280 |
assert.ErrorIs(t, f(innerCtx), context.DeadlineExceeded) |
| 281 |
}) |
| 282 |
|
| 283 |
t.Run("Canceled", func(t *testing.T) { |
| 284 |
innerCtx, innerCancel := context.WithCancel(ctx) |
| 285 |
innerCancel() |
| 286 |
|
| 287 |
f := factory() |
| 288 |
assert.ErrorIs(t, f(innerCtx), context.Canceled) |
| 289 |
}) |
| 290 |
} |
| 291 |
} |