Class OpenTelemetryUtils

java.lang.Object
org.forgerock.opentelemetry.OpenTelemetryUtils

public final class OpenTelemetryUtils extends Object
Utility class to configure OpenTelemetry tracing. Tracing is not enabled by default.

The following is an example configuration that enables tracing (using default values for everything else):

 
 {
     "tracing": {
          "enabled": true
     }
 }
 
 

The following is an example configuration that enables tracing with some defaults overridden:

 
 {
     "tracing": {
         "enabled": true,
         "resourceAttributes": {
              "service.instance.id": "my-instance-id-1"
          },
          "exporter": {
              "config": {
                  "headers": {
                      "X-CUSTOM-HEADER": "custom-value"
                  }
             },
             "batch": {
                 "maxQueueSize": 512
             }
         },
         "sampler": {
             "type": "parentBasedTraceIdRatio",
             "ratio": 25
         },
         "spanLimits": {
             "maxNumberOfAttributesPerEvent": 128
         }
     }
 }
 
 
  • Method Details

    • openTelemetryConfig

      public static Function<JsonValue,io.opentelemetry.api.OpenTelemetry,JsonException> openTelemetryConfig(String serviceName, String serviceVersion)
      Returns a transformation function that takes a well-defined JSON structure that represents an OpenTelemetry configuration and generates an OpenTelemetry instance from it.
      Parameters:
      serviceName - The service name to use as a resource attribute for the ServiceAttributes.SERVICE_NAME property, for example IG. The value will be converted to lowercase before use.
      serviceVersion - The service version to use as a resource attribute for the ServiceAttributes.SERVICE_VERSION property.
      Returns:
      A transformation function that takes a well-defined JSON structure that represents an OpenTelemetry configuration and generates an OpenTelemetry instance from it.
    • openTelemetryConfig

      public static Function<JsonValue,io.opentelemetry.api.OpenTelemetry,JsonException> openTelemetryConfig(OpenTelemetryUtils.ServiceConfig serviceConfig)
      Returns a transformation function that takes a well-defined JSON structure that represents an OpenTelemetry configuration and generates an OpenTelemetry instance from it.
      Parameters:
      serviceConfig - Service specific configuration items to be used when setting up OpenTelemetry tracing.
      Returns:
      A transformation function that takes a well-defined JSON structure that represents an OpenTelemetry configuration and generates an OpenTelemetry instance from it.
    • spanName

      public static String spanName(Request request)
      Computes a span name based on the give Request.

      The span name returned aims to follow the guidelines recommended by the OpenTelemetry naming conventions (see here and there).

      For instance, an HTTP GET request onto the url http://foo.com/bar/baz gives the span name GET /bar.

      Parameters:
      request - the request used to build the span name.
      Returns:
      a span name.
    • setSpanAttributes

      public static void setSpanAttributes(io.opentelemetry.api.trace.Span span, Request request, Context context)
      Configures the span attributes with data from the given request and context.
      Parameters:
      span - the span to configure
      request - the request used to set some attributes of the given span
      context - the context used to set some attributes of the given span