Class TracedResource


  • @Path("/resource")
    public class TracedResource
    extends Object
    OpenTracing example resource.

    Jersey (with registered OpenTracingFeature will automatically create and start span for each request ("root" span or "request" span) and a child span to be used in the resource method ("resource" span). The root span is used for Jersey-level event logging (resource matching started, request filters applied, etc). The resource span serves for application-level event logging purposes (used-defined). Both are automatically created and also automatically finished.

    Resource span is created right before the resource method invocation and finished right after resource method finishes. It can be resolved by calling OpenTracingUtils.getRequestSpan(ContainerRequestContext).

    Application code can also create ad-hoc spans as child spans of the resource span. This can be achieved by calling one of the convenience methods OpenTracingUtils.getRequestChildSpan(ContainerRequestContext).

    ContainerRequestContext can be obtained via injection.

    All the ad-hoc created spans MUST be finished explicitly.

    Author:
    Adam Lindenthal
    • Constructor Summary

      Constructors 
      Constructor Description
      TracedResource()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      jakarta.ws.rs.core.Response appLevelLogging​(jakarta.ws.rs.container.ContainerRequestContext context)
      Resource method with explicit logging into resource span.
      jakarta.ws.rs.core.Response childSpan​(jakarta.ws.rs.container.ContainerRequestContext context)
      Resource method with explicit child span creation.
      jakarta.ws.rs.core.Response defaultTrace()
      Resource method with no explicit tracing.
      String failTrace​(jakarta.ws.rs.container.ContainerRequestContext context)  
      jakarta.ws.rs.core.Response tracePost​(String entity, jakarta.ws.rs.container.ContainerRequestContext context)
      Similar as appLevelLogging(ContainerRequestContext), just with POST method.
      void traceWithAsync​(jakarta.ws.rs.container.AsyncResponse asyncResponse, jakarta.ws.rs.container.ContainerRequestContext context)  
      jakarta.ws.rs.core.Response traceWithManagedClient​(jakarta.ws.rs.container.ContainerRequestContext context, jakarta.ws.rs.client.WebTarget wt)
      Resource method with explicit span creation and propagation into injected managed client.
    • Constructor Detail

      • TracedResource

        public TracedResource()
    • Method Detail

      • defaultTrace

        @GET
        @Path("defaultTrace")
        public jakarta.ws.rs.core.Response defaultTrace()
        Resource method with no explicit tracing.

        One span (jersey-server) will be created and finished automatically.

        Returns:
        dummy response
      • appLevelLogging

        @GET
        @Path("appLevelLogging")
        public jakarta.ws.rs.core.Response appLevelLogging​(@Context
                                                           jakarta.ws.rs.container.ContainerRequestContext context)
                                                    throws InterruptedException
        Resource method with explicit logging into resource span.
        Parameters:
        context - injected request context with resource-level span reference
        Returns:
        dummy response
        Throws:
        InterruptedException - if interrupted
      • tracePost

        @POST
        @Path("appLevelPost")
        public jakarta.ws.rs.core.Response tracePost​(String entity,
                                                     @Context
                                                     jakarta.ws.rs.container.ContainerRequestContext context)
        Similar as appLevelLogging(ContainerRequestContext), just with POST method.
        Parameters:
        entity - posted entity
        context - injected context
        Returns:
        dummy response
      • childSpan

        @GET
        @Path("childSpan")
        public jakarta.ws.rs.core.Response childSpan​(@Context
                                                     jakarta.ws.rs.container.ContainerRequestContext context)
                                              throws InterruptedException
        Resource method with explicit child span creation.
        Parameters:
        context - injected request context with resource-level (parent) span reference
        Returns:
        dummy response
        Throws:
        InterruptedException - if interrupted
      • traceWithManagedClient

        @GET
        @Path("managedClient")
        public jakarta.ws.rs.core.Response traceWithManagedClient​(@Context
                                                                  jakarta.ws.rs.container.ContainerRequestContext context,
                                                                  @Uri("resource/appLevelPost")
                                                                  jakarta.ws.rs.client.WebTarget wt)
        Resource method with explicit span creation and propagation into injected managed client.

        Shows how to propagate the server-side span into managed client (or any common Jersey client). This way, the client span will be child of the resource span.

        Parameters:
        context - injected context
        wt - injected web target
        Returns:
        dummy response
      • traceWithAsync

        @GET
        @Path("async")
        @ManagedAsync
        public void traceWithAsync​(@Suspended
                                   jakarta.ws.rs.container.AsyncResponse asyncResponse,
                                   @Context
                                   jakarta.ws.rs.container.ContainerRequestContext context)
      • failTrace

        @GET
        @Path("error")
        public String failTrace​(@Context
                                jakarta.ws.rs.container.ContainerRequestContext context)