public static Object onMethodEnter()

in apm-agent-plugins/apm-opentelemetry/apm-opentelemetry-plugin/src/main/java/co/elastic/apm/agent/opentelemetry/WithSpanInstrumentation.java [109:159]


        public static Object onMethodEnter(
            @SimpleMethodSignatureOffsetMappingFactory.SimpleMethodSignature String signature,
            @AnnotationValueOffsetMappingFactory.AnnotationValueExtractor(annotationClassName = "io.opentelemetry.instrumentation.annotations.WithSpan", method = "value") String spanName,
            @AnnotationValueOffsetMappingFactory.AnnotationValueExtractor(annotationClassName = "io.opentelemetry.instrumentation.annotations.WithSpan", method = "kind") SpanKind otelKind,
            @Advice.Origin Method method,
            @Advice.AllArguments Object[] methodArguments) {

            TraceState<?> activeContext = tracer.currentContext();
            final AbstractSpan<?> parentSpan = activeContext.getSpan();
            if (parentSpan == null) {
                logger.debug("Not creating span for {} because there is no currently active span.", signature);
                return null;
            }
            if (activeContext.shouldSkipChildSpanCreation()) {
                // span limit reached means span will not be reported, thus we can optimize and skip creating one
                logger.debug("Not creating span for {} because span limit is reached.", signature);
                return null;
            }

            Span<?> span = activeContext.createSpan();
            if (span == null) {
                return null;
            }

            // process parameters that annotated with `io.opentelemetry.instrumentation.annotations.SpanAttribute` annotation
            int argsLength = methodArguments.length;
            if (argsLength > 0) {
                Annotation[][] parameterAnnotations = method.getParameterAnnotations();
                for (int i = 0; i < argsLength; i++) {
                    Annotation[] parameterAnnotation = parameterAnnotations[i];
                    int parameterAnnotationLength = parameterAnnotation.length;
                    for (int j = 0; j < parameterAnnotationLength; j++) {
                        if (parameterAnnotation[j] instanceof SpanAttribute) {
                            SpanAttribute spanAttribute = (SpanAttribute) parameterAnnotation[j];
                            String attributeName = spanAttribute.value();
                            if (!attributeName.isEmpty()) {
                                span.withOtelAttribute(attributeName, methodArguments[i]);
                            }
                            break;
                        }
                    }
                }
            }

            span.withName(spanName.isEmpty() ? signature : spanName)
                .activate();

            ((SpanImpl) span).withOtelKind(OTelHelper.map(otelKind));

            return span;
        }