Skip to content

Commit

Permalink
[exporter/logging] Improve tests for metrics and traces (#6534)
Browse files Browse the repository at this point in the history
Improve the test cases for metrics and traces to inspect the full generated text as it's already done for logs
  • Loading branch information
dmitryax authored Nov 14, 2022
1 parent bd1165e commit e4b2f6a
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 28 deletions.
47 changes: 32 additions & 15 deletions exporter/loggingexporter/internal/otlptext/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,53 @@
package otlptext

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/internal/testdata"
"go.opentelemetry.io/collector/pdata/pmetric"
)

func TestMetricsText(t *testing.T) {
type args struct {
md pmetric.Metrics
}
tests := []struct {
name string
args args
empty bool
name string
in pmetric.Metrics
out string
}{
{"empty metrics", args{pmetric.NewMetrics()}, true},
{"metrics with all types and datapoints", args{testdata.GenerateMetricsAllTypes()}, false},
{"metrics with all types without datapoints", args{testdata.GenerateMetricsAllTypesEmpty()}, false},
{"metrics with invalid metric types", args{testdata.GenerateMetricsMetricTypeInvalid()}, false},
{"metrics with lots of metrics", args{testdata.GenerateMetrics(10)}, false},
{
name: "empty_metrics",
in: pmetric.NewMetrics(),
out: "empty.out",
},
{
name: "metrics_with_all_types",
in: testdata.GenerateMetricsAllTypes(),
out: "metrics_with_all_types.out",
},
{
name: "two_metrics",
in: testdata.GenerateMetrics(2),
out: "two_metrics.out",
},
{
name: "invalid_metric_type",
in: testdata.GenerateMetricsMetricTypeInvalid(),
out: "invalid_metric_type.out",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metrics, err := NewTextMetricsMarshaler().MarshalMetrics(tt.args.md)
got, err := NewTextMetricsMarshaler().MarshalMetrics(tt.in)
assert.NoError(t, err)
if !tt.empty {
assert.NotEmpty(t, metrics)
}
out, err := os.ReadFile(filepath.Join("testdata", "metrics", tt.out))
require.NoError(t, err)
expected := strings.ReplaceAll(string(out), "\r", "")
assert.Equal(t, expected, string(got))
})
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ResourceMetrics #0
Resource SchemaURL:
Resource attributes:
-> resource-attr: Str(resource-attr-val-1)
ScopeMetrics #0
ScopeMetrics SchemaURL:
InstrumentationScope
Metric #0
Descriptor:
-> Name: sum-int
-> Description:
-> Unit: 1
-> DataType: Empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
ResourceMetrics #0
Resource SchemaURL:
Resource attributes:
-> resource-attr: Str(resource-attr-val-1)
ScopeMetrics #0
ScopeMetrics SchemaURL:
InstrumentationScope
Metric #0
Descriptor:
-> Name: gauge-int
-> Description:
-> Unit: 1
-> DataType: Gauge
NumberDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 123
NumberDataPoints #1
Data point attributes:
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 456
Metric #1
Descriptor:
-> Name: gauge-double
-> Description:
-> Unit: 1
-> DataType: Gauge
NumberDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 1.230000
NumberDataPoints #1
Data point attributes:
-> label-1: Str(label-value-1)
-> label-3: Str(label-value-3)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 4.560000
Metric #2
Descriptor:
-> Name: sum-int
-> Description:
-> Unit: 1
-> DataType: Sum
-> IsMonotonic: true
-> AggregationTemporality: Cumulative
NumberDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 123
NumberDataPoints #1
Data point attributes:
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 456
Metric #3
Descriptor:
-> Name: sum-double
-> Description:
-> Unit: 1
-> DataType: Sum
-> IsMonotonic: true
-> AggregationTemporality: Cumulative
NumberDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 1.230000
NumberDataPoints #1
Data point attributes:
-> label-1: Str(label-value-1)
-> label-3: Str(label-value-3)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 4.560000
Metric #4
Descriptor:
-> Name: histogram
-> Description:
-> Unit: 1
-> DataType: Histogram
-> AggregationTemporality: Cumulative
HistogramDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
-> label-3: Str(label-value-3)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Count: 1
Sum: 15.000000
HistogramDataPoints #1
Data point attributes:
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Count: 1
Sum: 15.000000
Min: 15.000000
Max: 15.000000
ExplicitBounds #0: 1.000000
Buckets #0, Count: 0
Buckets #1, Count: 1
Metric #5
Descriptor:
-> Name: exponential-histogram
-> Description:
-> Unit: 1
-> DataType: ExponentialHistogram
-> AggregationTemporality: Delta
ExponentialHistogramDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
-> label-3: Str(label-value-3)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Count: 5
Sum: 0.150000
Bucket (-1.414214, -1.000000], Count: 1
Bucket (-1.000000, -0.707107], Count: 1
Bucket [0, 0], Count: 1
Bucket [1.414214, 2.000000), Count: 1
Bucket [2.000000, 2.828427), Count: 1
ExponentialHistogramDataPoints #1
Data point attributes:
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Count: 3
Sum: 1.250000
Min: 0.000000
Max: 1.000000
Bucket [0, 0], Count: 1
Bucket [0.250000, 1.000000), Count: 1
Bucket [1.000000, 4.000000), Count: 1
Metric #6
Descriptor:
-> Name: summary
-> Description:
-> Unit: 1
-> DataType: Summary
SummaryDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
-> label-3: Str(label-value-3)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Count: 1
Sum: 15.000000
SummaryDataPoints #1
Data point attributes:
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Count: 1
Sum: 15.000000
QuantileValue #0: Quantile 0.010000, Value 15.000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
ResourceMetrics #0
Resource SchemaURL:
Resource attributes:
-> resource-attr: Str(resource-attr-val-1)
ScopeMetrics #0
ScopeMetrics SchemaURL:
InstrumentationScope
Metric #0
Descriptor:
-> Name: gauge-int
-> Description:
-> Unit: 1
-> DataType: Gauge
NumberDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 123
NumberDataPoints #1
Data point attributes:
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 456
Metric #1
Descriptor:
-> Name: gauge-double
-> Description:
-> Unit: 1
-> DataType: Gauge
NumberDataPoints #0
Data point attributes:
-> label-1: Str(label-value-1)
-> label-2: Str(label-value-2)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 1.230000
NumberDataPoints #1
Data point attributes:
-> label-1: Str(label-value-1)
-> label-3: Str(label-value-3)
StartTimestamp: 2020-02-11 20:26:12.000000321 +0000 UTC
Timestamp: 2020-02-11 20:26:13.000000789 +0000 UTC
Value: 4.560000
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
ResourceSpans #0
Resource SchemaURL:
Resource attributes:
-> resource-attr: Str(resource-attr-val-1)
ScopeSpans #0
ScopeSpans SchemaURL:
InstrumentationScope
Span #0
Trace ID : 0102030405060708090a0b0c0d0e0f10
Parent ID :
ID : 1112131415161718
Name : operationA
Kind : Unspecified
Start time : 2020-02-11 20:26:12.000000321 +0000 UTC
End time : 2020-02-11 20:26:13.000000789 +0000 UTC
Status code : Error
Status message : status-cancelled
Events:
SpanEvent #0
-> Name: event-with-attr
-> Timestamp: 2020-02-11 20:26:13.000000123 +0000 UTC
-> DroppedAttributesCount: 2
-> Attributes::
-> span-event-attr: Str(span-event-attr-val)
SpanEvent #1
-> Name: event
-> Timestamp: 2020-02-11 20:26:13.000000123 +0000 UTC
-> DroppedAttributesCount: 2
Span #1
Trace ID :
Parent ID :
ID :
Name : operationB
Kind : Unspecified
Start time : 2020-02-11 20:26:12.000000321 +0000 UTC
End time : 2020-02-11 20:26:13.000000789 +0000 UTC
Status code : Unset
Status message :
Links:
SpanLink #0
-> Trace ID:
-> ID:
-> TraceState:
-> DroppedAttributesCount: 4
-> Attributes::
-> span-link-attr: Str(span-link-attr-val)
SpanLink #1
-> Trace ID:
-> ID:
-> TraceState:
-> DroppedAttributesCount: 4
Loading

0 comments on commit e4b2f6a

Please sign in to comment.