From 151a88944af8408761bf05de3f0155fbe78a89e7 Mon Sep 17 00:00:00 2001 From: Henry Avetisyan Date: Mon, 28 Oct 2024 09:19:08 -0700 Subject: [PATCH] consistent indentation - formatting change only Signed-off-by: Henry Avetisyan --- .../metrics/impl/OpenTelemetryMetric.java | 241 +++++++-------- .../impl/OpenTelemetryMetricFactory.java | 16 +- .../impl/OpenTelemetryMetricFactoryTest.java | 24 +- .../impl/OpenTelemetryMetricsTest.java | 284 +++++++++--------- 4 files changed, 285 insertions(+), 280 deletions(-) diff --git a/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetric.java b/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetric.java index e26a562f765..377802d190b 100644 --- a/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetric.java +++ b/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetric.java @@ -25,128 +25,131 @@ import io.opentelemetry.context.Context; public class OpenTelemetryMetric implements Metric { - final Meter meter; - final Tracer tracer; - - private static final String REQUEST_DOMAIN_NAME = "requestDomainName"; - private static final String PRINCIPAL_DOMAIN_NAME = "principalDomainName"; - private static final String HTTP_METHOD_NAME = "httpMethodName"; - private static final String HTTP_STATUS = "httpStatus"; - private static final String API_NAME = "apiName"; - - public OpenTelemetryMetric(OpenTelemetry openTelemetry) { - meter = openTelemetry.getMeter("meter"); - tracer = openTelemetry.getTracer("tracer"); - } - - @Override - public void increment(String metric) { - LongCounter counter = meter.counterBuilder(metric).build(); - counter.add(1); - } - - @Override - public void increment(String metric, String requestDomainName) { - increment(metric, requestDomainName, 1); - } - - @Override - public void increment(String metric, String requestDomainName, int count) { - LongCounter counter = meter.counterBuilder(metric).build(); - Attributes attributes = Attributes.builder() - .put(REQUEST_DOMAIN_NAME, requestDomainName) - .build(); - counter.add(count, attributes); - } - - @Override - public void increment(String metric, String requestDomainName, String principalDomainName) { - increment(metric, requestDomainName, principalDomainName, 1); - } - - @Override - public void increment(String metric, String requestDomainName, String principalDomainName, String httpMethod, int httpStatus, String apiName) { - LongCounter counter = meter.counterBuilder(metric).build(); - Attributes attributes = Attributes.builder() - .put(REQUEST_DOMAIN_NAME, requestDomainName) - .put(PRINCIPAL_DOMAIN_NAME, principalDomainName) - .put(HTTP_METHOD_NAME, httpMethod) - .put(HTTP_STATUS, Integer.toString(httpStatus)) - .put(API_NAME, apiName) - .build(); - counter.add(1, attributes); - } - - @Override - public void increment(String metric, String requestDomainName, String principalDomainName, int count) { - LongCounter counter = meter.counterBuilder(metric).build(); - Attributes attributes = Attributes.builder() - .put(REQUEST_DOMAIN_NAME, requestDomainName) - .put(PRINCIPAL_DOMAIN_NAME, principalDomainName) - .build(); - counter.add(count, attributes); - } - - @Override - public Object startTiming(String metric, String requestDomainName) { - Span span = tracer.spanBuilder(metric).startSpan(); - Context context = Context.current().with(span); - return new Timer(context, System.currentTimeMillis(), span); - } - - @Override - public void stopTiming(Object timerMetric) { - //not necessary method - } - - @Override - public void stopTiming(Object timerMetric, String requestDomainName, String principalDomainName) { - stopTiming(timerMetric, requestDomainName, principalDomainName, null, -1, null); - } - - @Override - public void stopTiming(Object timerMetric, String requestDomainName, String principalDomainName, - String httpMethod, int httpStatus, String apiName) { - Timer timer = (Timer) timerMetric; - long duration = System.currentTimeMillis() - timer.start; - Span span = timer.getSpan(); - span.setAttribute("duration", duration); - span.setAttribute(REQUEST_DOMAIN_NAME, requestDomainName); - span.setAttribute(PRINCIPAL_DOMAIN_NAME, principalDomainName); - - if (httpMethod != null) { - span.setAttribute(HTTP_METHOD_NAME, httpMethod); + final Meter meter; + final Tracer tracer; + + private static final String REQUEST_DOMAIN_NAME = "requestDomainName"; + private static final String PRINCIPAL_DOMAIN_NAME = "principalDomainName"; + private static final String HTTP_METHOD_NAME = "httpMethodName"; + private static final String HTTP_STATUS = "httpStatus"; + private static final String API_NAME = "apiName"; + + public OpenTelemetryMetric(OpenTelemetry openTelemetry) { + meter = openTelemetry.getMeter("meter"); + tracer = openTelemetry.getTracer("tracer"); } - if (httpStatus != -1) { - span.setAttribute(HTTP_STATUS, Integer.toString(httpStatus)); + + @Override + public void increment(String metric) { + LongCounter counter = meter.counterBuilder(metric).build(); + counter.add(1); + } + + @Override + public void increment(String metric, String requestDomainName) { + increment(metric, requestDomainName, 1); + } + + @Override + public void increment(String metric, String requestDomainName, int count) { + LongCounter counter = meter.counterBuilder(metric).build(); + Attributes attributes = Attributes.builder() + .put(REQUEST_DOMAIN_NAME, requestDomainName) + .build(); + counter.add(count, attributes); + } + + @Override + public void increment(String metric, String requestDomainName, String principalDomainName) { + increment(metric, requestDomainName, principalDomainName, 1); + } + + @Override + public void increment(String metric, String requestDomainName, String principalDomainName, String httpMethod, + int httpStatus, String apiName) { + LongCounter counter = meter.counterBuilder(metric).build(); + Attributes attributes = Attributes.builder() + .put(REQUEST_DOMAIN_NAME, requestDomainName) + .put(PRINCIPAL_DOMAIN_NAME, principalDomainName) + .put(HTTP_METHOD_NAME, httpMethod) + .put(HTTP_STATUS, Integer.toString(httpStatus)) + .put(API_NAME, apiName) + .build(); + counter.add(1, attributes); + } + + @Override + public void increment(String metric, String requestDomainName, String principalDomainName, int count) { + LongCounter counter = meter.counterBuilder(metric).build(); + Attributes attributes = Attributes.builder() + .put(REQUEST_DOMAIN_NAME, requestDomainName) + .put(PRINCIPAL_DOMAIN_NAME, principalDomainName) + .build(); + counter.add(count, attributes); + } + + @Override + public Object startTiming(String metric, String requestDomainName) { + Span span = tracer.spanBuilder(metric).startSpan(); + Context context = Context.current().with(span); + return new Timer(context, System.currentTimeMillis(), span); + } + + @Override + public void stopTiming(Object timerMetric) { + //not necessary method + } + + @Override + public void stopTiming(Object timerMetric, String requestDomainName, String principalDomainName) { + stopTiming(timerMetric, requestDomainName, principalDomainName, null, -1, null); + } + + @Override + public void stopTiming(Object timerMetric, String requestDomainName, String principalDomainName, + String httpMethod, int httpStatus, String apiName) { + Timer timer = (Timer) timerMetric; + long duration = System.currentTimeMillis() - timer.start; + Span span = timer.getSpan(); + span.setAttribute("duration", duration); + span.setAttribute(REQUEST_DOMAIN_NAME, requestDomainName); + span.setAttribute(PRINCIPAL_DOMAIN_NAME, principalDomainName); + + if (httpMethod != null) { + span.setAttribute(HTTP_METHOD_NAME, httpMethod); + } + if (httpStatus != -1) { + span.setAttribute(HTTP_STATUS, Integer.toString(httpStatus)); + } + if (apiName != null) { + span.setAttribute(API_NAME, apiName); + } + span.end(); } - if (apiName != null) { - span.setAttribute(API_NAME, apiName); + + @Override + public void flush() { + //doesn't require flushing } - span.end(); - } - - @Override - public void flush() { - //doesn't require flushing - } - - @Override - public void quit() { - //don't need to quit anything - } - - static class Timer { - private final Context context; - private final long start; - private final Span span; - public Timer(Context context, long start, Span span) { - this.context = context; - this.start = start; - this.span = span; + + @Override + public void quit() { + //don't need to quit anything } - public Span getSpan() { - return span; + + static class Timer { + private final Context context; + private final long start; + private final Span span; + + public Timer(Context context, long start, Span span) { + this.context = context; + this.start = start; + this.span = span; + } + + public Span getSpan() { + return span; + } } - } } diff --git a/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactory.java b/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactory.java index e9137f529bc..78f3a43852d 100644 --- a/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactory.java +++ b/libs/java/server_common/src/main/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactory.java @@ -30,13 +30,13 @@ You need to set the endpoint (OTEL_EXPORTER_OTLP_ENDPOINT) which is defaulted to */ public class OpenTelemetryMetricFactory implements MetricFactory { - @Override - public Metric create() { - OpenTelemetry openTelemetry = initialize(); - return new OpenTelemetryMetric(openTelemetry); - } + @Override + public Metric create() { + OpenTelemetry openTelemetry = initialize(); + return new OpenTelemetryMetric(openTelemetry); + } - public OpenTelemetry initialize() { - return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); - } + public OpenTelemetry initialize() { + return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + } } diff --git a/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactoryTest.java b/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactoryTest.java index c08a3e311e8..894d5649384 100644 --- a/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactoryTest.java +++ b/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricFactoryTest.java @@ -16,21 +16,23 @@ package com.yahoo.athenz.common.metrics.impl; import static org.testng.Assert.*; + import com.yahoo.athenz.common.metrics.Metric; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; + public class OpenTelemetryMetricFactoryTest { - private OpenTelemetryMetricFactory factory; + private OpenTelemetryMetricFactory factory; - @BeforeMethod - public void setUp() { - factory = new OpenTelemetryMetricFactory(); - } + @BeforeMethod + public void setUp() { + factory = new OpenTelemetryMetricFactory(); + } - @Test - public void testCreate() { - Metric metric = factory.create(); - assertNotNull(metric); - assertTrue(metric instanceof OpenTelemetryMetric); - } + @Test + public void testCreate() { + Metric metric = factory.create(); + assertNotNull(metric); + assertTrue(metric instanceof OpenTelemetryMetric); + } } diff --git a/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricsTest.java b/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricsTest.java index da544a444a7..76cedc672a6 100644 --- a/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricsTest.java +++ b/libs/java/server_common/src/test/java/com/yahoo/athenz/common/metrics/impl/OpenTelemetryMetricsTest.java @@ -34,146 +34,146 @@ import org.mockito.ArgumentCaptor; public class OpenTelemetryMetricsTest { - private Meter meter; - private Tracer tracer; - private LongCounter counter; - private Span span; - private OpenTelemetryMetric metric; - - @BeforeMethod - public void setUp() { - meter = mock(Meter.class); - tracer = mock(Tracer.class); - counter = mock(LongCounter.class); - span = mock(Span.class); - OpenTelemetry openTelemetry = mock(OpenTelemetry.class); - - LongCounterBuilder counterBuilder = mock(LongCounterBuilder.class); - when(meter.counterBuilder(anyString())).thenReturn(counterBuilder); - when(counterBuilder.build()).thenReturn(counter); - - SpanBuilder spanBuilder = mock(SpanBuilder.class); - when(tracer.spanBuilder(anyString())).thenReturn(spanBuilder); - when(spanBuilder.startSpan()).thenReturn(span); - - when(openTelemetry.getMeter("meter")).thenReturn(meter); - when(openTelemetry.getTracer("tracer")).thenReturn(tracer); - - metric = new OpenTelemetryMetric(openTelemetry); - } - - @Test - public void testIncrementMetric() { - metric.increment("testIncrement"); - verify(counter).add(1L); - } - - @Test - public void testIncrementMetricRequest() { - metric.increment("testMetric", "testRequestDomain"); - ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); - verify(counter).add(eq(1L), captor.capture()); - Attributes attributes = captor.getValue(); - assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); - } - - @Test - public void testIncrementMetricRequestCount() { - metric.increment("testMetric", "testRequestDomain", 3); - ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); - verify(counter).add(eq(3L), captor.capture()); - Attributes attributes = captor.getValue(); - assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); - } - - @Test - public void testIncrementMetricRequestPrincipal() { - metric.increment("testMetric", "testRequestDomain", "testPrincipalDomain"); - ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); - verify(counter).add(eq(1L), captor.capture()); - Attributes attributes = captor.getValue(); - assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); - assertEquals(attributes.get(AttributeKey.stringKey("principalDomainName")), "testPrincipalDomain"); - } - - @Test - public void testIncrementMetricRequestPrincipalCount() { - metric.increment("testMetric", "testRequestDomain", - "testPrincipalDomain", 5); - ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); - verify(counter).add(eq(5L), captor.capture()); - Attributes attributes = captor.getValue(); - assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); - assertEquals(attributes.get(AttributeKey.stringKey("principalDomainName")), "testPrincipalDomain"); - } - - @Test - public void testIncrementAllAttributes() { - metric.increment("testMetric", "testRequestDomain", - "testPrincipalDomain", "GET", 200, "testAPI"); - ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); - verify(counter).add(eq(1L), captor.capture()); - Attributes attributes = captor.getValue(); - assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); - assertEquals(attributes.get(AttributeKey.stringKey("principalDomainName")), "testPrincipalDomain"); - assertEquals(attributes.get(AttributeKey.stringKey("httpMethodName")), "GET"); - assertEquals(attributes.get(AttributeKey.stringKey("httpStatus")), "200"); - assertEquals(attributes.get(AttributeKey.stringKey("apiName")), "testAPI"); - } - - @Test - public void testStartTiming() { - Object timerMetric = metric.startTiming("testMetric", "testRequestDomain"); - assertNotNull(timerMetric); - assertTrue(timerMetric instanceof OpenTelemetryMetric.Timer); - OpenTelemetryMetric.Timer timer = (OpenTelemetryMetric.Timer) timerMetric; - assertEquals(span, timer.getSpan()); - } - - @Test - public void testStopTimingTimer() { - OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(), - System.currentTimeMillis(), span); - metric.stopTiming(timer); - verifyNoInteractions(meter, tracer, counter, span); - } - - @Test - public void testStopTimingTimerRequestPrincipal() { - OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(), - System.currentTimeMillis(), span); - metric.stopTiming(timer, "testRequestDomain", "testPrincipalDomain"); - verify(span).setAttribute("requestDomainName", "testRequestDomain"); - verify(span).setAttribute("principalDomainName", "testPrincipalDomain"); - verify(span).setAttribute(eq("duration"), anyLong()); - verify(span).end(); - } - - @Test - public void testStopTimingAllAttributes() { - OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(), - System.currentTimeMillis(), span); - metric.stopTiming(timer, "testRequestDomain", - "testPrincipalDomain", "GET", 200, "testAPI"); - verify(span).setAttribute("requestDomainName", "testRequestDomain"); - verify(span).setAttribute("principalDomainName", "testPrincipalDomain"); - verify(span).setAttribute("httpMethodName", "GET"); - verify(span).setAttribute("httpStatus", "200"); - verify(span).setAttribute("apiName", "testAPI"); - verify(span).setAttribute(eq("duration"), anyLong()); - verify(span).end(); - } - - - @Test - public void testFlush() { - metric.flush(); - verifyNoInteractions(meter, tracer, counter, span); - } - - @Test - public void testQuit() { - metric.quit(); - verifyNoInteractions(meter, tracer, counter, span); - } + private Meter meter; + private Tracer tracer; + private LongCounter counter; + private Span span; + private OpenTelemetryMetric metric; + + @BeforeMethod + public void setUp() { + meter = mock(Meter.class); + tracer = mock(Tracer.class); + counter = mock(LongCounter.class); + span = mock(Span.class); + OpenTelemetry openTelemetry = mock(OpenTelemetry.class); + + LongCounterBuilder counterBuilder = mock(LongCounterBuilder.class); + when(meter.counterBuilder(anyString())).thenReturn(counterBuilder); + when(counterBuilder.build()).thenReturn(counter); + + SpanBuilder spanBuilder = mock(SpanBuilder.class); + when(tracer.spanBuilder(anyString())).thenReturn(spanBuilder); + when(spanBuilder.startSpan()).thenReturn(span); + + when(openTelemetry.getMeter("meter")).thenReturn(meter); + when(openTelemetry.getTracer("tracer")).thenReturn(tracer); + + metric = new OpenTelemetryMetric(openTelemetry); + } + + @Test + public void testIncrementMetric() { + metric.increment("testIncrement"); + verify(counter).add(1L); + } + + @Test + public void testIncrementMetricRequest() { + metric.increment("testMetric", "testRequestDomain"); + ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); + verify(counter).add(eq(1L), captor.capture()); + Attributes attributes = captor.getValue(); + assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); + } + + @Test + public void testIncrementMetricRequestCount() { + metric.increment("testMetric", "testRequestDomain", 3); + ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); + verify(counter).add(eq(3L), captor.capture()); + Attributes attributes = captor.getValue(); + assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); + } + + @Test + public void testIncrementMetricRequestPrincipal() { + metric.increment("testMetric", "testRequestDomain", "testPrincipalDomain"); + ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); + verify(counter).add(eq(1L), captor.capture()); + Attributes attributes = captor.getValue(); + assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); + assertEquals(attributes.get(AttributeKey.stringKey("principalDomainName")), "testPrincipalDomain"); + } + + @Test + public void testIncrementMetricRequestPrincipalCount() { + metric.increment("testMetric", "testRequestDomain", + "testPrincipalDomain", 5); + ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); + verify(counter).add(eq(5L), captor.capture()); + Attributes attributes = captor.getValue(); + assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); + assertEquals(attributes.get(AttributeKey.stringKey("principalDomainName")), "testPrincipalDomain"); + } + + @Test + public void testIncrementAllAttributes() { + metric.increment("testMetric", "testRequestDomain", + "testPrincipalDomain", "GET", 200, "testAPI"); + ArgumentCaptor captor = ArgumentCaptor.forClass(Attributes.class); + verify(counter).add(eq(1L), captor.capture()); + Attributes attributes = captor.getValue(); + assertEquals(attributes.get(AttributeKey.stringKey("requestDomainName")), "testRequestDomain"); + assertEquals(attributes.get(AttributeKey.stringKey("principalDomainName")), "testPrincipalDomain"); + assertEquals(attributes.get(AttributeKey.stringKey("httpMethodName")), "GET"); + assertEquals(attributes.get(AttributeKey.stringKey("httpStatus")), "200"); + assertEquals(attributes.get(AttributeKey.stringKey("apiName")), "testAPI"); + } + + @Test + public void testStartTiming() { + Object timerMetric = metric.startTiming("testMetric", "testRequestDomain"); + assertNotNull(timerMetric); + assertTrue(timerMetric instanceof OpenTelemetryMetric.Timer); + OpenTelemetryMetric.Timer timer = (OpenTelemetryMetric.Timer) timerMetric; + assertEquals(span, timer.getSpan()); + } + + @Test + public void testStopTimingTimer() { + OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(), + System.currentTimeMillis(), span); + metric.stopTiming(timer); + verifyNoInteractions(meter, tracer, counter, span); + } + + @Test + public void testStopTimingTimerRequestPrincipal() { + OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(), + System.currentTimeMillis(), span); + metric.stopTiming(timer, "testRequestDomain", "testPrincipalDomain"); + verify(span).setAttribute("requestDomainName", "testRequestDomain"); + verify(span).setAttribute("principalDomainName", "testPrincipalDomain"); + verify(span).setAttribute(eq("duration"), anyLong()); + verify(span).end(); + } + + @Test + public void testStopTimingAllAttributes() { + OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(), + System.currentTimeMillis(), span); + metric.stopTiming(timer, "testRequestDomain", + "testPrincipalDomain", "GET", 200, "testAPI"); + verify(span).setAttribute("requestDomainName", "testRequestDomain"); + verify(span).setAttribute("principalDomainName", "testPrincipalDomain"); + verify(span).setAttribute("httpMethodName", "GET"); + verify(span).setAttribute("httpStatus", "200"); + verify(span).setAttribute("apiName", "testAPI"); + verify(span).setAttribute(eq("duration"), anyLong()); + verify(span).end(); + } + + + @Test + public void testFlush() { + metric.flush(); + verifyNoInteractions(meter, tracer, counter, span); + } + + @Test + public void testQuit() { + metric.quit(); + verifyNoInteractions(meter, tracer, counter, span); + } }