Search in sources :

Example 16 with Tags

use of io.micrometer.core.instrument.Tags in project hono by eclipse.

the class MicrometerBasedMetrics method handleTenantTimeout.

/**
 * <b> On synchronization: </b>
 * <p>
 * Each remove operation ({@link MeterRegistry#remove(io.micrometer.core.instrument.Meter)}) is synchronized
 * internally. The removal operations are not synchronized together.
 * </p>
 * <b>Rationale:</b>
 * <p>
 * It is a) unlikely that a tenant connects during the cleanup (timeout is rather in hours than in very small time
 * units). And b) if it happens, a metrics would temporarily be not 100% accurate. This does not justify additional
 * locking for every message that is send to Hono.
 */
private void handleTenantTimeout(final String tenantId) {
    final Tags tenantTag = Tags.of(MetricsTags.getTenantTag(tenantId));
    // the onMeterRemoved() handler removes it also from this.authenticatedConnections
    registry.find(METER_CONNECTIONS_AUTHENTICATED).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_CONNECTIONS_AUTHENTICATED_DURATION).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_MESSAGES_PAYLOAD).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_MESSAGES_RECEIVED).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_COMMANDS_PAYLOAD).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_COMMANDS_RECEIVED).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_DOWNSTREAM_FULL).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_DOWNSTREAM_SENT).tags(tenantTag).meters().forEach(registry::remove);
    registry.find(METER_DOWNSTREAM_TIMEOUT).tags(tenantTag).meters().forEach(registry::remove);
    vertx.eventBus().publish(Constants.EVENT_BUS_ADDRESS_TENANT_TIMED_OUT, tenantId);
}
Also used : Tags(io.micrometer.core.instrument.Tags)

Example 17 with Tags

use of io.micrometer.core.instrument.Tags in project hono by eclipse.

the class MicrometerBasedMetrics method reportConnectionAttempt.

/**
 * {@inheritDoc}
 * <p>
 * The tenant tag will be set to value <em>UNKNOWN</em> if the
 * given tenant ID is {@code null}.
 * The cipher suite tag will be set to value <em>UNKNOWN</em> if
 * the given cipher suite is {@code null}.
 */
@Override
public void reportConnectionAttempt(final MetricsTags.ConnectionAttemptOutcome outcome, final String tenantId, final String cipherSuite) {
    Objects.requireNonNull(outcome);
    final Tags tags = Tags.of(outcome.asTag()).and(MetricsTags.getTenantTag(tenantId)).and(MetricsTags.getCipherSuiteTag(cipherSuite));
    Counter.builder(METER_CONNECTIONS_ATTEMPTS).tags(tags).register(this.registry).increment();
}
Also used : Tags(io.micrometer.core.instrument.Tags)

Example 18 with Tags

use of io.micrometer.core.instrument.Tags in project hono by eclipse.

the class MicrometerBasedMetricsTest method testTimeoutRemovesMetrics.

/**
 * Verifies that the metrics are removed when the tenant timeout is exceeded.
 *
 * @param registry The registry that the tests should be run against.
 */
@ParameterizedTest
@MethodSource("registries")
public void testTimeoutRemovesMetrics(final MeterRegistry registry) {
    final Tags tenantTags = Tags.of(MetricsTags.getTenantTag(tenant));
    final Vertx vertx = mock(Vertx.class);
    when(vertx.eventBus()).thenReturn(mock(EventBus.class));
    // a mocked Vert.x timer, that can be fired deliberately later in the test
    final AtomicReference<Handler<Long>> timerHandler = new AtomicReference<>();
    when(vertx.setTimer(anyLong(), any())).thenAnswer(invocation -> {
        final Handler<Long> task = invocation.getArgument(1);
        timerHandler.set(task);
        return 1L;
    });
    // GIVEN a metrics instance with tenantIdleTimeout configured ...
    final MicrometerBasedMetrics metrics = new MicrometerBasedMetrics(registry, vertx);
    metrics.setProtocolAdapterProperties(configWithTenantIdleTimeout(1L));
    // ... with a device connected and a telemetry message and a command recorded
    metrics.incrementConnections(tenant);
    reportTelemetry(metrics);
    reportCommand(metrics);
    assertNotNull(registry.find(MicrometerBasedMetrics.METER_MESSAGES_PAYLOAD).tags(tenantTags).meter());
    assertNotNull(registry.find(MicrometerBasedMetrics.METER_MESSAGES_RECEIVED).tags(tenantTags).meter());
    assertNotNull(registry.find(MicrometerBasedMetrics.METER_COMMANDS_PAYLOAD).tags(tenantTags).meter());
    assertNotNull(registry.find(MicrometerBasedMetrics.METER_COMMANDS_RECEIVED).tags(tenantTags).meter());
    assertNotNull(registry.find(MicrometerBasedMetrics.METER_CONNECTIONS_AUTHENTICATED).tags(tenantTags).meter());
    // WHEN the device disconnects ...
    metrics.decrementConnections(tenant);
    // ... and the timeout timer fires
    // fake timeout duration exceeded
    metrics.getLastSeenTimestampPerTenant().put(tenant, 0L);
    timerHandler.get().handle(null);
    // THEN the metrics have been removed
    assertNull(registry.find(MicrometerBasedMetrics.METER_MESSAGES_PAYLOAD).tags(tenantTags).meter());
    assertNull(registry.find(MicrometerBasedMetrics.METER_MESSAGES_RECEIVED).tags(tenantTags).meter());
    assertNull(registry.find(MicrometerBasedMetrics.METER_COMMANDS_PAYLOAD).tags(tenantTags).meter());
    assertNull(registry.find(MicrometerBasedMetrics.METER_COMMANDS_RECEIVED).tags(tenantTags).meter());
    assertNull(registry.find(MicrometerBasedMetrics.METER_CONNECTIONS_AUTHENTICATED).tags(tenantTags).meter());
}
Also used : ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) EventBus(io.vertx.core.eventbus.EventBus) Vertx(io.vertx.core.Vertx) Tags(io.micrometer.core.instrument.Tags) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 19 with Tags

use of io.micrometer.core.instrument.Tags in project hono by eclipse.

the class MicrometerBasedMetricsTest method testReportTelemetryWithOptionalQos.

/**
 * Verifies that arbitrary telemetry messages with or without a QoS
 * can be reported successfully.
 *
 * @param registry The registry that the tests should be run against.
 */
@ParameterizedTest
@MethodSource("registries")
public void testReportTelemetryWithOptionalQos(final MeterRegistry registry) {
    final MicrometerBasedMetrics metrics = new MicrometerBasedMetrics(registry, mock(Vertx.class));
    // GIVEN a sample
    final Sample sample = metrics.startTimer();
    // WHEN reporting a telemetry message with a QoS of AT_LEAST_ONCE
    // and no TTD
    metrics.reportTelemetry(MetricsTags.EndpointType.TELEMETRY, "tenant", TenantObject.from("tenant", true), MetricsTags.ProcessingOutcome.FORWARDED, MetricsTags.QoS.AT_LEAST_ONCE, 1024, MetricsTags.TtdStatus.NONE, sample);
    // THEN the meter can be found in the registry with the tags that have a known value
    final Tags expectedTags = Tags.of(MetricsTags.EndpointType.TELEMETRY.asTag()).and(MetricsTags.getTenantTag("tenant")).and(MetricsTags.ProcessingOutcome.FORWARDED.asTag()).and(MetricsTags.QoS.AT_LEAST_ONCE.asTag());
    assertNotNull(registry.find(MicrometerBasedMetrics.METER_MESSAGES_RECEIVED).tags(expectedTags).timer());
    // and reporting another telemetry message with no QoS but with a TTD status succeeds
    final Sample otherSample = metrics.startTimer();
    metrics.reportTelemetry(MetricsTags.EndpointType.TELEMETRY, "tenant", TenantObject.from("tenant", true), MetricsTags.ProcessingOutcome.FORWARDED, MetricsTags.QoS.UNKNOWN, 1024, MetricsTags.TtdStatus.EXPIRED, otherSample);
}
Also used : Sample(io.micrometer.core.instrument.Timer.Sample) Vertx(io.vertx.core.Vertx) Tags(io.micrometer.core.instrument.Tags) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with Tags

use of io.micrometer.core.instrument.Tags in project hono by eclipse.

the class MicrometerBasedMetricsTest method testReportTelemetryWithUnknownTagValues.

/**
 * Verifies that when reporting a downstream message no tags for
 * {@link QoS#UNKNOWN} nor {@link TtdStatus#NONE} are included.
 *
 * @param registry The registry that the tests should be run against.
 */
@ParameterizedTest
@MethodSource("registries")
public void testReportTelemetryWithUnknownTagValues(final MeterRegistry registry) {
    final MicrometerBasedMetrics metrics = new MicrometerBasedMetrics(registry, mock(Vertx.class));
    metrics.reportTelemetry(MetricsTags.EndpointType.TELEMETRY, "tenant", TenantObject.from("tenant", true), MetricsTags.ProcessingOutcome.FORWARDED, MetricsTags.QoS.UNKNOWN, 1024, MetricsTags.TtdStatus.NONE, metrics.startTimer());
    final Tags expectedTags = Tags.of(MetricsTags.EndpointType.TELEMETRY.asTag()).and(MetricsTags.getTenantTag("tenant")).and(MetricsTags.ProcessingOutcome.FORWARDED.asTag()).and(MetricsTags.QoS.UNKNOWN.asTag()).and(MetricsTags.TtdStatus.NONE.asTag());
    assertNotNull(registry.find(MicrometerBasedMetrics.METER_MESSAGES_RECEIVED).tags(expectedTags).timer());
}
Also used : Vertx(io.vertx.core.Vertx) Tags(io.micrometer.core.instrument.Tags) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Tags (io.micrometer.core.instrument.Tags)26 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)10 Test (org.junit.jupiter.api.Test)9 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)7 Timer (io.micrometer.core.instrument.Timer)6 Clock (io.micrometer.core.instrument.Clock)5 Counter (io.micrometer.core.instrument.Counter)5 DistributionSummary (io.micrometer.core.instrument.DistributionSummary)5 TimeUnit (java.util.concurrent.TimeUnit)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 MockClock (io.micrometer.core.instrument.MockClock)4 SimpleConfig (io.micrometer.core.instrument.simple.SimpleConfig)4 Duration (java.time.Duration)4 Scannable (reactor.core.Scannable)4 Metrics (reactor.util.Metrics)4 Sample (io.micrometer.core.instrument.Timer.Sample)3 Vertx (io.vertx.core.Vertx)3 File (java.io.File)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatCode (org.assertj.core.api.Assertions.assertThatCode)3