View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.agent.plugin.tracing.opentelemetry.advice;
19  
20  import io.opentelemetry.api.GlobalOpenTelemetry;
21  import io.opentelemetry.api.trace.Span;
22  import io.opentelemetry.api.trace.SpanBuilder;
23  import io.opentelemetry.api.trace.StatusCode;
24  import io.opentelemetry.api.trace.Tracer;
25  import io.opentelemetry.context.Context;
26  import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
27  import org.apache.shardingsphere.agent.plugin.tracing.core.advice.TracingJDBCExecutorCallbackAdvice;
28  import org.apache.shardingsphere.agent.plugin.tracing.core.constant.AttributeConstants;
29  import org.apache.shardingsphere.agent.plugin.tracing.opentelemetry.constant.OpenTelemetryConstants;
30  import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
31  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
32  import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
33  
34  import java.lang.reflect.Method;
35  
36  /**
37   * OpenTelemetry JDBC executor callback advice executor.
38   */
39  public final class OpenTelemetryJDBCExecutorCallbackAdvice extends TracingJDBCExecutorCallbackAdvice<Span> {
40      
41      @Override
42      protected void recordExecuteInfo(final Span parentSpan, final TargetAdviceObject target, final JDBCExecutionUnit executionUnit, final boolean isTrunkThread,
43                                       final ConnectionProperties connectionProps, final DatabaseType databaseType) {
44          Tracer tracer = GlobalOpenTelemetry.getTracer(OpenTelemetryConstants.TRACER_NAME);
45          SpanBuilder spanBuilder = tracer.spanBuilder(OPERATION_NAME);
46          spanBuilder.setParent(Context.current().with(parentSpan));
47          spanBuilder.setAttribute(AttributeConstants.COMPONENT, AttributeConstants.COMPONENT_NAME);
48          spanBuilder.setAttribute(AttributeConstants.DB_TYPE, databaseType.getType());
49          spanBuilder.setAttribute(AttributeConstants.DB_INSTANCE, executionUnit.getExecutionUnit().getDataSourceName())
50                  .setAttribute(AttributeConstants.PEER_HOSTNAME, connectionProps.getHostname())
51                  .setAttribute(AttributeConstants.PEER_PORT, String.valueOf(connectionProps.getPort()))
52                  .setAttribute(AttributeConstants.DB_STATEMENT, executionUnit.getExecutionUnit().getSqlUnit().getSql())
53                  .setAttribute(AttributeConstants.DB_BIND_VARIABLES, executionUnit.getExecutionUnit().getSqlUnit().getParameters().toString())
54                  .setAttribute(AttributeConstants.SPAN_KIND, AttributeConstants.SPAN_KIND_CLIENT);
55          target.setAttachment(spanBuilder.startSpan());
56      }
57      
58      @Override
59      public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result, final String pluginType) {
60          Span span = (Span) target.getAttachment();
61          span.setStatus(StatusCode.OK);
62          span.end();
63      }
64      
65      @Override
66      public void onThrowing(final TargetAdviceObject target, final Method method, final Object[] args, final Throwable throwable, final String pluginType) {
67          Span span = (Span) target.getAttachment();
68          span.setStatus(StatusCode.ERROR).recordException(throwable);
69          span.end();
70      }
71  }