All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.instrumentation.testing.GlobalTraceUtil Maven / Gradle / Ivy

There is a newer version: 2.10.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.instrumentation.testing;

import groovy.lang.Closure;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.testing.util.ThrowingRunnable;
import io.opentelemetry.instrumentation.testing.util.ThrowingSupplier;

/**
 * Can be useful when you need to start a span and neither {@link
 * io.opentelemetry.instrumentation.test.InstrumentationSpecification} nor {@link
 * io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension} are available.
 */
public final class GlobalTraceUtil {

  private static final TestInstrumenters testInstrumenters =
      new TestInstrumenters(GlobalOpenTelemetry.get());

  /**
   * Runs the provided {@code callback} inside the scope of an INTERNAL span with name {@code
   * spanName}.
   */
  public static  T runWithSpan(String spanName, Closure callback) {
    return runWithSpan(spanName, (ThrowingSupplier) callback::call);
  }

  /**
   * Runs the provided {@code callback} inside the scope of an INTERNAL span with name {@code
   * spanName}.
   */
  public static  void runWithSpan(
      String spanName, ThrowingRunnable callback) throws E {
    runWithSpan(
        spanName,
        () -> {
          callback.run();
          return null;
        });
  }

  /**
   * Runs the provided {@code callback} inside the scope of an INTERNAL span with name {@code
   * spanName}.
   */
  public static  T runWithSpan(
      String spanName, ThrowingSupplier callback) throws E {
    return testInstrumenters.runWithSpan(spanName, callback);
  }

  private GlobalTraceUtil() {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy