Class: Tracekit::EndpointResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/tracekit/endpoint_resolver.rb

Overview

Resolves endpoint URLs for different TraceKit services (traces, metrics, snapshots) Implements the same logic as .NET, Go, and Java SDKs for consistency

CRITICAL: This must match the exact behavior across all SDK implementations

Class Method Summary collapse

Class Method Details

.extract_base_url(full_url) ⇒ String

Extracts base URL (scheme + host + port) from full URL Always strips any path component, regardless of what it is

Examples:

extract_base_url("https://app.tracekit.dev/v1/traces")
# => "https://app.tracekit.dev"

extract_base_url("http://localhost:8081/custom/path")
# => "http://localhost:8081"

Parameters:

  • full_url (String)

    The full URL to extract base from

Returns:

  • (String)

    The base URL (scheme + host + port)



63
64
65
66
# File 'lib/tracekit/endpoint_resolver.rb', line 63

def extract_base_url(full_url)
  match = full_url.match(%r{^(https?://[^/]+)})
  match ? match[1] : full_url
end

.resolve(endpoint, path, use_ssl) ⇒ String

Resolves a full endpoint URL from a base endpoint and path

Examples:

resolve("app.tracekit.dev", "/v1/traces", true)
# => "https://app.tracekit.dev/v1/traces"

resolve("http://localhost:8081", "/v1/traces", true)
# => "http://localhost:8081/v1/traces"

resolve("https://app.tracekit.dev/v1/traces", "/v1/metrics", true)
# => "https://app.tracekit.dev/v1/metrics"

Parameters:

  • endpoint (String)

    The base endpoint (can be host, host with scheme, or full URL)

  • path (String)

    The path to append (e.g., “/v1/traces”, “/v1/metrics”, or “”)

  • use_ssl (Boolean)

    Whether to use HTTPS (ignored if endpoint already has a scheme)

Returns:

  • (String)

    The resolved endpoint URL



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tracekit/endpoint_resolver.rb', line 26

def resolve(endpoint, path, use_ssl)
  # Case 1: Endpoint has a scheme (http:// or https://)
  if endpoint.start_with?("http://", "https://")
    # Remove trailing slash
    endpoint = endpoint.chomp("/")

    # Check if endpoint has a path component (anything after the host)
    without_scheme = endpoint.sub(%r{^https?://}i, "")

    if without_scheme.include?("/")
      # Endpoint has a path component - extract base and append correct path
      base_url = extract_base_url(endpoint)
      return path.empty? ? base_url : "#{base_url}#{path}"
    end

    # Just host with scheme, add the path
    return "#{endpoint}#{path}"
  end

  # Case 2: No scheme provided - build URL with scheme
  scheme = use_ssl ? "https://" : "http://"
  endpoint = endpoint.chomp("/")
  "#{scheme}#{endpoint}#{path}"
end