Skip to content

Commit

Permalink
tracer changes to okhttp (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
elopezcastro authored Feb 9, 2024
1 parent ee7ce0b commit 40cb48a
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 141 deletions.
8 changes: 4 additions & 4 deletions legend-depot-core-tracing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>io.opentracing</groupId>
<artifactId>opentracing-noop</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-okhttp3</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
Expand Down Expand Up @@ -86,10 +90,6 @@
<groupId>org.finos.legend.shared</groupId>
<artifactId>legend-shared-opentracing-base</artifactId>
</dependency>
<dependency>
<groupId>org.finos.legend.shared</groupId>
<artifactId>legend-shared-opentracing-jersey</artifactId>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.finos.legend.depot.core.services.api.tracing.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.finos.legend.opentracing.AuthenticationProvider;


public class OpenTracingConfiguration
{
Expand All @@ -30,8 +30,6 @@ public class OpenTracingConfiguration
@JsonProperty
private String serviceName;

@JsonProperty
private AuthenticationProvider authenticationProvider;

@JsonProperty
private TracerProvider tracerProvider;
Expand Down Expand Up @@ -66,16 +64,6 @@ public void setEnabled(boolean enabled)
this.enabled = enabled;
}

public AuthenticationProvider getAuthenticationProvider()
{
return authenticationProvider;
}

public void setAuthenticationProvider(AuthenticationProvider authenticationProvider)
{
this.authenticationProvider = authenticationProvider;
}

public TracerProvider getTracerProvider()
{
return tracerProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@

public interface TracerProvider
{
Tracer create(Sender sender, String serviceName);
Tracer create(OpenTracingConfiguration configuration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.finos.legend.opentracing.AuthenticationProvider;

public abstract class TracerProviderConfiguration
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.sun.istack.NotNull;

import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.Histogram;
Expand All @@ -28,6 +28,7 @@
import org.finos.legend.depot.core.services.api.metrics.PrometheusMetricsHandler;
import org.finos.legend.depot.core.services.tracing.resources.TracingResource;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,60 @@

package org.finos.legend.depot.core.services.tracing;

import brave.Tracing;
import brave.handler.SpanHandler;
import brave.opentracing.BraveTracer;
import brave.propagation.CurrentTraceContext;
import brave.sampler.Sampler;
import io.opentracing.Tracer;
import io.prometheus.client.CollectorRegistry;
import org.finos.legend.depot.core.services.api.tracing.configuration.OpenTracingConfiguration;
import org.finos.legend.depot.core.services.api.tracing.configuration.TracerProvider;
import org.finos.legend.engine.shared.core.operational.prometheus.TracingExports;
import org.finos.legend.opentracing.OpenTracing;
import zipkin2.codec.Encoding;
import zipkin2.reporter.InMemoryReporterMetrics;
import zipkin2.reporter.Sender;
import zipkin2.reporter.brave.AsyncZipkinSpanHandler;
import zipkin2.reporter.okhttp3.OkHttpSender;

import java.util.logging.Level;
import java.util.logging.LogManager;
import static org.finos.legend.depot.core.services.tracing.TracerFactory.DEFAULT_SERVICE_NAME;

public class DefaultTracerProvider implements TracerProvider
{
private static final int TRACE_MAX_BYTES = 5 * 1024 * 1024;

@Override
public Tracer create(Sender sender,String serviceName)
public Tracer create(OpenTracingConfiguration configuration)
{
Tracer tracer = OpenTracing.create(sender, serviceName, null, getMemoryMetricsReporter());
LogManager.getLogManager().getLogger("").setLevel(Level.FINE);
return tracer;
if (configuration.getOpenTracingUri() == null || configuration.getOpenTracingUri().isEmpty())
{
throw new IllegalArgumentException("Invalid uri, openTracingUri cannot be empty");
}
String serviceName = configuration.getServiceName() != null && !configuration.getServiceName().isEmpty() ?
configuration.getServiceName() : DEFAULT_SERVICE_NAME;
try
{
OkHttpSender sender = OkHttpSender.newBuilder()
.encoding(Encoding.JSON)
.messageMaxBytes(TRACE_MAX_BYTES)
.endpoint(configuration.getOpenTracingUri())
.build();

SpanHandler spanHandler = AsyncZipkinSpanHandler.newBuilder(sender).metrics(getMemoryMetricsReporter()).build();
Tracer tracer = BraveTracer.create(Tracing.newBuilder().localServiceName(serviceName)
.sampler(Sampler.ALWAYS_SAMPLE)
.currentTraceContext(CurrentTraceContext.Default.newBuilder().build())
.addSpanHandler(spanHandler)
.build());
return tracer;
}
catch (Exception e)
{
throw new IllegalArgumentException("Invalid openTracingUri provided", e);
}

}

private InMemoryReporterMetrics getMemoryMetricsReporter()
protected InMemoryReporterMetrics getMemoryMetricsReporter()
{
CollectorRegistry collectorRegistry = CollectorRegistry.defaultRegistry;
InMemoryReporterMetrics tracingMetrics = new InMemoryReporterMetrics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@
import io.opentracing.tag.StringTag;
import io.opentracing.tag.Tags;
import io.opentracing.util.GlobalTracer;
import org.finos.legend.depot.core.services.api.tracing.VoidAuthenticationProvider;
import org.finos.legend.depot.core.services.api.tracing.configuration.OpenTracingConfiguration;
import org.finos.legend.depot.core.services.api.tracing.configuration.TracerProvider;
import org.finos.legend.opentracing.AuthenticationProvider;
import org.finos.legend.opentracing.JerseyClientSender;
import org.slf4j.Logger;

import javax.inject.Singleton;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -40,11 +35,13 @@
public final class TracerFactory
{
private static final Logger LOGGER = org.slf4j.LoggerFactory.getLogger(TracerFactory.class);
private static final String DEFAULT_SERVICE_NAME = "legend-depot";
static final String DEFAULT_SERVICE_NAME = "legend-depot";
private static final String ERROR_TAG = "error";
private static TracerFactory INSTANCE;
private final Tracer tracer;



private TracerFactory(Tracer tracer)
{
this.tracer = tracer;
Expand All @@ -68,25 +65,10 @@ public static TracerFactory configure(OpenTracingConfiguration openTracingConfig
{
if (openTracingConfiguration != null && openTracingConfiguration.isEnabled())
{
if (openTracingConfiguration.getOpenTracingUri() == null || openTracingConfiguration.getOpenTracingUri().isEmpty())
{
throw new IllegalArgumentException("Invalid uri, openTracingUri cannot be empty");
}
AuthenticationProvider authenticationProvider = openTracingConfiguration.getAuthenticationProvider() != null ? openTracingConfiguration.getAuthenticationProvider() : new VoidAuthenticationProvider();
String serviceName = openTracingConfiguration.getServiceName() != null && !openTracingConfiguration.getServiceName().isEmpty() ?
openTracingConfiguration.getServiceName() : DEFAULT_SERVICE_NAME;
try (JerseyClientSender sender = new JerseyClientSender(new URI(openTracingConfiguration.getOpenTracingUri()), authenticationProvider);)
{
TracerProvider tracerProvider = openTracingConfiguration.getTracerProvider() != null ? openTracingConfiguration.getTracerProvider() : new DefaultTracerProvider();
Tracer tracer = tracerProvider.create(sender,serviceName);
GlobalTracer.registerIfAbsent(tracer);
INSTANCE = new TracerFactory(tracer);
}
catch (Exception e)
{
LOGGER.error("tracing configuration failed",e);
throw new IllegalArgumentException("Invalid openTracingUri provided", e);
}
TracerProvider tracerProvider = openTracingConfiguration.getTracerProvider() != null ? openTracingConfiguration.getTracerProvider() : new DefaultTracerProvider();
Tracer tracer = tracerProvider.create(openTracingConfiguration);
GlobalTracer.registerIfAbsent(tracer);
INSTANCE = new TracerFactory(tracer);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.finos.legend.depot.core.server.error.DepotServerExceptionMapper;
import org.finos.legend.depot.core.services.api.metrics.configuration.PrometheusMetricsProviderConfiguration;
import org.finos.legend.depot.core.services.api.tracing.configuration.TracerProviderConfiguration;
import org.finos.legend.depot.core.services.api.tracing.configuration.TracingAuthenticationProviderConfiguration;
import org.finos.legend.depot.core.services.metrics.PrometheusMetricsFactory;
import org.finos.legend.depot.store.StorageConfiguration;
import org.finos.legend.server.pac4j.LegendPac4jBundle;
Expand Down Expand Up @@ -92,7 +91,6 @@ protected boolean isEnvironmentVariableSubstitutionStrict()
protected void configureObjectMapper(Bootstrap<T> bootstrap)
{
StorageConfiguration.configureObjectMapper(bootstrap.getObjectMapper());
TracingAuthenticationProviderConfiguration.configureObjectMapper(bootstrap.getObjectMapper());
TracerProviderConfiguration.configureObjectMapper(bootstrap.getObjectMapper());
PrometheusMetricsProviderConfiguration.configureObjectMapper(bootstrap.getObjectMapper());
}
Expand Down
22 changes: 6 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
<maven.compiler>1.8</maven.compiler>
<maven.compiler>11</maven.compiler>
<java.version.range>[11,12),[17,18)</java.version.range>

<!-- Legend dependency versions -->
Expand Down Expand Up @@ -728,21 +728,6 @@
<artifactId>legend-shared-opentracing-base</artifactId>
<version>${legend.shared.version}</version>
</dependency>
<dependency>
<groupId>org.finos.legend.shared</groupId>
<artifactId>legend-shared-opentracing-jersey</artifactId>
<version>${legend.shared.version}</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.finos.legend.shared</groupId>
<artifactId>legend-shared-pac4j-gitlab</artifactId>
Expand Down Expand Up @@ -1266,6 +1251,11 @@
<artifactId>zipkin</artifactId>
<version>${zipkin.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-okhttp3</artifactId>
<version>${zipkin-reporter.version}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
Expand Down

0 comments on commit 40cb48a

Please sign in to comment.