Class LokiLogExporter

java.lang.Object
codes.grow.packages.loki.LokiLogExporter
All Implemented Interfaces:
LogExporter

public final class LokiLogExporter extends Object implements LogExporter
LogExporter of the logging category that writes records with POST /loki/api/v1/push.

Mapping (recordsToStreams(java.util.List<codes.grow.packages.loki.logging.LogRecord>, codes.grow.packages.loki.CardinalityGuard)): resource and attribute keys are normalized ( service.name → service_name); allowlisted keys become index labels, plus level (lower-case severity) when allowed; everything else, and trace_id, span_id, trace_flags, severity_text, severity_number and observed_timestamp, becomes structured metadata. The body is the log line. Loki accepts or rejects a whole push, so a result is either all accepted or all rejected (with the error).

  • Field Details

    • METADATA_TRACE_ID

      public static final String METADATA_TRACE_ID
      Structured metadata key of the trace id.
      See Also:
    • METADATA_SPAN_ID

      public static final String METADATA_SPAN_ID
      Structured metadata key of the span id.
      See Also:
    • METADATA_TRACE_FLAGS

      public static final String METADATA_TRACE_FLAGS
      Structured metadata key of the trace flags.
      See Also:
    • METADATA_SEVERITY_TEXT

      public static final String METADATA_SEVERITY_TEXT
      Structured metadata key of the severity text.
      See Also:
    • METADATA_SEVERITY_NUMBER

      public static final String METADATA_SEVERITY_NUMBER
      Structured metadata key of the OTel severity number.
      See Also:
    • METADATA_OBSERVED_TIMESTAMP

      public static final String METADATA_OBSERVED_TIMESTAMP
      Structured metadata key of the observed timestamp (Unix nanoseconds).
      See Also:
  • Constructor Details

    • LokiLogExporter

      public LokiLogExporter(LokiClient client, PushFormat format, CardinalityGuard guard)
      Creates an exporter.
      Parameters:
      client - client (and tenant) to push with
      format - wire format
      guard - cardinality guard
  • Method Details

    • recordsToStreams

      public static List<LokiStream> recordsToStreams(List<LogRecord> records, CardinalityGuard guard)
      Converts records into Loki streams; records with the same final label set share a stream.
      Parameters:
      records - records
      guard - cardinality guard
      Returns:
      streams, in first-seen order of their label sets
    • export

      public CompletableFuture<ExportResult> export(List<LogRecord> records, ExportOptions options)
      Description copied from interface: LogExporter
      Exports a batch. The future completes with the accepted/rejected counts; it may also complete exceptionally (treated as "all rejected"). Cancelling the future cancels the export.
      Specified by:
      export in interface LogExporter
      Parameters:
      records - records to send (never empty)
      options - gzip flag and time budget
      Returns:
      the export result
    • shutdown

      public CompletableFuture<Void> shutdown()
      Description copied from interface: LogExporter
      Releases resources. Called once by BatchProcessor.shutdown.
      Specified by:
      shutdown in interface LogExporter
      Returns:
      completes when the exporter is closed