Skip to content

Commit

Permalink
OAP-252 Metric assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
galaxina committed Mar 6, 2024
1 parent 9ec5305 commit dd25957
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
82 changes: 71 additions & 11 deletions oap-stdlib-test/src/main/java/oap/metrics/MetricsFixture.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
package oap.metrics;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.search.MeterNotFoundException;
import io.micrometer.core.instrument.search.RequiredSearch;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import oap.testng.AbstractScopeFixture;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractDoubleAssert;

import static org.assertj.core.api.Assertions.assertThat;

public class MetricsFixture extends AbstractScopeFixture<MetricsFixture> {

private SimpleMeterRegistry registry;

public MetricsAssertion assertMetric( String name, Tags tags ) {
return new MetricsAssertion( name, tags );
}

public AbstractDoubleAssert<?> assertGauge( String name, Tags tags ) {
RequiredSearch requiredSearch = get( name, tags );
var gauge = requiredSearch.gauge();

Meter.Id id = new Meter.Id( name, tags, null, null, Meter.Type.COUNTER );
return assertThat( gauge.value() ).as( id.toString() );
}

@Override
protected void before() {
registry = new SimpleMeterRegistry();
Metrics.globalRegistry.add( registry );
super.before();
}

public RequiredSearch get( String name, String... tags ) {

RequiredSearch rs = RequiredSearch.in( Metrics.globalRegistry ).name( name );

assertThat( ( tags.length / 2 ) * 2 ).isEqualTo( tags.length );

for( int i = 0; i < tags.length / 2; i += 2 ) {
rs = rs.tag( tags[i], tags[i + 1] );
}

return rs;
public RequiredSearch get( String name, Tags tags ) {
return RequiredSearch.in( Metrics.globalRegistry ).name( name ).tags( tags );
}

@Override
Expand All @@ -38,4 +48,54 @@ protected void after() {
Metrics.globalRegistry.remove( registry );
}
}

public static class CounterMetricsAssertion extends AbstractAssert<CounterMetricsAssertion, Counter> {
public CounterMetricsAssertion( Counter actual ) {
super( actual, CounterMetricsAssertion.class );
}

public CounterMetricsAssertion isEqualTo( double expected ) {
assertThat( actual.count() ).isEqualTo( expected );

return this;
}
}

public static class GaugeMetricsAssertion extends AbstractAssert<GaugeMetricsAssertion, Gauge> {
public GaugeMetricsAssertion( Gauge actual ) {
super( actual, GaugeMetricsAssertion.class );
}

public GaugeMetricsAssertion isEqualTo( double expected ) {
assertThat( actual.value() ).isEqualTo( expected );

return this;
}
}

public class MetricsAssertion extends AbstractAssert<MetricsAssertion, RequiredSearch> {
protected MetricsAssertion( String name, Tags tags ) {
super( get( name, tags ), MetricsAssertion.class );
}

public CounterMetricsAssertion isCounter() {
try {
return new CounterMetricsAssertion( actual.counter() );
} catch( ClassCastException e ) {
throw failure( "a metric is not a counter" );
} catch( MeterNotFoundException e ) {
throw failure( "metric not found" );
}
}

public GaugeMetricsAssertion isGauge() {
try {
return new GaugeMetricsAssertion( actual.gauge() );
} catch( ClassCastException e ) {
throw failure( "a metric is not a gauge" );
} catch( MeterNotFoundException e ) {
throw failure( "metric not found" );
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package oap.tools;

import io.micrometer.core.instrument.Tags;
import oap.metrics.MetricsFixture;
import oap.testng.Fixtures;
import oap.testng.TestDirectoryFixture;
Expand Down Expand Up @@ -71,8 +72,12 @@ public void testCompileSingleFileOk() {

assertThat( result2.isSuccess() ).isTrue();

assertThat( metricsFixture.get( "oap_template", "type", "disk" ).counter().count() ).isEqualTo( 1.0d );
assertThat( metricsFixture.get( "oap_template", "type", "compile" ).counter().count() ).isEqualTo( 1.0d );
metricsFixture.assertMetric( "oap_template", Tags.of( "type", "disk" ) )
.isCounter()
.isEqualTo( 1.0d );
metricsFixture.assertMetric( "oap_template", Tags.of( "type", "compile" ) )
.isCounter()
.isEqualTo( 1.0d );
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</repositories>

<properties>
<oap.project.version>21.15.7</oap.project.version>
<oap.project.version>21.15.8</oap.project.version>

<oap.deps.config.version>21.0.0</oap.deps.config.version>
<oap.deps.oap-teamcity.version>21.0.1</oap.deps.oap-teamcity.version>
Expand Down

0 comments on commit dd25957

Please sign in to comment.