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