Class DbServerTargetBuilder

java.lang.Object
io.opentelemetry.instrumentation.api.incubator.semconv.db.internal.DbServerTargetBuilder

public class DbServerTargetBuilder extends Object
Collects the endpoints a database client was configured with and renders them as a single DbServerTarget. A caller that knows which single endpoint served an operation collects that one endpoint instead, and reports the result as network.peer.address and network.peer.port.

Every endpoint is validated as a host name, an IPv4 literal, or an IPv6 literal without any name resolution. When one endpoint cannot be validated the whole target is dropped, because a partial list describes a deployment the client was never pointed at. Endpoints are validated before the list is truncated, so an unsafe endpoint beyond the cap still drops the target.

Duplicate endpoints are kept. A single endpoint is rendered as a bare host with its port reported separately, and it is reported only when it differs from the default port. Several endpoints are rendered as a comma separated list. Their ports are omitted when every endpoint listens on its default port. Otherwise known ports are included, while endpoints without either a configured or known default port remain bare.

Instances are not thread safe.

This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.

  • Field Details

  • Method Details

    • isValidHost

      public static boolean isValidHost(@Nullable String host)
      Returns whether host can be represented safely as a database server host.
    • setSorted

      @CanIgnoreReturnValue public DbServerTargetBuilder setSorted(boolean sorted)
      Sort the rendered endpoints in natural string order. Default is to preserve the configured order. Turn sorting on only when endpoint order carries no semantic meaning.
    • setPortAlwaysInline

      @CanIgnoreReturnValue public DbServerTargetBuilder setPortAlwaysInline(boolean portAlwaysInline)
      Always render ports inside the address instead of reporting a single endpoint's port separately. Default is to report a single endpoint's port separately. Turn this on for a target that names a discovery or coordination service rather than the server that runs the operation.
    • setSuffix

      @CanIgnoreReturnValue public DbServerTargetBuilder setSuffix(@Nullable String suffix)
      Append suffix to the address as a path segment, identifying the logical subset of the endpoints that the client selects. A suffix that is not a safe path segment is dropped and the remaining target is kept. Ports are always rendered inline when a suffix is set, so that the suffix cannot be read as part of an endpoint.
    • addEndpoint

      @CanIgnoreReturnValue public DbServerTargetBuilder addEndpoint(@Nullable String host, int port)
      Add an endpoint. A negative port means that the endpoint has no configured port. The target's default port is used when known.
    • addEndpoint

      @CanIgnoreReturnValue public DbServerTargetBuilder addEndpoint(@Nullable String host, int port, int defaultPort)
      Add an endpoint whose default port differs from the target's default port, such as an endpoint whose default port follows from its scheme.
    • addEndpoint

      @CanIgnoreReturnValue public DbServerTargetBuilder addEndpoint(@Nullable InetSocketAddress address)
      Add an endpoint from a socket address. The host is read without resolving it, so an address created from a name keeps that name and an address created from a numeric address keeps its literal form.
    • build

      @Nullable public DbServerTarget build()
      Returns the target, or null when it cannot be rendered safely.