Skip to content

Commit

Permalink
report spans to SkyWalking (sofastack#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhao11 committed Sep 7, 2021
1 parent ca86846 commit 974d284
Show file tree
Hide file tree
Showing 39 changed files with 1,839 additions and 11 deletions.
26 changes: 21 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-skywalking-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-skywalking-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,25 @@
<version>1.3.1</version>
</dependency>


<!-- 对外部框架的一些插件使用 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</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>

<!-- HttpClient Dependency -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -294,11 +314,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
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ private void appendRpcServerSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTrac
tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
String app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
tagsStr.put(CommonSpanTags.REMOTE_PORT, String.valueOf(rpcContext.getRemotePort()));
tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
String protocol = rpcContext.getUrl().getProtocol();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ private void appendRpcServerSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTrac
tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
String app = rpcContext.getUrl().getParameter(CommonConstants.APPLICATION_KEY);
tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
tagsStr.put(CommonSpanTags.REMOTE_PORT, String.valueOf(rpcContext.getRemotePort()));
tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
String protocol = rpcContext.getUrl().getProtocol();
Expand Down
97 changes: 97 additions & 0 deletions sofa-tracer-plugins/sofa-tracer-skywalking-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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-skywalking-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>
<!-- 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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.skywalking;

import com.alibaba.fastjson.JSON;
import com.alipay.common.tracer.core.listener.SpanReportListener;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.sofa.tracer.plugins.skywalking.adapter.SkywalkingSegmentAdapter;
import com.alipay.sofa.tracer.plugins.skywalking.reporter.AsyncReporter;
import com.alipay.sofa.tracer.plugins.skywalking.sender.SkywalkingRestTemplateSender;
import com.alipay.sofa.tracer.plugins.skywalking.model.Segment;

import org.springframework.web.client.RestTemplate;

import java.io.Closeable;
import java.io.Flushable;
import java.io.IOException;

/**
* SkywalkingSpanRemoteReporter
* @author zhaochen
*/
public class SkywalkingSpanRemoteReporter implements SpanReportListener, Closeable, Flushable {
private AsyncReporter reporter;
private SkywalkingRestTemplateSender sender;
private SkywalkingSegmentAdapter adapter;

public SkywalkingSpanRemoteReporter(String baseUrl, int maxBufferSize) {
adapter = new SkywalkingSegmentAdapter();
sender = new SkywalkingRestTemplateSender(new RestTemplate(), baseUrl);
reporter = new AsyncReporter(maxBufferSize, sender);
}

@Override
public void onSpanReport(SofaTracerSpan sofaTracerSpan) {
if (sofaTracerSpan == null || !sofaTracerSpan.getSofaTracerSpanContext().isSampled()) {
return;
}
Segment segment = adapter.convertToSkywalkingSegment(sofaTracerSpan);
int segmentSize = JSON.toJSONString(segment).length();
reporter.report(segment, segmentSize);
}

@Override
public void close() throws IOException {
reporter.close();
}

@Override
public void flush() throws IOException {
reporter.flush();
}
}
Loading

0 comments on commit 974d284

Please sign in to comment.