forked from open-telemetry/opentelemetry-java
/
SourceInfoTest.java
136 lines (128 loc) · 5.7 KB
/
SourceInfoTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.testing;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.InstrumentValueType;
import io.opentelemetry.sdk.metrics.View;
import io.opentelemetry.sdk.metrics.internal.debug.SourceInfo;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.descriptor.MetricDescriptor;
import io.opentelemetry.sdk.metrics.internal.state.DebugUtils;
import org.junit.jupiter.api.Test;
// Note: This class MUST be outside the io.opentelemetry.metrics package to work correctly.
class SourceInfoTest {
// Note: The line numbers for these statics are used as part of the test.
private static final SourceInfo info = SourceInfo.fromCurrentStack();
@Test
void sourceInfoFindsStackTrace() {
assertThat(info.shortDebugString()).isEqualTo("SourceInfoTest.java:22");
assertThat(info.multiLineDebugString())
.startsWith(
"\tat io.opentelemetry.testing.SourceInfoTest.<clinit>(SourceInfoTest.java:22)\n");
}
@Test
void testDuplicateExceptionMessage_pureInstruments() {
MetricDescriptor simple =
MetricDescriptor.create(
View.builder().build(),
SourceInfo.fromCurrentStack(),
InstrumentDescriptor.create(
"name",
"description",
"unit",
InstrumentType.OBSERVABLE_COUNTER,
InstrumentValueType.DOUBLE));
MetricDescriptor simpleWithNewDescription =
MetricDescriptor.create(
View.builder().build(),
SourceInfo.fromCurrentStack(),
InstrumentDescriptor.create(
"name2",
"description2",
"unit2",
InstrumentType.COUNTER,
InstrumentValueType.LONG));
assertThat(DebugUtils.duplicateMetricErrorMessage(simple, simpleWithNewDescription))
.contains("Found duplicate metric definition: name")
.contains("- InstrumentDescription [description2] does not match [description]")
.contains("- InstrumentName [name2] does not match [name]")
.contains("- InstrumentUnit [unit2] does not match [unit]")
.contains("- InstrumentType [COUNTER] does not match [OBSERVABLE_COUNTER]")
.contains("- InstrumentValueType [LONG] does not match [DOUBLE]")
.contains(simple.getSourceInstrument().getSourceInfo().multiLineDebugString())
.contains("Original instrument registered with same name but is incompatible.")
.contains(
simpleWithNewDescription.getSourceInstrument().getSourceInfo().multiLineDebugString());
}
@Test
void testDuplicateExceptionMessage_viewBasedConflict() {
SourceInfo simpleSourceInfo = SourceInfo.fromCurrentStack();
MetricDescriptor simple =
MetricDescriptor.create(
View.builder().setName("name2").build(),
simpleSourceInfo,
InstrumentDescriptor.create(
"name",
"description",
"unit",
InstrumentType.HISTOGRAM,
InstrumentValueType.DOUBLE));
MetricDescriptor simpleWithNewDescription =
MetricDescriptor.create(
View.builder().build(),
SourceInfo.fromCurrentStack(),
InstrumentDescriptor.create(
"name2",
"description2",
"unit",
InstrumentType.HISTOGRAM,
InstrumentValueType.DOUBLE));
assertThat(DebugUtils.duplicateMetricErrorMessage(simple, simpleWithNewDescription))
.contains("Found duplicate metric definition: name2")
.contains(simple.getSourceInstrument().getSourceInfo().multiLineDebugString())
.contains("- InstrumentDescription [description2] does not match [description]")
.contains("Conflicting view registered")
.contains(simpleSourceInfo.multiLineDebugString())
.contains("FROM instrument name")
.contains(
simpleWithNewDescription.getSourceInstrument().getSourceInfo().multiLineDebugString());
}
@Test
void testDuplicateExceptionMessage_viewBasedConflict2() {
SourceInfo problemViewSourceInfo = SourceInfo.fromCurrentStack();
View problemView = View.builder().setName("name").build();
MetricDescriptor simple =
MetricDescriptor.create(
View.builder().build(),
SourceInfo.fromCurrentStack(),
InstrumentDescriptor.create(
"name",
"description",
"unit2",
InstrumentType.HISTOGRAM,
InstrumentValueType.DOUBLE));
MetricDescriptor simpleWithNewDescription =
MetricDescriptor.create(
problemView,
problemViewSourceInfo,
InstrumentDescriptor.create(
"name2",
"description",
"unit",
InstrumentType.HISTOGRAM,
InstrumentValueType.DOUBLE));
assertThat(DebugUtils.duplicateMetricErrorMessage(simple, simpleWithNewDescription))
.contains("Found duplicate metric definition: name")
.contains("VIEW defined")
.contains(problemViewSourceInfo.multiLineDebugString())
.contains("FROM instrument name2")
.contains(simple.getSourceInstrument().getSourceInfo().multiLineDebugString())
.contains("- InstrumentUnit [unit] does not match [unit2]")
.contains("Original instrument registered with same name but is incompatible.")
.contains(
simpleWithNewDescription.getSourceInstrument().getSourceInfo().multiLineDebugString());
}
}