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.TracingSQLParserEngineAdvice;
28  import org.apache.shardingsphere.agent.plugin.tracing.core.constant.AttributeConstants;
29  import org.apache.shardingsphere.agent.plugin.tracing.opentelemetry.constant.OpenTelemetryConstants;
30  
31  import java.lang.reflect.Method;
32  
33  /**
34   * OpenTelemetry SQL parser engine advice executor.
35   */
36  public final class OpenTelemetrySQLParserEngineAdvice extends TracingSQLParserEngineAdvice<Span> {
37      
38      @Override
39      protected Object recordSQLParseInfo(final Span parentSpan, final TargetAdviceObject target, final String sql) {
40          Tracer tracer = GlobalOpenTelemetry.getTracer(OpenTelemetryConstants.TRACER_NAME);
41          SpanBuilder spanBuilder = tracer.spanBuilder(OPERATION_NAME)
42                  .setAttribute(AttributeConstants.COMPONENT, AttributeConstants.COMPONENT_NAME)
43                  .setAttribute(AttributeConstants.DB_STATEMENT, sql)
44                  .setAttribute(AttributeConstants.SPAN_KIND, AttributeConstants.SPAN_KIND_INTERNAL);
45          spanBuilder.setParent(Context.current().with(parentSpan));
46          Span result = spanBuilder.startSpan();
47          target.setAttachment(result);
48          return result;
49      }
50      
51      @Override
52      public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result, final String pluginType) {
53          Span span = (Span) target.getAttachment();
54          span.setStatus(StatusCode.OK);
55          span.end();
56      }
57      
58      @Override
59      public void onThrowing(final TargetAdviceObject target, final Method method, final Object[] args, final Throwable throwable, final String pluginType) {
60          Span span = (Span) target.getAttachment();
61          span.setStatus(StatusCode.ERROR).recordException(throwable);
62          span.end();
63      }
64  }