Skip to content

Commit

Permalink
Updated to current implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
salladi30 committed Aug 14, 2024
1 parent 6e2b907 commit 36e0db0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 97 deletions.
34 changes: 18 additions & 16 deletions libs/java/server_common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
<packaging>jar</packaging>

<properties>
<code.coverage.min>0.9476</code.coverage.min>
<code.coverage.min>0.9482</code.coverage.min>
<commons.dbcp2.version>2.12.0</commons.dbcp2.version>
<commons.pool2.version>2.12.0</commons.pool2.version>
<jakarta.mail.version>2.1.3</jakarta.mail.version>
<angus.mail.version>2.0.3</angus.mail.version>
<opentelemetry.version>1.39.0</opentelemetry.version>
<protobuf.java.version>3.25.3</protobuf.java.version>
<pulsar.client.version>3.2.3</pulsar.client.version>
<pulsar.client.version>3.3.0</pulsar.client.version>
<aws.secretmanager.version>2.0.2</aws.secretmanager.version>
<uuid.version>5.0.0</uuid.version>
<uuid.version>5.1.0</uuid.version>
<hamcrest.json.version>0.3</hamcrest.json.version>
<opentelemetry.version>1.40.0</opentelemetry.version>
</properties>

<dependencyManagement>
Expand All @@ -53,6 +53,14 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
Expand Down Expand Up @@ -127,7 +135,7 @@
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>${uuid.version}</version>
</dependency>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>athenz-auth-core</artifactId>
Expand Down Expand Up @@ -311,11 +319,11 @@
<artifactId>athenz-zms-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.yahoo.athenz</groupId>
<artifactId>athenz-zms-java-client</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.yahoo.athenz</groupId>
<artifactId>athenz-zms-java-client</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
Expand All @@ -338,12 +346,6 @@
<artifactId>aws-secretsmanager-jdbc</artifactId>
<version>${aws.secretmanager.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
<scope>compile</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ public class OpenTelemetryMetric implements Metric {
private static final String HTTP_STATUS = "httpStatus";
private static final String API_NAME = "apiName";

public OpenTelemetryMetric(OpenTelemetryMetricFactory factory) {
//OpenTelemetryMetricFactory factory = new OpenTelemetryMetricFactory();
OpenTelemetry openTelemetry = factory.initialize();
public OpenTelemetryMetric(OpenTelemetry openTelemetry) {
meter = openTelemetry.getMeter("meter");
tracer = openTelemetry.getTracer("tracer");
}

public OpenTelemetryMetric(Meter meter, Tracer tracer) {
this.meter = meter;
this.tracer = tracer;
}

@Override
public void increment(String metric) {
LongCounter counter = meter.counterBuilder(metric).build();
Expand Down Expand Up @@ -67,7 +60,6 @@ public void increment(String metric, String requestDomainName, String principalD
.put(API_NAME, apiName)
.build();
counter.add(1, attributes);

}

@Override
Expand All @@ -84,41 +76,39 @@ public void increment(String metric, String requestDomainName, String principalD
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, 0);
return new Timer(context, System.currentTimeMillis(), span);
}

@Override
public void stopTiming(Object timerMetric) {
Timer timer = (Timer) timerMetric;
timer.duration = System.currentTimeMillis() - timer.getStart();
timer.getSpan().end();
//not necessary method
}

@Override
public void stopTiming(Object timerMetric, String requestDomainName, String principalDomainName) {
Timer timer = (Timer) timerMetric;
long duration = System.currentTimeMillis() - timer.start;
timer.duration = duration;
timer.getSpan().end();
LongCounter counter = meter.counterBuilder("timing").build();
Attributes attributes = Attributes.builder().put(REQUEST_DOMAIN_NAME, requestDomainName)
.put(PRINCIPAL_DOMAIN_NAME, principalDomainName).build();
counter.add(duration, attributes);
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;
timer.duration = duration;
timer.getSpan().end();
LongCounter counter = meter.counterBuilder("timing").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(duration, attributes);
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();
}

@Override
Expand All @@ -135,15 +125,10 @@ static class Timer {
private final Context context;
private final long start;
private final Span span;
private long duration;
public Timer(Context context, long start, Span span, long duration) {
public Timer(Context context, long start, Span span) {
this.context = context;
this.start = start;
this.span = span;
this.duration = duration;
}
public long getStart() {
return start;
}
public Span getSpan() {
return span;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,25 @@

import com.yahoo.athenz.common.metrics.Metric;
import com.yahoo.athenz.common.metrics.MetricFactory;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.exporter.logging.LoggingMetricExporter;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import java.time.Duration;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;

/*
In order to use the otlp exporters you need to configure the environment variables.
You need to set the endpoint (OTEL_EXPORTER_OTLP_ENDPOINT) which is defaulted to
"http:://localhost:4317" and the attributes (OTEL_RESOURCE_ATTRIBUTES) which is defaulted
to "service.name=my-service." AutoConfiguredOpenTelemetrySdk automatically reads the
configuration and sets up the exporter.
*/

public class OpenTelemetryMetricFactory implements MetricFactory {
@Override
public Metric create() {
return new OpenTelemetryMetric(this);
OpenTelemetry openTelemetry = initialize();
return new OpenTelemetryMetric(openTelemetry);
}

public OpenTelemetry initialize() {
final long interval = 800L;
MetricReader reader = PeriodicMetricReader.builder(LoggingMetricExporter.create())
.setInterval(Duration.ofMillis(interval))
.build();
SdkMeterProvider meterProvider = SdkMeterProvider.builder().registerMetricReader(reader).build();
SdkTracerProvider tracerProvider = SdkTracerProvider.builder().
addSpanProcessor(SimpleSpanProcessor.create(LoggingSpanExporter.create())).build();

return OpenTelemetrySdk.builder().setMeterProvider(meterProvider)
.setTracerProvider(tracerProvider).buildAndRegisterGlobal();
return AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package com.yahoo.athenz.common.metrics.impl;

import static org.testng.Assert.*;
import com.yahoo.athenz.common.metrics.Metric;
import org.testng.annotations.BeforeMethod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@ public class OpenTelemetryMetricsTest {
private LongCounter counter;
private Span span;
private OpenTelemetryMetric metric;
private OpenTelemetryMetricFactory factory;
private OpenTelemetry openTelemetry;

@BeforeMethod
public void setUp() {
meter = mock(Meter.class);
tracer = mock(Tracer.class);
counter = mock(LongCounter.class);
span = mock(Span.class);
factory = mock(OpenTelemetryMetricFactory.class);
openTelemetry = mock(OpenTelemetry.class);
OpenTelemetry openTelemetry = mock(OpenTelemetry.class);

LongCounterBuilder counterBuilder = mock(LongCounterBuilder.class);
when(meter.counterBuilder(anyString())).thenReturn(counterBuilder);
Expand All @@ -44,23 +41,10 @@ public void setUp() {
when(tracer.spanBuilder(anyString())).thenReturn(spanBuilder);
when(spanBuilder.startSpan()).thenReturn(span);

metric = new OpenTelemetryMetric(meter, tracer);

when(factory.initialize()).thenReturn(openTelemetry);
when(openTelemetry.getMeter("meter")).thenReturn(meter);
when(openTelemetry.getTracer("tracer")).thenReturn(tracer);
}

@Test
public void testInitialConstructor() {
metric = new OpenTelemetryMetric(factory);
assertNotNull(metric);
assertEquals(metric.meter, meter);
assertEquals(metric.tracer, tracer);

verify(factory).initialize();
verify(openTelemetry).getMeter("meter");
verify(openTelemetry).getTracer("tracer");
metric = new OpenTelemetryMetric(openTelemetry);
}

@Test
Expand Down Expand Up @@ -134,30 +118,38 @@ public void testStartTiming() {
@Test
public void testStopTimingTimer() {
OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(),
System.currentTimeMillis(), span, 0);
System.currentTimeMillis(), span);
metric.stopTiming(timer);
verify(span).end();
verifyNoInteractions(meter, tracer, counter, span);
}

@Test
public void testStopTimingTimerRequestPrincipal() {
OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(),
System.currentTimeMillis(), span, 0);
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();
verify(counter).add(anyLong(), any(Attributes.class));
}

@Test
public void testStopTimingAllAttributes() {
OpenTelemetryMetric.Timer timer = new OpenTelemetryMetric.Timer(Context.current(),
System.currentTimeMillis(), span, 0);
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();
verify(counter).add(anyLong(), any(Attributes.class));
}


@Test
public void testFlush() {
metric.flush();
Expand Down

0 comments on commit 36e0db0

Please sign in to comment.