Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
@NotThreadSafe
public class CapaMeter implements Meter {

private final String meterName;
protected final String meterName;

private final String schemaUrl;
protected final String schemaUrl;

private final String version;
protected final String version;

private final Meter meter;
protected final Meter meter;

public CapaMeter(String meterName, String schemaUrl, String version, Meter meter) {
this.meterName = meterName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public CapaMeterBuilder(String name, MeterBuilder builder) {
@Override
public MeterBuilder setSchemaUrl(String schemaUrl) {
this.schemaUrl = schemaUrl;
return meterBuilder.setSchemaUrl(schemaUrl);
meterBuilder.setSchemaUrl(schemaUrl);
return this;
}

@Override
public MeterBuilder setInstrumentationVersion(String instrumentationVersion) {
version = instrumentationVersion;
return meterBuilder.setInstrumentationVersion(instrumentationVersion);
meterBuilder.setInstrumentationVersion(instrumentationVersion);
return this;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,4 @@ public CompletableResultCode flush() {

protected abstract CompletableResultCode doFlush();

@Override
public CompletableResultCode shutdown() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.component.telemetry.metrics;

import io.opentelemetry.api.metrics.DoubleHistogramBuilder;
import io.opentelemetry.api.metrics.LongHistogramBuilder;
import io.opentelemetry.api.metrics.internal.NoopMeter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author: chenyijiang
* @date: 2021/12/1 20:05
*/
public class CapaDoubleHistogramBuilderTest {

@Test
public void build() {
CapaDoubleHistogramBuilder builder = new CapaDoubleHistogramBuilder("", "", "", "");
assertEquals(builder, builder.setDescription("aaa"));
assertEquals(builder, builder.setUnit("bbb"));
assertTrue(builder.build() instanceof NoopMeter.NoopDoubleHistogram);

LongHistogramBuilder longHistogramBuilder = builder.ofLongs();
assertEquals(longHistogramBuilder, longHistogramBuilder.setDescription("ccc"));
assertEquals(longHistogramBuilder, longHistogramBuilder.setUnit("ddd"));
assertTrue(longHistogramBuilder.build() instanceof TestLongHistogram);

DoubleHistogramBuilder doubleHistogramBuilder = longHistogramBuilder.ofDoubles();
assertTrue(doubleHistogramBuilder instanceof CapaDoubleHistogramBuilder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.component.telemetry.metrics;

import io.opentelemetry.api.metrics.Meter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author: chenyijiang
* @date: 2021/11/25 22:35
*/
public class CapaMeterProviderTest {

@Test
public void meterBuilder() {
Meter meter = new CapaMeterProviderBuilder().buildMeterProvider().meterBuilder("aaa").setSchemaUrl("url")
.setInstrumentationVersion("1.1").build();
assertTrue(meter instanceof CapaMeter);
assertEquals("aaa", ((CapaMeter) meter).meterName);
assertEquals("1.1", ((CapaMeter) meter).version);
assertEquals("url", ((CapaMeter) meter).schemaUrl);
}


@Test
public void get() {
Meter meter = new CapaMeterProviderBuilder().buildMeterProvider().get("aaa");
assertTrue(meter instanceof CapaMeter);
assertEquals("aaa", ((CapaMeter) meter).meterName);
assertNull(((CapaMeter) meter).version);
assertNull(((CapaMeter) meter).schemaUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.component.telemetry.metrics;

import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.internal.NoopMeter;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

/**
* @author: chenyijiang
* @date: 2021/12/1 19:55
*/
public class CapaMeterTest {

@Test
public void counterBuilder() {
Meter meter = mock(Meter.class);
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
capaMeter.counterBuilder("aaa");
verify(meter).counterBuilder("aaa");
}

@Test
public void upDownCounterBuilder() {
Meter meter = mock(Meter.class);
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
capaMeter.upDownCounterBuilder("aaa");
verify(meter).upDownCounterBuilder("aaa");
}

@Test
public void histogramBuilder() {
Meter meter = mock(Meter.class);
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
assertTrue(capaMeter.histogramBuilder("aaa").setUnit("a").setDescription("desc").build() instanceof NoopMeter.NoopDoubleHistogram);
verify(meter, never()).histogramBuilder("aaa");
}

@Test
public void gaugeBuilder() {
Meter meter = mock(Meter.class);
CapaMeter capaMeter = new CapaMeter("", "", "", meter);
capaMeter.gaugeBuilder("aaa");
verify(meter).gaugeBuilder("aaa");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.component.telemetry.metrics;

import group.rxcloud.capa.component.telemetry.SamplerConfig;
import io.opentelemetry.sdk.common.CompletableResultCode;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

/**
* @author: chenyijiang
* @date: 2021/12/1 19:20
*/
public class CapaMetricsExporterTest {

@Test
public void export() {
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG));
assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>()));
verify(exporter).doExport(anyList());
}

@Test
public void exportWithNullConfig() {
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> null));
assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>()));
verify(exporter).doExport(anyList());
}

@Test
public void exportWithDisableConfig() {
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> new SamplerConfig() {{ setMetricsEnable(false); setTraceEnable(true);}}));
assertEquals(CompletableResultCode.ofSuccess(), exporter.export(new ArrayList<>()));
verify(exporter, never()).doExport(anyList());
}

@Test
public void flushWithNullConfig() {
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> null));
assertEquals(CompletableResultCode.ofSuccess(), exporter.flush());
verify(exporter).doFlush();
}

@Test
public void flushWithDisableConfig() {
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> new SamplerConfig() {{ setMetricsEnable(false); setTraceEnable(true);}}));
assertEquals(CompletableResultCode.ofSuccess(), exporter.flush());
verify(exporter, never()).doFlush();
}

@Test
public void flush() {
TestCapaMetricsExporter exporter = spy(new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG));
assertEquals(CompletableResultCode.ofSuccess(), exporter.flush());
verify(exporter).doFlush();
}


@Test
public void shutdown() {
assertEquals(CompletableResultCode.ofSuccess(), new TestCapaMetricsExporter(() -> SamplerConfig.DEFAULT_CONFIG).shutdown());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package group.rxcloud.capa.component.telemetry.metrics;

import group.rxcloud.capa.component.telemetry.SamplerConfig;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.metrics.data.MetricData;

import java.util.Collection;
import java.util.function.Supplier;

/**
* @author: chenyijiang
* @date: 2021/12/1 19:20
*/
public class TestCapaMetricsExporter extends CapaMetricsExporter {

public TestCapaMetricsExporter(
Supplier<SamplerConfig> samplerConfig) {
super(samplerConfig);
}

@Override
protected CompletableResultCode doExport(Collection<MetricData> metrics) {
return CompletableResultCode.ofSuccess();
}

@Override
protected CompletableResultCode doFlush() {
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode shutdown() {
return CompletableResultCode.ofSuccess();
}
}
Loading