Class UrlParsingUtils

java.lang.Object
io.opentelemetry.instrumentation.jdbc.internal.parser.UrlParsingUtils

public final class UrlParsingUtils extends Object
Utility methods for parsing JDBC URLs.

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

  • Field Details

    • IPV6_PATTERN

      public static final Pattern IPV6_PATTERN
  • Method Details

    • splitQuery

      public static Map<String,String> splitQuery(String query, String separator)
      Split a query string into key-value pairs.
      Parameters:
      query - the query string
      separator - the separator between pairs (e.g., "&" or ";")
      Returns:
      a map of key-value pairs
    • parsePort

      @Nullable public static Integer parsePort(@Nullable String value)
      Parse an integer value, returning null if parsing fails.
      Parameters:
      value - the string value to parse
      Returns:
      the parsed integer, or null if parsing fails
    • buildShortUrl

      public static String buildShortUrl(String type, @Nullable String subtype, @Nullable String host, @Nullable Integer port)
      Build the short URL for db.connection_string attribute.
      Parameters:
      type - the JDBC type (e.g., "postgresql", "mysql")
      subtype - optional subtype (e.g., "thin" for Oracle, "aurora" for MySQL)
      host - the host name
      port - the port number
      Returns:
      the short URL in format "type:[subtype:]//host:port" or "type:" if no host
    • extractParams

      public static Map<String,String> extractParams(String jdbcUrl, String startDelimiter, String splitSeparator)
      Extract parameters from a JDBC URL.
      Parameters:
      jdbcUrl - the JDBC URL
      startDelimiter - the delimiter marking the start of parameters (";" or "?")
      splitSeparator - the separator between parameters (";" or "&")
      Returns:
      a map of parameter key-value pairs
    • extractSemicolonParams

      public static Map<String,String> extractSemicolonParams(String jdbcUrl)
      Extract semicolon-delimited URL parameters from a JDBC URL.
      Parameters:
      jdbcUrl - the JDBC URL containing parameters after semicolon
      Returns:
      a map of parameter key-value pairs
    • extractQueryParams

      public static Map<String,String> extractQueryParams(String jdbcUrl, String separator)
      Extract query-style URL parameters from a JDBC URL.
      Parameters:
      jdbcUrl - the JDBC URL containing parameters after "?"
      separator - the parameter separator (typically "&")
      Returns:
      a map of parameter key-value pairs
    • extractSubtype

      @Nullable public static String extractSubtype(String jdbcUrl)
      Extract subtype from a JDBC URL of the form "type:subtype://...".

      For example, "mysql:aurora://host:port/db" returns "aurora", "oceanbase:oracle://host/db" returns "oracle".

      Parameters:
      jdbcUrl - the JDBC URL
      Returns:
      the subtype, or null if no subtype is present
    • indexOfAny

      public static int indexOfAny(String str, char... chars)
      Find the index of the first occurrence of any of the specified characters.
      Parameters:
      str - the string to search
      chars - the characters to search for
      Returns:
      the index of the first occurrence, or -1 if none found
    • extractAuthority

      @Nullable public static String extractAuthority(String url)
      Extract the authority from a JDBC URL without reporting ambiguous credential text.
    • hasMultipleTargets

      public static boolean hasMultipleTargets(String jdbcUrl)
      Returns whether a JDBC URL contains more than one configured target.
    • sanitizeHostList

      @Nullable public static String sanitizeHostList(String authority)
      Sanitize a comma-separated host list, returning null when it is not valid.
    • parseServerTargetGroup

      @Nullable public static io.opentelemetry.instrumentation.api.incubator.semconv.db.internal.DbServerTarget parseServerTargetGroup(String authority, @Nullable Integer defaultPort)
      Parse and normalize a comma-separated configured server group.
    • extractHostPort

      public static UrlParsingUtils.HostPort extractHostPort(String serverName)
      Extract host and port from a server string, handling IPv6 addresses. Supports formats: host, host:port, [ipv6], [ipv6]:port, ipv6.

      The returned host never carries the enclosing brackets of a literal IPv6 address, matching the server.address semantic convention, which holds the address alone.

      Parameters:
      serverName - the server string to parse
      Returns:
      the extracted host and port
    • stripIpv6Brackets

      public static String stripIpv6Brackets(String host)
      Remove the enclosing brackets from a literal IPv6 address, e.g. [::1] becomes ::1. Any other value is returned unchanged.
      Parameters:
      host - the host to unbracket
      Returns:
      the host without enclosing brackets