> ## Documentation Index
> Fetch the complete documentation index at: https://docs.appsignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java Custom Instrumentation

AppSignal is compatible with OpenTelemetry instrumentation packages.
Some of these instrumentation packages may require additional setup.

These instrumentations do not not always include all the information you need to debug issues.
For more fine-grained reporting, you can add custom instrumentation.

We support [OpenTelemetry tracing][manual] as laid out in the OpenTelemetry documentation.
It's possible to add more data to span using OpenTelemetry span attributes.

There are also AppSignal span attributes that can change how traces are grouped and some attributes that allow for filtering out sensitive data.

## Creating a new span

Using the tracer from the global tracer provider, create a new span.

<CodeGroup>
  ```java Java theme={null}
  import io.opentelemetry.api.GlobalOpenTelemetry;
  import io.opentelemetry.api.trace.Span;
  import io.opentelemetry.api.trace.Tracer;
  import io.opentelemetry.context.Scope;

  public void someFunction() {
      // Get OpenTelemetry tracer
      Tracer tracer = GlobalOpenTelemetry.getTracer("my-app");

      // Create a new span
      Span span = tracer.spanBuilder("My span").startSpan();

      // Set attributes
      span.setAttribute("operation.name", "handle_request");

      // Your application logic here

      // Close the span
      span.end();
  }
  ```
</CodeGroup>

*Note: In these examples we're using the global tracer provider set by OpenTelemetry. You may want to inject the tracer into your classes for better testability.*

## Get the current span

You can also fetch the current active span:

<CodeGroup>
  ```java Java theme={null}
  import io.opentelemetry.api.trace.Span;

  public void someFunction() {
      Span span = Span.current();

      // Set attributes on the current span
      span.setAttribute("user.id", userId);
      span.setAttribute("operation.name", "database_query");
  }
  ```
</CodeGroup>

*Note: This will only return a span if one is already active.*

More ways to create and get spans are documented on the [OpenTelemetry Java Instrumentation page][manual].

## AppSignal attributes

Once you have created, or fetched, a span, you can set certain attributes that are specific to AppSignal to customize how the data arrives in AppSignal.

<CodeGroup>
  ```java Java theme={null}
  Span span = // Create a new span or fetch the current span
  span.setAttribute("attribute_name", "attribute_value");
  ```
</CodeGroup>

See our guides for more information on what custom AppSignal attributes are supported:

* [Tagging guide](/guides/tagging)
* [Data customization guide](/guides/custom-data)

[manual]: https://opentelemetry.io/docs/languages/java/api/
