|
| 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 exporterhelper |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "reflect" |
| 20 | + "sort" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "go.opencensus.io/stats/view" |
| 25 | + "go.opencensus.io/tag" |
| 26 | + |
| 27 | + "go.opentelemetry.io/collector/config" |
| 28 | + "go.opentelemetry.io/collector/config/configtelemetry" |
| 29 | + "go.opentelemetry.io/collector/obsreport" |
| 30 | + "go.opentelemetry.io/collector/obsreport/obsreporttest" |
| 31 | +) |
| 32 | + |
| 33 | +func TestExportEnqueueFailure(t *testing.T) { |
| 34 | + doneFn, err := obsreporttest.SetupRecordedMetricsTest() |
| 35 | + require.NoError(t, err) |
| 36 | + defer doneFn() |
| 37 | + |
| 38 | + exporter := config.NewID("fakeExporter") |
| 39 | + |
| 40 | + obsrep := newObsExporter(obsreport.ExporterSettings{Level: configtelemetry.LevelNormal, ExporterID: exporter}) |
| 41 | + |
| 42 | + logRecords := 7 |
| 43 | + obsrep.recordLogsEnqueueFailure(context.Background(), logRecords) |
| 44 | + checkExporterEnqueueFailedLogsStats(t, exporter, int64(logRecords)) |
| 45 | + |
| 46 | + spans := 12 |
| 47 | + obsrep.recordTracesEnqueueFailure(context.Background(), spans) |
| 48 | + checkExporterEnqueueFailedTracesStats(t, exporter, int64(spans)) |
| 49 | + |
| 50 | + metricPoints := 21 |
| 51 | + obsrep.recordMetricsEnqueueFailure(context.Background(), metricPoints) |
| 52 | + checkExporterEnqueueFailedMetricsStats(t, exporter, int64(metricPoints)) |
| 53 | +} |
| 54 | + |
| 55 | +// checkExporterEnqueueFailedTracesStats checks that reported number of spans failed to enqueue match given values. |
| 56 | +// When this function is called it is required to also call SetupRecordedMetricsTest as first thing. |
| 57 | +func checkExporterEnqueueFailedTracesStats(t *testing.T, exporter config.ComponentID, spans int64) { |
| 58 | + exporterTags := tagsForExporterView(exporter) |
| 59 | + checkValueForView(t, exporterTags, spans, "exporter/enqueue_failed_spans") |
| 60 | +} |
| 61 | + |
| 62 | +// checkExporterEnqueueFailedMetricsStats checks that reported number of metric points failed to enqueue match given values. |
| 63 | +// When this function is called it is required to also call SetupRecordedMetricsTest as first thing. |
| 64 | +func checkExporterEnqueueFailedMetricsStats(t *testing.T, exporter config.ComponentID, metricPoints int64) { |
| 65 | + exporterTags := tagsForExporterView(exporter) |
| 66 | + checkValueForView(t, exporterTags, metricPoints, "exporter/enqueue_failed_metric_points") |
| 67 | +} |
| 68 | + |
| 69 | +// checkExporterEnqueueFailedLogsStats checks that reported number of log records failed to enqueue match given values. |
| 70 | +// When this function is called it is required to also call SetupRecordedMetricsTest as first thing. |
| 71 | +func checkExporterEnqueueFailedLogsStats(t *testing.T, exporter config.ComponentID, logRecords int64) { |
| 72 | + exporterTags := tagsForExporterView(exporter) |
| 73 | + checkValueForView(t, exporterTags, logRecords, "exporter/enqueue_failed_log_records") |
| 74 | +} |
| 75 | + |
| 76 | +// checkValueForView checks that for the current exported value in the view with the given name |
| 77 | +// for {LegacyTagKeyReceiver: receiverName} is equal to "value". |
| 78 | +func checkValueForView(t *testing.T, wantTags []tag.Tag, value int64, vName string) { |
| 79 | + // Make sure the tags slice is sorted by tag keys. |
| 80 | + sortTags(wantTags) |
| 81 | + |
| 82 | + rows, err := view.RetrieveData(vName) |
| 83 | + require.NoError(t, err) |
| 84 | + |
| 85 | + for _, row := range rows { |
| 86 | + // Make sure the tags slice is sorted by tag keys. |
| 87 | + sortTags(row.Tags) |
| 88 | + if reflect.DeepEqual(wantTags, row.Tags) { |
| 89 | + sum := row.Data.(*view.SumData) |
| 90 | + require.Equal(t, float64(value), sum.Value) |
| 91 | + return |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + require.Failf(t, "could not find tags", "wantTags: %s in rows %v", wantTags, rows) |
| 96 | +} |
| 97 | + |
| 98 | +// tagsForExporterView returns the tags that are needed for the exporter views. |
| 99 | +func tagsForExporterView(exporter config.ComponentID) []tag.Tag { |
| 100 | + return []tag.Tag{ |
| 101 | + {Key: exporterTag, Value: exporter.String()}, |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func sortTags(tags []tag.Tag) { |
| 106 | + sort.SliceStable(tags, func(i, j int) bool { |
| 107 | + return tags[i].Key.Name() < tags[j].Key.Name() |
| 108 | + }) |
| 109 | +} |
0 commit comments