Skip to content

Commit da9bf31

Browse files
authored
Merge pull request #62 from JasmineJ1230/feature/metrics
Add comments and ut.
2 parents baa605b + c9736fa commit da9bf31

11 files changed

Lines changed: 391 additions & 11 deletions

File tree

sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
@NotThreadSafe
3030
public class CapaMeter implements Meter {
3131

32-
private final String meterName;
32+
protected final String meterName;
3333

34-
private final String schemaUrl;
34+
protected final String schemaUrl;
3535

36-
private final String version;
36+
protected final String version;
3737

38-
private final Meter meter;
38+
protected final Meter meter;
3939

4040
public CapaMeter(String meterName, String schemaUrl, String version, Meter meter) {
4141
this.meterName = meterName;

sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMeterBuilder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ public CapaMeterBuilder(String name, MeterBuilder builder) {
4040
@Override
4141
public MeterBuilder setSchemaUrl(String schemaUrl) {
4242
this.schemaUrl = schemaUrl;
43-
return meterBuilder.setSchemaUrl(schemaUrl);
43+
meterBuilder.setSchemaUrl(schemaUrl);
44+
return this;
4445
}
4546

4647
@Override
4748
public MeterBuilder setInstrumentationVersion(String instrumentationVersion) {
4849
version = instrumentationVersion;
49-
return meterBuilder.setInstrumentationVersion(instrumentationVersion);
50+
meterBuilder.setInstrumentationVersion(instrumentationVersion);
51+
return this;
5052
}
5153

5254
@Override

sdk-component/src/main/java/group/rxcloud/capa/component/telemetry/metrics/CapaMetricsExporter.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,4 @@ public CompletableResultCode flush() {
5959

6060
protected abstract CompletableResultCode doFlush();
6161

62-
@Override
63-
public CompletableResultCode shutdown() {
64-
return null;
65-
}
6662
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component.telemetry.metrics;
18+
19+
import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
20+
import io.opentelemetry.api.metrics.LongHistogramBuilder;
21+
import io.opentelemetry.api.metrics.internal.NoopMeter;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
/**
27+
* @author: chenyijiang
28+
* @date: 2021/12/1 20:05
29+
*/
30+
public class CapaDoubleHistogramBuilderTest {
31+
32+
@Test
33+
public void build() {
34+
CapaDoubleHistogramBuilder builder = new CapaDoubleHistogramBuilder("", "", "", "");
35+
assertEquals(builder, builder.setDescription("aaa"));
36+
assertEquals(builder, builder.setUnit("bbb"));
37+
assertTrue(builder.build() instanceof NoopMeter.NoopDoubleHistogram);
38+
39+
LongHistogramBuilder longHistogramBuilder = builder.ofLongs();
40+
assertEquals(longHistogramBuilder, longHistogramBuilder.setDescription("ccc"));
41+
assertEquals(longHistogramBuilder, longHistogramBuilder.setUnit("ddd"));
42+
assertTrue(longHistogramBuilder.build() instanceof TestLongHistogram);
43+
44+
DoubleHistogramBuilder doubleHistogramBuilder = longHistogramBuilder.ofDoubles();
45+
assertTrue(doubleHistogramBuilder instanceof CapaDoubleHistogramBuilder);
46+
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component.telemetry.metrics;
18+
19+
import io.opentelemetry.api.metrics.Meter;
20+
import org.junit.jupiter.api.Test;
21+
22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
26+
/**
27+
* @author: chenyijiang
28+
* @date: 2021/11/25 22:35
29+
*/
30+
public class CapaMeterProviderTest {
31+
32+
@Test
33+
public void meterBuilder() {
34+
Meter meter = new CapaMeterProviderBuilder().buildMeterProvider().meterBuilder("aaa").setSchemaUrl("url")
35+
.setInstrumentationVersion("1.1").build();
36+
assertTrue(meter instanceof CapaMeter);
37+
assertEquals("aaa", ((CapaMeter) meter).meterName);
38+
assertEquals("1.1", ((CapaMeter) meter).version);
39+
assertEquals("url", ((CapaMeter) meter).schemaUrl);
40+
}
41+
42+
43+
@Test
44+
public void get() {
45+
Meter meter = new CapaMeterProviderBuilder().buildMeterProvider().get("aaa");
46+
assertTrue(meter instanceof CapaMeter);
47+
assertEquals("aaa", ((CapaMeter) meter).meterName);
48+
assertNull(((CapaMeter) meter).version);
49+
assertNull(((CapaMeter) meter).schemaUrl);
50+
}
51+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component.telemetry.metrics;
18+
19+
import io.opentelemetry.api.metrics.Meter;
20+
import io.opentelemetry.api.metrics.internal.NoopMeter;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.never;
26+
import static org.mockito.Mockito.verify;
27+
28+
/**
29+
* @author: chenyijiang
30+
* @date: 2021/12/1 19:55
31+
*/
32+
public class CapaMeterTest {
33+
34+
@Test
35+
public void counterBuilder() {
36+
Meter meter = mock(Meter.class);
37+
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
38+
capaMeter.counterBuilder("aaa");
39+
verify(meter).counterBuilder("aaa");
40+
}
41+
42+
@Test
43+
public void upDownCounterBuilder() {
44+
Meter meter = mock(Meter.class);
45+
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
46+
capaMeter.upDownCounterBuilder("aaa");
47+
verify(meter).upDownCounterBuilder("aaa");
48+
}
49+
50+
@Test
51+
public void histogramBuilder() {
52+
Meter meter = mock(Meter.class);
53+
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
54+
assertTrue(capaMeter.histogramBuilder("aaa").setUnit("a").setDescription("desc").build() instanceof NoopMeter.NoopDoubleHistogram);
55+
verify(meter, never()).histogramBuilder("aaa");
56+
}
57+
58+
@Test
59+
public void gaugeBuilder() {
60+
Meter meter = mock(Meter.class);
61+
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
62+
capaMeter.gaugeBuilder("aaa");
63+
verify(meter).gaugeBuilder("aaa");
64+
}
65+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component.telemetry.metrics;
18+
19+
import group.rxcloud.capa.component.telemetry.SamplerConfig;
20+
import io.opentelemetry.sdk.common.CompletableResultCode;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.ArrayList;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
import static org.mockito.ArgumentMatchers.anyList;
27+
import static org.mockito.Mockito.never;
28+
import static org.mockito.Mockito.spy;
29+
import static org.mockito.Mockito.verify;
30+
31+
/**
32+
* @author: chenyijiang
33+
* @date: 2021/12/1 19:20
34+
*/
35+
public class CapaMetricsExporterTest {
36+
37+
@Test
38+
public void export() {
39+
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG));
40+
assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>()));
41+
verify(exporter).doExport(anyList());
42+
}
43+
44+
@Test
45+
public void exportWithNullConfig() {
46+
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> null));
47+
assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>()));
48+
verify(exporter).doExport(anyList());
49+
}
50+
51+
@Test
52+
public void exportWithDisableConfig() {
53+
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> new SamplerConfig() {{ setMetricsEnable(false); setTraceEnable(true);}}));
54+
assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>()));
55+
verify(exporter, never()).doExport(anyList());
56+
}
57+
58+
@Test
59+
public void flushWithNullConfig() {
60+
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> null));
61+
assertEquals(CompletableResultCode.ofSuccess(), exporter.flush());
62+
verify(exporter).doFlush();
63+
}
64+
65+
@Test
66+
public void flushWithDisableConfig() {
67+
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> new SamplerConfig() {{ setMetricsEnable(false); setTraceEnable(true);}}));
68+
assertEquals(CompletableResultCode.ofSuccess(), exporter.flush());
69+
verify(exporter, never()).doFlush();
70+
}
71+
72+
@Test
73+
public void flush() {
74+
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG));
75+
assertEquals(CompletableResultCode.ofSuccess(), exporter.flush());
76+
verify(exporter).doFlush();
77+
}
78+
79+
80+
@Test
81+
public void shutdown() {
82+
assertEquals(CompletableResultCode.ofSuccess(), new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG).shutdown());
83+
}
84+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package group.rxcloud.capa.component.telemetry.metrics;
18+
19+
import group.rxcloud.capa.component.telemetry.SamplerConfig;
20+
import io.opentelemetry.sdk.common.CompletableResultCode;
21+
import io.opentelemetry.sdk.metrics.data.MetricData;
22+
23+
import java.util.Collection;
24+
import java.util.function.Supplier;
25+
26+
/**
27+
* @author: chenyijiang
28+
* @date: 2021/12/1 19:20
29+
*/
30+
public class TestCapaMetricsExporter extends CapaMetricsExporter {
31+
32+
public TestCapaMetricsExporter(
33+
Supplier<SamplerConfig> samplerConfig) {
34+
super(samplerConfig);
35+
}
36+
37+
@Override
38+
protected CompletableResultCode doExport(Collection<MetricData> metrics) {
39+
return CompletableResultCode.ofSuccess();
40+
}
41+
42+
@Override
43+
protected CompletableResultCode doFlush() {
44+
return CompletableResultCode.ofSuccess();
45+
}
46+
47+
@Override
48+
public CompletableResultCode shutdown() {
49+
return CompletableResultCode.ofSuccess();
50+
}
51+
}

0 commit comments

Comments
 (0)