Skip to content

Commit

Permalink
add zipkin plugin (apache#8709)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsolr authored Dec 22, 2020
1 parent bf74c0b commit 0581113
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 0 deletions.
12 changes: 12 additions & 0 deletions shardingsphere-agent/shardingsphere-agent-plugins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-proxy-common</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-proxy-frontend-core</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<modules>
<module>shardingsphere-agent-tracer-jaeger</module>
<module>shardingsphere-agent-tracer-zipkin</module>
</modules>

<properties>
Expand All @@ -43,6 +44,10 @@
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-agent-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-proxy-common</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-agent-plugin-tracers</artifactId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>

<packaging>jar</packaging>
<artifactId>shardingsphere-agent-tracer-zipkin</artifactId>

<properties>
<zipkin-reporter.version>2.16.2</zipkin-reporter.version>
<zipkin-brave.version>5.13.2</zipkin-brave.version>
<entrypoint.class>org.apache.shardingsphere.agent.plugin.trace.zipkin.ZipkinPluginDefinition</entrypoint.class>
</properties>

<dependencies>
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave</artifactId>
<version>${zipkin-brave.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
<version>${zipkin-reporter.version}</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-okhttp3</artifactId>
<version>${zipkin-reporter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-proxy-frontend-core</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 org.apache.shardingsphere.agent.plugin.trace.zipkin;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
* Sharding tags.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingConstants {

public static final String COMPONENT_NAME = "shardingsphere";

public static final String ROOT_SPAN = "_root_span_";

public static final String DB_TYPE_VALUE = "shardingsphere-proxy";

public static class Tags {
/**
* The tag to record the bind variables of SQL.
*/
public static final String DB_TYPE = "db.type";

public static final String DB_STATEMENT = "db.statement";

public static final String DB_INSTANCE = "db.instance";

public static final String DB_BIND_VARIABLES = "db.bind_vars";

public static final String COMPONENT = "component";

public static final String PEER_HOSTNAME = "peer.hostname";

/**
* PEER_PORT records the port number of the peer.
*/
public static final String PEER_PORT = "peer.port";


/**
* The tag to record the connection count.
*/
public static final String CONNECTION_COUNT = "connection.count";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 org.apache.shardingsphere.agent.plugin.trace.zipkin;

import net.bytebuddy.matcher.ElementMatchers;
import org.apache.shardingsphere.agent.core.plugin.PluginDefinition;

/**
* Zipkin plugin definition.
*/
public class ZipkinPluginDefinition extends PluginDefinition {

public ZipkinPluginDefinition() {
super("zipkin");
}

@Override
protected void define() {
registerService(ZipkinTracerService.class);

intercept("org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask")
.aroundInstanceMethod(ElementMatchers.named("executeCommand"))
.implement("org.apache.shardingsphere.agent.plugin.trace.zipkin.advice.CommandExecutorTaskAdvice")
.build();
intercept("org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine")
.aroundInstanceMethod(ElementMatchers.named("parse"))
.implement("org.apache.shardingsphere.agent.plugin.trace.zipkin.advice.SQLParserEngineAdvice")
.build();
intercept("org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback")
.aroundInstanceMethod(
ElementMatchers.named("execute")
.and(ElementMatchers.takesArgument(
0,
ElementMatchers.named("org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit"))
)
)
.implement("org.apache.shardingsphere.agent.plugin.trace.zipkin.advice.JDBCExecutorCallbackAdvice")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 org.apache.shardingsphere.agent.plugin.trace.zipkin;

import brave.Tracing;
import org.apache.shardingsphere.agent.core.config.AgentConfiguration;
import org.apache.shardingsphere.agent.core.plugin.Service;
import org.apache.shardingsphere.agent.core.utils.SingletonHolder;
import zipkin2.reporter.brave.AsyncZipkinSpanHandler;
import zipkin2.reporter.okhttp3.OkHttpSender;

/**
* Zipkin tracer service.
*/
public class ZipkinTracerService implements Service {

private AsyncZipkinSpanHandler zipkinSpanHandler;

private OkHttpSender sender;

private Tracing tracing;

@Override
public void setup() {
AgentConfiguration configuration = SingletonHolder.INSTANCE.get(AgentConfiguration.class);
AgentConfiguration.TracingConfiguration tracingConfiguration = configuration.getTracing();
sender = OkHttpSender.create("http://" + tracingConfiguration.getAgentHost() + ":" + tracingConfiguration.getAgentPort());
zipkinSpanHandler = AsyncZipkinSpanHandler.create(sender);
}

@Override
public void start() {
tracing = Tracing.newBuilder().localServiceName("shardingsphere-agent").addSpanHandler(zipkinSpanHandler).build();
}

@Override
public void cleanup() {
tracing.close();
zipkinSpanHandler.close();
sender.close();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 org.apache.shardingsphere.agent.plugin.trace.zipkin.advice;

import brave.Span;
import brave.Tracing;
import lombok.SneakyThrows;
import org.apache.shardingsphere.agent.core.plugin.advice.MethodAroundAdvice;
import org.apache.shardingsphere.agent.core.plugin.advice.MethodInvocationResult;
import org.apache.shardingsphere.agent.core.plugin.advice.TargetObject;
import org.apache.shardingsphere.agent.plugin.trace.zipkin.ShardingConstants;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutorDataMap;
import org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask;

import java.lang.reflect.Method;

/**
* Command executor task advice.
*/
public class CommandExecutorTaskAdvice implements MethodAroundAdvice {

private static final String OPERATION_NAME = "/ShardingSphere/rootInvoke/";

@Override
public void beforeMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
Span span = Tracing.currentTracer().newTrace().name(OPERATION_NAME);
span.tag(ShardingConstants.Tags.COMPONENT, ShardingConstants.COMPONENT_NAME).kind(Span.Kind.CLIENT)
.tag(ShardingConstants.Tags.DB_STATEMENT, ShardingConstants.DB_TYPE_VALUE).start();
ExecutorDataMap.getValue().put(ShardingConstants.ROOT_SPAN, span);
}

@SneakyThrows
@Override
public void afterMethod(final TargetObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
BackendConnection connection = (BackendConnection) CommandExecutorTask.class.getDeclaredField("backendConnection").get(target);
Span span = (Span) ExecutorDataMap.getValue().remove(ShardingConstants.ROOT_SPAN);
span.tag(ShardingConstants.Tags.CONNECTION_COUNT, String.valueOf(connection.getConnectionSize()));
span.flush();
}

@Override
public void onThrowing(final TargetObject target, final Method method, final Object[] args, final Throwable throwable) {
((Span) ExecutorDataMap.getValue().get(ShardingConstants.ROOT_SPAN)).error(throwable);
}
}
Loading

0 comments on commit 0581113

Please sign in to comment.