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.TargetAdviceMethod;
27  import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
28  import org.apache.shardingsphere.agent.plugin.tracing.core.advice.TracingJDBCExecutorCallbackAdvice;
29  import org.apache.shardingsphere.agent.plugin.tracing.core.constant.AttributeConstants;
30  import org.apache.shardingsphere.agent.plugin.tracing.opentelemetry.constant.OpenTelemetryConstants;
31  import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
32  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
33  import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
34  
35  /**
36   * OpenTelemetry JDBC executor callback advice executor.
37   */
38  public final class OpenTelemetryJDBCExecutorCallbackAdvice extends TracingJDBCExecutorCallbackAdvice<Span> {
39      
40      @Override
41      protected void recordExecuteInfo(final Span parentSpan, final TargetAdviceObject target, final JDBCExecutionUnit executionUnit, final boolean isTrunkThread,
42                                       final ConnectionProperties connectionProps, final DatabaseType databaseType) {
43          Tracer tracer = GlobalOpenTelemetry.getTracer(OpenTelemetryConstants.TRACER_NAME);
44          SpanBuilder spanBuilder = tracer.spanBuilder(OPERATION_NAME);
45          spanBuilder.setParent(Context.current().with(parentSpan));
46          spanBuilder.setAttribute(AttributeConstants.COMPONENT, AttributeConstants.COMPONENT_NAME);
47          spanBuilder.setAttribute(AttributeConstants.DB_TYPE, databaseType.getType());
48          spanBuilder.setAttribute(AttributeConstants.DB_INSTANCE, executionUnit.getExecutionUnit().getDataSourceName())
49                  .setAttribute(AttributeConstants.PEER_HOSTNAME, connectionProps.getHostname())
50                  .setAttribute(AttributeConstants.PEER_PORT, String.valueOf(connectionProps.getPort()))
51                  .setAttribute(AttributeConstants.DB_STATEMENT, executionUnit.getExecutionUnit().getSqlUnit().getSql())
52                  .setAttribute(AttributeConstants.DB_BIND_VARIABLES, executionUnit.getExecutionUnit().getSqlUnit().getParameters().toString())
53                  .setAttribute(AttributeConstants.SPAN_KIND, AttributeConstants.SPAN_KIND_CLIENT);
54          target.setAttachment(spanBuilder.startSpan());
55      }
56      
57      @Override
58      public void afterMethod(final TargetAdviceObject target, final TargetAdviceMethod method, final Object[] args, final Object result, final String pluginType) {
59          Span span = (Span) target.getAttachment();
60          span.setStatus(StatusCode.OK);
61          span.end();
62      }
63      
64      @Override
65      public void onThrowing(final TargetAdviceObject target, final TargetAdviceMethod method, final Object[] args, final Throwable throwable, final String pluginType) {
66          Span span = (Span) target.getAttachment();
67          span.setStatus(StatusCode.ERROR).recordException(throwable);
68          span.end();
69      }
70  }