|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package otlptest |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "time" |
| 21 | + |
| 22 | + "go.opentelemetry.io/otel/codes" |
| 23 | + "go.opentelemetry.io/otel/label" |
| 24 | + "go.opentelemetry.io/otel/metric" |
| 25 | + "go.opentelemetry.io/otel/metric/number" |
| 26 | + exportmetric "go.opentelemetry.io/otel/sdk/export/metric" |
| 27 | + exporttrace "go.opentelemetry.io/otel/sdk/export/trace" |
| 28 | + "go.opentelemetry.io/otel/sdk/instrumentation" |
| 29 | + "go.opentelemetry.io/otel/sdk/metric/aggregator/sum" |
| 30 | + "go.opentelemetry.io/otel/sdk/resource" |
| 31 | + "go.opentelemetry.io/otel/trace" |
| 32 | +) |
| 33 | + |
| 34 | +// Used to avoid implementing locking functions for test |
| 35 | +// checkpointsets. |
| 36 | +type noopLocker struct{} |
| 37 | + |
| 38 | +// Lock implements sync.Locker, which is needed for |
| 39 | +// exportmetric.CheckpointSet. |
| 40 | +func (noopLocker) Lock() {} |
| 41 | + |
| 42 | +// Unlock implements sync.Locker, which is needed for |
| 43 | +// exportmetric.CheckpointSet. |
| 44 | +func (noopLocker) Unlock() {} |
| 45 | + |
| 46 | +// RLock implements exportmetric.CheckpointSet. |
| 47 | +func (noopLocker) RLock() {} |
| 48 | + |
| 49 | +// RUnlock implements exportmetric.CheckpointSet. |
| 50 | +func (noopLocker) RUnlock() {} |
| 51 | + |
| 52 | +// OneRecordCheckpointSet is a CheckpointSet that returns just one |
| 53 | +// filled record. It may be useful for testing driver's metrics |
| 54 | +// export. |
| 55 | +type OneRecordCheckpointSet struct { |
| 56 | + noopLocker |
| 57 | +} |
| 58 | + |
| 59 | +var _ exportmetric.CheckpointSet = OneRecordCheckpointSet{} |
| 60 | + |
| 61 | +// ForEach implements exportmetric.CheckpointSet. It always invokes |
| 62 | +// the callback once with always the same record. |
| 63 | +func (OneRecordCheckpointSet) ForEach(kindSelector exportmetric.ExportKindSelector, recordFunc func(exportmetric.Record) error) error { |
| 64 | + desc := metric.NewDescriptor( |
| 65 | + "foo", |
| 66 | + metric.CounterInstrumentKind, |
| 67 | + number.Int64Kind, |
| 68 | + ) |
| 69 | + res := resource.NewWithAttributes(label.String("a", "b")) |
| 70 | + agg := sum.New(1) |
| 71 | + if err := agg[0].Update(context.Background(), number.NewInt64Number(42), &desc); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + start := time.Date(2020, time.December, 8, 19, 15, 0, 0, time.UTC) |
| 75 | + end := time.Date(2020, time.December, 8, 19, 16, 0, 0, time.UTC) |
| 76 | + labels := label.NewSet(label.String("abc", "def"), label.Int64("one", 1)) |
| 77 | + rec := exportmetric.NewRecord(&desc, &labels, res, agg[0].Aggregation(), start, end) |
| 78 | + return recordFunc(rec) |
| 79 | +} |
| 80 | + |
| 81 | +// SingleSpanSnapshot returns a one-element slice with a snapshot. It |
| 82 | +// may be useful for testing driver's trace export. |
| 83 | +func SingleSpanSnapshot() []*exporttrace.SpanSnapshot { |
| 84 | + sd := &exporttrace.SpanSnapshot{ |
| 85 | + SpanContext: trace.SpanContext{ |
| 86 | + TraceID: trace.TraceID{2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 87 | + SpanID: trace.SpanID{3, 4, 5, 6, 7, 8, 9, 0}, |
| 88 | + TraceFlags: trace.FlagsSampled, |
| 89 | + }, |
| 90 | + ParentSpanID: trace.SpanID{1, 2, 3, 4, 5, 6, 7, 8}, |
| 91 | + SpanKind: trace.SpanKindInternal, |
| 92 | + Name: "foo", |
| 93 | + StartTime: time.Date(2020, time.December, 8, 20, 23, 0, 0, time.UTC), |
| 94 | + EndTime: time.Date(2020, time.December, 0, 20, 24, 0, 0, time.UTC), |
| 95 | + Attributes: []label.KeyValue{}, |
| 96 | + MessageEvents: []exporttrace.Event{}, |
| 97 | + Links: []trace.Link{}, |
| 98 | + StatusCode: codes.Ok, |
| 99 | + StatusMessage: "", |
| 100 | + HasRemoteParent: false, |
| 101 | + DroppedAttributeCount: 0, |
| 102 | + DroppedMessageEventCount: 0, |
| 103 | + DroppedLinkCount: 0, |
| 104 | + ChildSpanCount: 0, |
| 105 | + Resource: resource.NewWithAttributes(label.String("a", "b")), |
| 106 | + InstrumentationLibrary: instrumentation.Library{ |
| 107 | + Name: "bar", |
| 108 | + Version: "0.0.0", |
| 109 | + }, |
| 110 | + } |
| 111 | + return []*exporttrace.SpanSnapshot{sd} |
| 112 | +} |
| 113 | + |
| 114 | +// EmptyCheckpointSet is a checkpointer that has no records at all. |
| 115 | +type EmptyCheckpointSet struct { |
| 116 | + noopLocker |
| 117 | +} |
| 118 | + |
| 119 | +var _ exportmetric.CheckpointSet = EmptyCheckpointSet{} |
| 120 | + |
| 121 | +// ForEach implements exportmetric.CheckpointSet. It never invokes the |
| 122 | +// callback. |
| 123 | +func (EmptyCheckpointSet) ForEach(kindSelector exportmetric.ExportKindSelector, recordFunc func(exportmetric.Record) error) error { |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +// FailCheckpointSet is a checkpointer that returns an error during |
| 128 | +// ForEach. |
| 129 | +type FailCheckpointSet struct { |
| 130 | + noopLocker |
| 131 | +} |
| 132 | + |
| 133 | +var _ exportmetric.CheckpointSet = FailCheckpointSet{} |
| 134 | + |
| 135 | +// ForEach implements exportmetric.CheckpointSet. It always fails. |
| 136 | +func (FailCheckpointSet) ForEach(kindSelector exportmetric.ExportKindSelector, recordFunc func(exportmetric.Record) error) error { |
| 137 | + return fmt.Errorf("fail") |
| 138 | +} |
0 commit comments