Docs » Supported integrations in Splunk Observability Cloud » Instrument back-end applications to send spans to Splunk APM » Instrument Java applications for Splunk Observability Cloud » Manually instrument Java applications for Splunk Observability Cloud

Manually instrument Java applications for Splunk Observability Cloud 🔗

Instrumenting applications automatically using the agent of the Splunk Distribution of OpenTelemetry Java covers most needs. Manually instrumenting your application is only necessary when, for example, you need to add custom attributes to spans or need to manually generate spans and metrics.

Note

Manual OTel instrumentation is fully compatible with Splunk automatic JVM instrumentation and is fully supported by Splunk.

Send custom Java application traces 🔗

To create custom spans and traces, follow these steps:

  1. Install the Splunk Distribution of OpenTelemetry Java. See Instrument your Java application for Splunk Observability Cloud.

  2. Acquire a tracer using the getTracer method:

    import io.opentelemetry.api.trace.Tracer;
    
    Tracer tracer = openTelemetry.getTracer("instrumentation-scope-name", "instrumentation-scope-version");
    
  3. Create a span. The following example shows how to create and end a span in a sample application:

    import io.opentelemetry.api.trace.Span;
    import io.opentelemetry.context.Scope;
    
    // ...
    @GetMapping("/rolldice")
    public List<Integer> index(@RequestParam("player") Optional<String> player,
          @RequestParam("rolls") Optional<Integer> rolls) {
       Span span = tracer.spanBuilder("rollTheDice")
          .setAttribute("player.name", player.orElse("unknown"))
          .startSpan();
    
       // Make the span the current span
       try (Scope scope = span.makeCurrent()) {
    
          //.. Application logic
    
       } catch(Throwable t) {
          span.recordException(t);
          throw t;
       } finally {
          span.end();
       }
    }
    

For more examples, see the manual instrumentation docs in the OpenTelemetry Java Instrumentation repository at https://opentelemetry.io/docs/java/manual_instrumentation .

Send custom Java application metrics 🔗

To create custom metrics, follow these steps:

  1. Install the Splunk Distribution of OpenTelemetry Java. See Instrument your Java application for Splunk Observability Cloud.

  2. Create a meter:

    OpenTelemetry openTelemetry = GlobalOpenTelemetry.get();
    Meter sampleMeter = openTelemetry.getMeter("foo.example.metrics");
    
  3. Build a specific metric type. The following example shows how to create a gauge metric:

    sampleMeter
       .gaugeBuilder("player.hitpoints")
       .setDescription("A player's currently remaining hit points.")
       .setUnit("HP")
       .ofLongs()
       .buildWithCallback(res -> {
           long hitpoints = currentPlayer.hitpoints();
           String playerName = currentPlayer.name();
           res.record(hitpoints, Attributes.of(stringKey("name"), playerName)));
       });
       .buildWithCallback(
          result -> result.record(Runtime.getRuntime().totalMemory(), Attributes.empty()));
    

For more examples, see the manual instrumentation docs in the OpenTelemetry Java Instrumentation repository at https://opentelemetry.io/docs/java/manual_instrumentation .

This page was last updated on Jun 25, 2024.