Class SpanScopeBuilder

java.lang.Object
org.bithon.agent.sdk.tracing.SpanScopeBuilder

public class SpanScopeBuilder extends Object
Builder for creating scoped spans with fluent API. Use TraceContext.newScopedSpan(String) to create an instance. For example,

 try (ISpanScope span = TraceContext.newScopedSpan("operation")
                                    .kind(SpanKind.CLIENT)              // optional, default is SpanKind.INTERNAL
                                    .create()) {
          // Update tags if needed
          span.tag("key", "value");

          // Business logic here
   }
   
Author:
frank.chen021@outlook.com
Date:
2025/01/20
  • Field Details

    • LOGGER

      private static final Logger LOGGER
    • LOG_INTERVAL_MS

      private static final long LOG_INTERVAL_MS
      See Also:
    • lastLogTime

      private static long lastLogTime
    • operationName

      private final String operationName
    • kind

      private SpanKind kind
  • Constructor Details

    • SpanScopeBuilder

      public SpanScopeBuilder(String operationName)
  • Method Details

    • shouldLog

      private static boolean shouldLog()
    • kind

      public SpanScopeBuilder kind(SpanKind kind)
      Sets the span kind for this scoped span.
      Parameters:
      kind - the span kind to use
      Returns:
      this builder for method chaining
    • create

      public ISpanScope create()
      Creates a scoped span within the current tracing context.

      The created span will be a child of the current active span. If the kind(SpanKind) is not called, the default kind is SpanKind.INTERNAL.

      Returns:
      an ISpan ready for use in try-with-resources. Callers MUST ensure to close the span to avoid resource leaks.
      Throws:
      SdkException - if there's no active tracing context.
    • operationName

      public String operationName()
    • kind

      public SpanKind kind()