forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OtlpJsonLoggingMetricExporterTest.java
174 lines (159 loc) · 6.17 KB
/
OtlpJsonLoggingMetricExporterTest.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.exporter.logging.otlp;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;
import io.github.netmikey.logunit.api.LogCapturer;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.data.AggregationTemporality;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.MetricExporter;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableDoublePointData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData;
import io.opentelemetry.sdk.resources.Resource;
import java.util.Arrays;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.skyscreamer.jsonassert.JSONAssert;
import org.slf4j.event.Level;
@SuppressLogger(OtlpJsonLoggingMetricExporter.class)
class OtlpJsonLoggingMetricExporterTest {
private static final Resource RESOURCE =
Resource.create(Attributes.builder().put("key", "value").build());
private static final MetricData METRIC1 =
ImmutableMetricData.createDoubleSum(
RESOURCE,
InstrumentationScopeInfo.builder("instrumentation")
.setVersion("1")
.setAttributes(Attributes.builder().put("key", "value").build())
.build(),
"metric1",
"metric1 description",
"m",
ImmutableSumData.create(
true,
AggregationTemporality.CUMULATIVE,
Arrays.asList(
ImmutableDoublePointData.create(
1, 2, Attributes.of(stringKey("cat"), "meow"), 4))));
private static final MetricData METRIC2 =
ImmutableMetricData.createDoubleSum(
RESOURCE,
InstrumentationScopeInfo.builder("instrumentation2").setVersion("2").build(),
"metric2",
"metric2 description",
"s",
ImmutableSumData.create(
true,
AggregationTemporality.CUMULATIVE,
Arrays.asList(
ImmutableDoublePointData.create(
1, 2, Attributes.of(stringKey("cat"), "meow"), 4))));
@RegisterExtension
LogCapturer logs = LogCapturer.create().captureForType(OtlpJsonLoggingMetricExporter.class);
private MetricExporter exporter;
@BeforeEach
void setUp() {
exporter = OtlpJsonLoggingMetricExporter.create();
}
@Test
void getAggregationTemporality() {
assertThat(
OtlpJsonLoggingMetricExporter.create()
.getAggregationTemporality(InstrumentType.COUNTER))
.isEqualTo(AggregationTemporality.CUMULATIVE);
assertThat(
OtlpJsonLoggingMetricExporter.create(AggregationTemporality.DELTA)
.getAggregationTemporality(InstrumentType.COUNTER))
.isEqualTo(AggregationTemporality.DELTA);
}
@Test
void log() throws Exception {
exporter.export(Arrays.asList(METRIC1, METRIC2));
assertThat(logs.getEvents())
.hasSize(1)
.allSatisfy(log -> assertThat(log.getLevel()).isEqualTo(Level.INFO));
JSONAssert.assertEquals(
"{"
+ " \"resource\": {"
+ " \"attributes\": [{"
+ " \"key\": \"key\","
+ " \"value\": {"
+ " \"stringValue\": \"value\""
+ " }"
+ " }]"
+ " },"
+ " \"scopeMetrics\": [{"
+ " \"scope\": {"
+ " \"name\": \"instrumentation2\","
+ " \"version\": \"2\""
+ " },"
+ " \"metrics\": [{"
+ " \"name\": \"metric2\","
+ " \"description\": \"metric2 description\","
+ " \"unit\": \"s\","
+ " \"sum\": {"
+ " \"dataPoints\": [{"
+ " \"attributes\": [{"
+ " \"key\": \"cat\","
+ " \"value\": {\"stringValue\": \"meow\"}"
+ " }],"
+ " \"startTimeUnixNano\": \"1\","
+ " \"timeUnixNano\": \"2\","
+ " \"asDouble\": 4.0"
+ " }],"
+ " \"aggregationTemporality\": 2,"
+ " \"isMonotonic\": true"
+ " }"
+ " }]"
+ " }, {"
+ " \"scope\": {"
+ " \"name\": \"instrumentation\","
+ " \"version\": \"1\","
+ " \"attributes\":[{"
+ " \"key\":\"key\","
+ " \"value\":{"
+ " \"stringValue\":\"value\""
+ " }"
+ " }]"
+ " },"
+ " \"metrics\": [{"
+ " \"name\": \"metric1\","
+ " \"description\": \"metric1 description\","
+ " \"unit\": \"m\","
+ " \"sum\": {"
+ " \"dataPoints\": [{"
+ " \"attributes\": [{"
+ " \"key\": \"cat\","
+ " \"value\": {\"stringValue\": \"meow\"}"
+ " }],"
+ " \"startTimeUnixNano\": \"1\","
+ " \"timeUnixNano\": \"2\","
+ " \"asDouble\": 4.0"
+ " }],"
+ " \"aggregationTemporality\": 2,"
+ " \"isMonotonic\": true"
+ " }"
+ " }]"
+ " }]"
+ "}",
logs.getEvents().get(0).getMessage(),
/* strict= */ false);
assertThat(logs.getEvents().get(0).getMessage()).doesNotContain("\n");
}
@Test
void flush() {
assertThat(exporter.flush().isSuccess()).isTrue();
}
@Test
void shutdown() {
assertThat(exporter.shutdown().isSuccess()).isTrue();
}
}