You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zipkin.apache.org by ab...@apache.org on 2019/01/31 10:25:32 UTC

[incubator-zipkin-brave-karaf] 10/43: Adding okhttp3 reporter and tracer

This is an automated email from the ASF dual-hosted git repository.

abesto pushed a commit to branch jenkinsfile-tweaks
in repository https://gitbox.apache.org/repos/asf/incubator-zipkin-brave-karaf.git

commit abe37eeef3bf576a402891582c9033f3992988e1
Author: Kamesh Sampath <ka...@hotmail.com>
AuthorDate: Mon Jul 3 17:13:56 2017 +0530

    Adding okhttp3 reporter and tracer
---
 brave-exporter-okhttp3/pom.xml                     | 103 +++++++++++++++
 .../okhttp3/internal/OkHttp3ReporterExporter.java  | 140 +++++++++++++++++++++
 brave-features/src/main/resources/features.xml     |  15 +++
 pom.xml                                            |   3 +-
 4 files changed, 260 insertions(+), 1 deletion(-)

diff --git a/brave-exporter-okhttp3/pom.xml b/brave-exporter-okhttp3/pom.xml
new file mode 100644
index 0000000..17827bb
--- /dev/null
+++ b/brave-exporter-okhttp3/pom.xml
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Copyright 2017 The OpenZipkin Authors
+
+    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+    in compliance with the License. You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software distributed under the License
+    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+    or implied. See the License for the specific language governing permissions and limitations under
+    the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>brave-karaf-parent</artifactId>
+        <groupId>io.zipkin.brave.karaf</groupId>
+        <version>1.0.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>brave-exporter-okhttp3</artifactId>
+
+    <properties>
+        <main.basedir>${project.basedir}/..</main.basedir>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.core</artifactId>
+            <version>6.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <version>6.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>io.zipkin.java</groupId>
+            <artifactId>zipkin</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>io.zipkin.brave</groupId>
+            <artifactId>brave-context-slf4j</artifactId>
+            <version>${brave.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>io.zipkin.reporter</groupId>
+            <artifactId>zipkin-reporter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>io.zipkin.reporter</groupId>
+            <artifactId>zipkin-sender-okhttp3</artifactId>
+            <version>${zipkin.reporter.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>biz.aQute.bnd</groupId>
+                <artifactId>bnd-maven-plugin</artifactId>
+                <version>3.3.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>bnd-process</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>3.0.2</version>
+                <configuration>
+                    <archive>
+                        <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
+                    </archive>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
\ No newline at end of file
diff --git a/brave-exporter-okhttp3/src/main/java/io/zipkin/brave/osgi/exporter/okhttp3/internal/OkHttp3ReporterExporter.java b/brave-exporter-okhttp3/src/main/java/io/zipkin/brave/osgi/exporter/okhttp3/internal/OkHttp3ReporterExporter.java
new file mode 100644
index 0000000..55aecac
--- /dev/null
+++ b/brave-exporter-okhttp3/src/main/java/io/zipkin/brave/osgi/exporter/okhttp3/internal/OkHttp3ReporterExporter.java
@@ -0,0 +1,140 @@
+/**
+ * Copyright 2016-2017 The OpenZipkin Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package io.zipkin.brave.osgi.exporter.okhttp3.internal;
+
+import brave.Tracing;
+import brave.context.slf4j.MDCCurrentTraceContext;
+import brave.sampler.Sampler;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+import zipkin.Span;
+import zipkin.reporter.AsyncReporter;
+import zipkin.reporter.Encoding;
+import zipkin.reporter.Reporter;
+import zipkin.reporter.okhttp3.OkHttpSender;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+@Component(
+        immediate = true,
+        name = "io.zipkin.reporter.okhttp3",
+        property = {"sender=okhttp3"},
+        configurationPolicy = ConfigurationPolicy.REQUIRE
+)
+@Designate(ocd = OkHttp3ReporterExporter.Config.class)
+public class OkHttp3ReporterExporter {
+
+    private static final String OVERRIDE_PREFIX = "okhttp3.";
+
+    @SuppressWarnings("rawtypes")
+    private ServiceRegistration<Reporter> reporterServiceRegistration;
+    @SuppressWarnings("rawtypes")
+    private ServiceRegistration<Tracing> tracingServiceRegistration;
+
+    private AsyncReporter<Span> reporter;
+    private Tracing tracing;
+
+    private BundleContext context;
+
+    @ObjectClassDefinition(name = "Zipkin Reporter OkHttp3")
+    @interface Config {
+        String zipkinUrl() default "localhost:9411";
+
+        boolean compressionEnabled() default true;
+
+        Encoding encoding() default Encoding.THRIFT;
+
+        int messageMaxBytes() default 1000000;
+
+        String localServiceName() default "zipkin-okhttp3";
+
+        String samplerFilter() default "(type=always)";
+    }
+
+    @Activate
+    public void activate(Config config, BundleContext context, Map<String, String> properties) {
+        this.context = context;
+        OkHttpSender sender = createSender(config, properties);
+        reporter = AsyncReporter.create(sender);
+
+        reporterServiceRegistration = context.registerService(Reporter.class, reporter,
+                new Hashtable<String, String>(properties));
+
+        tracing = httpTracer(config, properties, reporter);
+        tracingServiceRegistration = context.registerService(Tracing.class, tracing,
+                new Hashtable<String, String>(properties));
+    }
+
+    @SuppressWarnings("unchecked")
+    public Tracing httpTracer(Config config, Map<String, String> properties, Reporter reporter) {
+
+        Sampler sampler = Sampler.ALWAYS_SAMPLE;
+
+        try {
+            ServiceReference<Sampler>[] samplers =
+                    (ServiceReference<Sampler>[]) context.getAllServiceReferences(
+                            Sampler.class.getName(), config.samplerFilter());
+            if (samplers != null && samplers.length > 0) {
+                sampler = context.getService(samplers[0]);
+            }
+
+        } catch (InvalidSyntaxException e) {
+
+        }
+
+        Tracing.Builder tracingBuilder = Tracing.newBuilder();
+        tracingBuilder.localServiceName(config.localServiceName());
+        tracingBuilder.sampler(sampler);
+        tracingBuilder.reporter(reporter);
+        tracingBuilder.currentTraceContext(MDCCurrentTraceContext.create());
+
+        return tracingBuilder.build();
+    }
+
+
+    OkHttpSender createSender(Config config, Map<String, String> properties) {
+        return OkHttpSender.builder()
+                .endpoint(config.zipkinUrl())
+                .compressionEnabled(config.compressionEnabled())
+                .messageMaxBytes(config.messageMaxBytes())
+                .build();
+    }
+
+    private HashMap<String, String> getOverrides(Map<String, String> properties) {
+        HashMap<String, String> overrides = new HashMap<String, String>();
+        for (String key : properties.keySet()) {
+            if (key.startsWith(OVERRIDE_PREFIX)) {
+                overrides.put(key.substring(OVERRIDE_PREFIX.length() - 1), properties.get(key));
+            }
+        }
+        return overrides;
+    }
+
+    @Deactivate
+    public void deactive() {
+        tracingServiceRegistration.unregister();
+        reporterServiceRegistration.unregister();
+        reporter.close();
+    }
+}
diff --git a/brave-features/src/main/resources/features.xml b/brave-features/src/main/resources/features.xml
index 16c4c9c..9cb5b49 100644
--- a/brave-features/src/main/resources/features.xml
+++ b/brave-features/src/main/resources/features.xml
@@ -33,6 +33,21 @@
 		<bundle>mvn:io.zipkin.reporter/zipkin-sender-kafka08/${zipkin.reporter.version}</bundle>
 		<bundle>mvn:io.zipkin.brave.karaf/brave-exporter-kafka08/${project.version}</bundle>
 	</feature>
+
+	<feature name="brave-okhttp3`">
+		<feature>brave-core</feature>
+		<bundle>mvn:org.apache.httpcomponents/httpcore-osgi/4.4.5</bundle>
+		<bundle>mvn:org.apache.httpcomponents/httpclient-osgi/4.5.3</bundle>
+		<bundle>mvn:io.zipkin.brave/brave/${brave.version}</bundle>
+		<bundle>mvn:io.zipkin.brave/brave/${brave.version}</bundle>
+		<bundle>mvn:io.zipkin.brave/brave-instrumentation-http/${brave.version}</bundle>
+		<bundle>mvn:io.zipkin.brave/brave-instrumentation-httpclient/${brave.version}</bundle>
+		<bundle>mvn:io.zipkin.brave/brave-context-slf4j/${brave.version}</bundle>
+		<bundle>mvn:io.zipkin.reporter/zipkin-sender-okhttp3/${zipkin.reporter.version}</bundle>
+		<bundle>wrap:mvn:com.squareup.okhttp3/okhttp/3.8.0</bundle>
+		<bundle>wrap:mvn:com.squareup.okio/okio/1.13.0</bundle>
+		<bundle>mvn:io.zipkin.brave.karaf/brave-exporter-okhttp3/${project.version}</bundle>
+	</feature>
 	
 	<feature name="brave-jaxrs2">
 		<feature>brave-core</feature>
diff --git a/pom.xml b/pom.xml
index 5861972..03dec1a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,7 +26,8 @@ the License.
 		<module>brave-itests</module>
 		<module>brave-exporter</module>
 		<module>brave-exporter-kafka08</module>
-	</modules>
+		<module>brave-exporter-okhttp3</module>
+  </modules>
 
 	<properties>
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>