Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: use timer for latency #4174

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -5,6 +5,7 @@

package org.opensearch.dataprepper.model.sink;

import io.micrometer.core.instrument.Timer;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import io.micrometer.core.instrument.DistributionSummary;
import org.opensearch.dataprepper.model.event.EventHandle;
Expand All @@ -15,19 +16,19 @@
public class SinkLatencyMetrics {
public static final String INTERNAL_LATENCY = "PipelineLatency";
public static final String EXTERNAL_LATENCY = "EndToEndLatency";
private final DistributionSummary internalLatencySummary;
private final DistributionSummary externalLatencySummary;
private final Timer internalLatencyTimer;
private final Timer externalLatencyTimer;

public SinkLatencyMetrics(PluginMetrics pluginMetrics) {
internalLatencySummary = pluginMetrics.summary(INTERNAL_LATENCY);
externalLatencySummary = pluginMetrics.summary(EXTERNAL_LATENCY);
internalLatencyTimer = pluginMetrics.timer(INTERNAL_LATENCY);
externalLatencyTimer = pluginMetrics.timer(EXTERNAL_LATENCY);
}
public void update(final EventHandle eventHandle) {
Instant now = Instant.now();
internalLatencySummary.record(Duration.between(eventHandle.getInternalOriginationTime(), now).toMillis());
internalLatencyTimer.record(Duration.between(eventHandle.getInternalOriginationTime(), now));
if (eventHandle.getExternalOriginationTime() == null) {
return;
}
externalLatencySummary.record(Duration.between(eventHandle.getExternalOriginationTime(), now).toMillis());
externalLatencyTimer.record(Duration.between(eventHandle.getExternalOriginationTime(), now));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.opensearch.dataprepper.model.sink;

import io.micrometer.core.instrument.Timer;
import org.opensearch.dataprepper.metrics.PluginMetrics;
import org.opensearch.dataprepper.model.event.EventHandle;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
Expand All @@ -20,14 +21,15 @@
import static org.hamcrest.Matchers.greaterThanOrEqualTo;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

class SinkLatencyMetricsTest {

private PluginMetrics pluginMetrics;
private EventHandle eventHandle;
private SinkLatencyMetrics latencyMetrics;
private DistributionSummary internalLatencySummary;
private DistributionSummary externalLatencySummary;
private Timer internalLatencyTimer;
private Timer externalLatencyTimer;

public SinkLatencyMetrics createObjectUnderTest() {
return new SinkLatencyMetrics(pluginMetrics);
Expand All @@ -37,16 +39,14 @@ public SinkLatencyMetrics createObjectUnderTest() {
void setup() {
pluginMetrics = mock(PluginMetrics.class);
SimpleMeterRegistry registry = new SimpleMeterRegistry();
internalLatencySummary = DistributionSummary
internalLatencyTimer = Timer
.builder("internalLatency")
.baseUnit("milliseconds")
.register(registry);
externalLatencySummary = DistributionSummary
externalLatencyTimer = Timer
.builder("externalLatency")
.baseUnit("milliseconds")
.register(registry);
when(pluginMetrics.summary(SinkLatencyMetrics.INTERNAL_LATENCY)).thenReturn(internalLatencySummary);
when(pluginMetrics.summary(SinkLatencyMetrics.EXTERNAL_LATENCY)).thenReturn(externalLatencySummary);
when(pluginMetrics.timer(SinkLatencyMetrics.INTERNAL_LATENCY)).thenReturn(internalLatencyTimer);
when(pluginMetrics.timer(SinkLatencyMetrics.EXTERNAL_LATENCY)).thenReturn(externalLatencyTimer);
eventHandle = mock(EventHandle.class);
when(eventHandle.getInternalOriginationTime()).thenReturn(Instant.now());
latencyMetrics = createObjectUnderTest();
Expand All @@ -55,16 +55,16 @@ void setup() {
@Test
public void testInternalOriginationTime() {
latencyMetrics.update(eventHandle);
assertThat(internalLatencySummary.count(), equalTo(1L));
assertThat(internalLatencyTimer.count(), equalTo(1L));
}

@Test
public void testExternalOriginationTime() {
when(eventHandle.getExternalOriginationTime()).thenReturn(Instant.now().minusMillis(10));
latencyMetrics.update(eventHandle);
assertThat(internalLatencySummary.count(), equalTo(1L));
assertThat(externalLatencySummary.count(), equalTo(1L));
assertThat(externalLatencySummary.max(), greaterThanOrEqualTo(10.0));
assertThat(internalLatencyTimer.count(), equalTo(1L));
assertThat(externalLatencyTimer.count(), equalTo(1L));
assertThat(externalLatencyTimer.max(TimeUnit.MILLISECONDS), greaterThanOrEqualTo(10.0));
}
}

Expand Down
Loading