Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持上报数据到Jaeger #445

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<module>sofa-tracer-plugins/sofa-tracer-okhttp-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-datasource-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-zipkin-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-jaeger-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-dubbo-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-spring-cloud-plugin</module>
<module>sofa-tracer-plugins/sofa-tracer-resttmplate-plugin</module>
Expand All @@ -40,6 +41,8 @@
<module>tracer-test/logback-test</module>
<module>tracer-test/log4j2-test</module>
<module>tracer-test/log4j-test</module>


</modules>

<properties>
Expand Down Expand Up @@ -130,6 +133,11 @@
<artifactId>sofa-tracer-zipkin-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-jaeger-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-tracer-spring-cloud-plugin</artifactId>
Expand Down Expand Up @@ -233,13 +241,24 @@
<version>1.3.1</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<!-- 对外部框架的一些插件使用 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<!-- HttpClient Dependency -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -294,11 +313,7 @@
<version>${jmh.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>

</dependencies>
</dependencyManagement>
<build>
Expand Down
128 changes: 128 additions & 0 deletions sofa-tracer-plugins/sofa-tracer-jaeger-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>tracer-all-parent</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>3.1.1</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sofa-tracer-jaeger-plugin</artifactId>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-core</artifactId>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>tracer-extensions</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<!-- jaeger -->
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
<version>1.6.0</version>
<exclusions>
<exclusion>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.12.1</version>
</dependency>
<!-- opentracing dependency -->
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>0.33.0</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-noop</artifactId>
<version>0.33.0</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<version>0.33.0</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<version>0.33.0</version>
</dependency>

<!-- test dependency -->
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit-coverage</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alipay.sofa.tracer.plugins.jaeger;

import com.alipay.common.tracer.core.listener.SpanReportListener;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.sofa.tracer.plugins.jaeger.adapter.JaegerSpanAdapter;
import com.alipay.common.tracer.core.utils.NetUtils;
import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.reporters.RemoteReporter;
import io.jaegertracing.thrift.internal.senders.HttpSender;
import io.jaegertracing.thrift.internal.senders.ThriftSender;
import io.jaegertracing.thrift.internal.senders.UdpSender;
import okhttp3.OkHttpClient;
import org.apache.thrift.transport.TTransportException;
import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
* JaegerSofaTracerSpanRemoteReporter
* @author: zhaochen
*/
public class JaegerSofaTracerSpanRemoteReporter implements SpanReportListener, Closeable {
private JaegerSpanAdapter adapter = new JaegerSpanAdapter();
private ThriftSender sender;
private RemoteReporter reporter;
private JaegerTracer jaegerTracer;
private static final int OKHTTP_MAX_REQUESTS = 64;
private static final Long OKHTTP_READ_TIMEOUT_SECONDS = 60L;

public JaegerSofaTracerSpanRemoteReporter(String host, int port, int maxPacketSize,
String serviceName, int flushInterval,
int maxQueueSize, int closeEnqueueTimeout)
throws TTransportException {
//use UdpSender to send to the jaeger Agent
sender = new UdpSender(host, port, maxPacketSize);
buildTracer(serviceName, flushInterval, maxQueueSize, closeEnqueueTimeout);
}

public JaegerSofaTracerSpanRemoteReporter(String baseUrl, int maxPacketSizeBytes,
String serviceName, int flushInterval,
int maxQueueSize, int closeEnqueueTimeout)
throws TTransportException {
String url = baseUrl + (baseUrl.endsWith("/") ? "" : "/") + "api/traces";
OkHttpClient client = new OkHttpClient.Builder().readTimeout(OKHTTP_READ_TIMEOUT_SECONDS,
TimeUnit.SECONDS).build();
client.dispatcher().setMaxRequests(OKHTTP_MAX_REQUESTS);
client.dispatcher().setMaxRequestsPerHost(OKHTTP_MAX_REQUESTS);
//use Http sender to send to Jaeger collector directly
sender = new HttpSender.Builder(url).withMaxPacketSize(maxPacketSizeBytes)
.withClient(client).build();
buildTracer(serviceName, flushInterval, maxQueueSize, closeEnqueueTimeout);
}

private void buildTracer(String serviceName, int flushInterval, int maxQueueSize,
int closeEnqueueTimeout) {
reporter = new RemoteReporter.Builder().withSender(sender).withFlushInterval(flushInterval)
.withMaxQueueSize(maxQueueSize).withCloseEnqueueTimeout(closeEnqueueTimeout).build();

jaegerTracer = new JaegerTracer.Builder(serviceName).withReporter(reporter)
.withTraceId128Bit().withTag("ip", NetUtils.getLocalIpv4()).build();
}

@Override
public void onSpanReport(SofaTracerSpan sofaTracerSpan) {
if (sofaTracerSpan == null || !sofaTracerSpan.getSofaTracerSpanContext().isSampled()) {
return;
}
// the sender in jaegerTracer will send the input sofaTracerSpan according to the setting
adapter.convertAndReport(sofaTracerSpan, jaegerTracer);
}

@Override
public void close() throws IOException {
//close jaegerTracer and it will close reporter internal
this.jaegerTracer.close();
}

public RemoteReporter getReporter() {
return this.reporter;
}

public JaegerTracer getJaegerTracer() {
return this.jaegerTracer;
}

}
Loading