From d944092100696aa4a5974ef5c2e710547a824622 Mon Sep 17 00:00:00 2001 From: rghetia Date: Thu, 3 Jan 2019 12:56:06 -0800 Subject: [PATCH] Add Jaeger support for Adservice. (#111) This is the first service that exports to jaeger. Others to follow. Requires jaeger to be instantiated using - helm install --name jaeger stable/jaeger-operator - kubectl apply -f jaeger.yaml === jaeger.yaml Content === apiVersion: io.jaegertracing/v1alpha1 kind: Jaeger metadata: name: jaeger Above steps will be added to README in subsequent PR. --- kubernetes-manifests/adservice.yaml | 2 ++ src/adservice/build.gradle | 4 +-- .../src/main/java/hipstershop/AdService.java | 31 +++++++++++++------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/kubernetes-manifests/adservice.yaml b/kubernetes-manifests/adservice.yaml index dc0879f..0329a72 100644 --- a/kubernetes-manifests/adservice.yaml +++ b/kubernetes-manifests/adservice.yaml @@ -31,6 +31,8 @@ spec: env: - name: PORT value: "9555" + - name: JAEGER_ENABLED + value: "false" resources: requests: cpu: 200m diff --git a/src/adservice/build.gradle b/src/adservice/build.gradle index 59b0313..7450826 100644 --- a/src/adservice/build.gradle +++ b/src/adservice/build.gradle @@ -28,7 +28,6 @@ version = "0.1.0-SNAPSHOT" // CURRENT_OPENCENSUS_VERSION def opencensusVersion = "0.17.0" // LATEST_OPENCENSUS_RELEASE_VERSION def grpcVersion = "1.15.0" // CURRENT_GRPC_VERSION def jacksonVersion = "2.9.6" -def prometheusVersion = "0.3.0" tasks.withType(JavaCompile) { sourceCompatibility = '1.8' @@ -45,7 +44,7 @@ dependencies { compile fileTree(dir: offlineCompile, include: '*.jar') } else { compile "com.google.api.grpc:proto-google-common-protos:1.11.0", - "io.opencensus:opencensus-exporter-stats-prometheus:${opencensusVersion}", + "io.opencensus:opencensus-exporter-trace-jaeger:${opencensusVersion}", "io.opencensus:opencensus-exporter-stats-stackdriver:${opencensusVersion}", "io.opencensus:opencensus-exporter-trace-stackdriver:${opencensusVersion}", "io.opencensus:opencensus-exporter-trace-logging:${opencensusVersion}", @@ -53,7 +52,6 @@ dependencies { "io.grpc:grpc-stub:${grpcVersion}", "io.grpc:grpc-netty:${grpcVersion}", "io.grpc:grpc-services:${grpcVersion}", - "io.prometheus:simpleclient_httpserver:${prometheusVersion}", "org.apache.logging.log4j:log4j-core:2.11.1" runtime "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}", diff --git a/src/adservice/src/main/java/hipstershop/AdService.java b/src/adservice/src/main/java/hipstershop/AdService.java index 1224efe..b5d21a6 100644 --- a/src/adservice/src/main/java/hipstershop/AdService.java +++ b/src/adservice/src/main/java/hipstershop/AdService.java @@ -31,7 +31,7 @@ import io.grpc.services.*; import io.opencensus.common.Duration; import io.opencensus.common.Scope; import io.opencensus.contrib.grpc.metrics.RpcViews; -import io.opencensus.exporter.stats.prometheus.PrometheusStatsCollector; +import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter; import io.opencensus.exporter.trace.logging.LoggingTraceExporter; import io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration; import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter; @@ -46,7 +46,6 @@ import io.opencensus.trace.samplers.Samplers; import java.io.IOException; import java.util.Collection; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -201,11 +200,6 @@ public class AdService { public static void initStackdriver() { logger.info("Initialize StackDriver"); - // Registers all RPC views. - RpcViews.registerAllViews(); - - // Registers logging trace exporter. - LoggingTraceExporter.register(); long sleepTime = 10; /* seconds */ int maxAttempts = 5; boolean statsExporterRegistered = false; @@ -243,18 +237,35 @@ public class AdService { logger.info("StackDriver initialization complete."); } + static void initJaeger() { + boolean enabled = Boolean.parseBoolean(System.getenv("JAEGER_ENABLED")); + if (enabled) { + // Register Jaeger Tracing. + JaegerTraceExporter.createAndRegister("http://jaeger-collector:14268/api/traces", "adservice"); + logger.info("Jaeger initialization complete."); + } else { + logger.info("Jaeger initialization disabled."); + } + } + /** Main launches the server from the command line. */ public static void main(String[] args) throws IOException, InterruptedException { // Add final keyword to pass checkStyle. + // Registers all RPC views. + RpcViews.registerAllViews(); + + // Registers logging trace exporter. + LoggingTraceExporter.register(); + new Thread( new Runnable() { public void run(){ initStackdriver(); } }).start(); - // Register Prometheus exporters and export metrics to a Prometheus HTTPServer. - PrometheusStatsCollector.createAndRegister(); + // Register Jaeger + initJaeger(); // Start the RPC server. You shouldn't see any output from gRPC before this. logger.info("AdService starting."); @@ -262,4 +273,4 @@ public class AdService { service.start(); service.blockUntilShutdown(); } -} \ No newline at end of file +}