Class LokiConfig

java.lang.Object
codes.grow.packages.loki.LokiConfig

public final class LokiConfig extends Object
Immutable configuration of a LokiClient (SPEC section 2.2).

Build one with builder() or read it from the environment with fromEnv(). Precedence: explicit builder value > GROW_GRAFANA_LOKI_<NAME> > vendor variables > default. toString() never prints credentials.

Loki itself has no authentication: LokiConfig.Builder.tenantId(String) is sent as X-Scope-OrgID (required by multi-tenant Loki), Basic (username/password) and Bearer credentials are for gateways and hosted Loki (Grafana Cloud: user = instance id, password = access token).

Environment variables
FieldVariables (in order)
urlGROW_GRAFANA_LOKI_URL, LOKI_URL
tenantIdGROW_GRAFANA_LOKI_TENANT_ID, LOKI_TENANT_ID
usernameGROW_GRAFANA_LOKI_USERNAME, LOKI_USERNAME, LOKI_BASIC_AUTH_USER
passwordGROW_GRAFANA_LOKI_PASSWORD, LOKI_PASSWORD, LOKI_BASIC_AUTH_PASSWORD
bearerTokenGROW_GRAFANA_LOKI_BEARER_TOKEN, LOKI_BEARER_TOKEN

plus the common CONNECT_TIMEOUT, REQUEST_TIMEOUT, TOTAL_TIMEOUT, MAX_RETRIES, CA_CERT, CLIENT_CERT, CLIENT_KEY, INSECURE_SKIP_VERIFY and PROXY after the GROW_GRAFANA_LOKI_ prefix.

  • Field Details

    • DEFAULT_URL

      public static final String DEFAULT_URL
      Default endpoint (the stack catalog port 3100).
      See Also:
    • MAX_TENANT_ID_LENGTH

      public static final int MAX_TENANT_ID_LENGTH
      Longest tenant id Loki accepts, in bytes.
      See Also:
  • Method Details

    • builder

      public static LokiConfig.Builder builder()
      Starts an empty builder (every field at its default).
      Returns:
      a new builder
    • toBuilder

      public LokiConfig.Builder toBuilder()
      Starts a builder holding every field of this configuration.
      Returns:
      a pre-filled builder
    • fromEnv

      public static LokiConfig fromEnv()
      Reads the configuration from the process environment.
      Returns:
      the validated configuration
      Throws:
      ConfigException - when a variable holds an invalid value (the message names the variable, never the value)
    • fromEnv

      public static LokiConfig fromEnv(Map<String,String> env)
      Reads the configuration from an explicit variable map (tests).
      Parameters:
      env - variable map
      Returns:
      the validated configuration
    • tenantIdProblem

      public static Optional<String> tenantIdProblem(String id, boolean multiTenantQueries)
      Returns the problem with a tenant id (X-Scope-OrgID), or empty when it is valid.

      Loki rules: non-empty, at most 150 bytes, not . or .., only [A-Za-z0-9!-_.*'()]. A | (multi-tenant query) is accepted only when multiTenantQueries is set, and every part must then be valid.

      Parameters:
      id - tenant id
      multiTenantQueries - whether a|b ids are allowed
      Returns:
      the problem, or empty
    • validate

      public void validate()
      Checks URL scheme, positive timeouts, TLS pairs, the tenant id and the credential rules.
      Throws:
      ConfigException - when a rule is violated
    • toTransportConfig

      public TransportConfig toTransportConfig()
      Translates the common fields into transport settings.
      Returns:
      the transport configuration
    • authorizationHeader

      public Optional<String> authorizationHeader()
      Returns the Authorization header value when credentials are configured.
      Returns:
      Bearer … or Basic …, or empty
    • knownSecrets

      public List<String> knownSecrets()
      Returns the credential values that are removed from error messages and logs.
      Returns:
      secret values (password, token, encoded Basic credentials)
    • url

      public String url()
      Returns the endpoint base URL.
      Returns:
      the URL
    • tenantId

      public Optional<String> tenantId()
      Returns the tenant sent as X-Scope-OrgID.
      Returns:
      the tenant id, or empty (no header)
    • username

      public Optional<String> username()
      Returns the Basic-auth user.
      Returns:
      the user, or empty
    • password

      public Optional<Secret> password()
      Returns the Basic-auth password (or Grafana Cloud access token).
      Returns:
      the password, or empty
    • bearerToken

      public Optional<Secret> bearerToken()
      Returns the Bearer token for gateways.
      Returns:
      the token, or empty
    • multiTenantQueries

      public boolean multiTenantQueries()
      Returns whether tenant ids joined with | (multi-tenant queries) are allowed.
      Returns:
      true when allowed
    • allowedLabels

      public List<String> allowedLabels()
      Returns the cardinality guard allowlist: label names that may become index labels.
      Returns:
      the configured list, or Labels.DEFAULT_ALLOWED_LABELS
    • disableLabelGuard

      public boolean disableLabelGuard()
      Returns whether LokiClient.push(java.util.List<codes.grow.packages.loki.LokiStream>) sends every stream label as an index label.
      Returns:
      true when the guard is off for push (the log exporter always applies it)
    • connectTimeout

      public Duration connectTimeout()
      Returns the connect timeout.
      Returns:
      the timeout
    • requestTimeout

      public Duration requestTimeout()
      Returns the per-attempt timeout.
      Returns:
      the timeout
    • totalTimeout

      public Duration totalTimeout()
      Returns the total timeout per call including retries.
      Returns:
      the timeout
    • retry

      public RetryPolicy retry()
      Returns the retry policy.
      Returns:
      the policy
    • tls

      public TlsOptions tls()
      Returns the TLS options.
      Returns:
      the options
    • proxy

      public Optional<String> proxy()
      Returns the explicit proxy URL.
      Returns:
      the proxy, or empty
    • headers

      public Map<String,String> headers()
      Returns the extra request headers.
      Returns:
      immutable map
    • userAgentSuffix

      public Optional<String> userAgentSuffix()
      Returns the user agent suffix.
      Returns:
      the suffix, or empty
    • hooks

      public Hooks hooks()
      Returns the hooks.
      Returns:
      the hooks
    • logger

      public Logger logger()
      Returns the logger.
      Returns:
      the logger
    • toString

      public String toString()
      Overrides:
      toString in class Object