Class DatadogLogMapping

java.lang.Object
codes.grow.packages.datadoglogs.DatadogLogMapping

public final class DatadogLogMapping extends Object
LogRecord to Datadog JSON and back (API.md sections 5.2 and 6.1; DECISIONS 6, 7, 9).

Intake entries carry message, timestamp (epoch milliseconds), status, service, hostname, ddsource, ddtags, dd.trace_id / dd.span_id (decimal of the lower 64 bits), otel.trace_id / otel.span_id (lower hex, lossless), then the record attributes. User attributes named like a reserved key are sent as attr.<name>.

  • Field Details

    • MAX_ENTRIES_PER_REQUEST

      public static final int MAX_ENTRIES_PER_REQUEST
      Maximum entries in one intake request (Datadog limit).
      See Also:
    • MAX_PAYLOAD_BYTES

      public static final int MAX_PAYLOAD_BYTES
      Maximum uncompressed intake payload in bytes, brackets and commas included (5 MB).
      See Also:
    • MAX_LOG_BYTES

      public static final int MAX_LOG_BYTES
      Maximum serialized size of one log entry in bytes (1 MB).
      See Also:
    • TRUNCATED_ATTRIBUTE

      public static final String TRUNCATED_ATTRIBUTE
      Attribute set to true on an entry whose message the SDK truncated.
      See Also:
    • RESERVED_PREFIX

      public static final String RESERVED_PREFIX
      Prefix given to user attributes whose name is reserved (DECISIONS 7).
      See Also:
    • RESERVED_KEYS

      public static final List<String> RESERVED_KEYS
      Top-level keys the SDK writes itself.
  • Method Details

    • traceIdToDatadog

      public static Optional<String> traceIdToDatadog(String traceId)
      Datadog trace-id form (DECISIONS 6): the decimal string of the lower 64 bits of an OpenTelemetry trace id (up to 32 hex characters).
      
       traceIdToDatadog("4bf92f3577b34da6a3ce929d0e0e4736") // "11803532876627986230"
       
      Parameters:
      traceId - hex trace id, may be null
      Returns:
      the decimal form, or empty for invalid input
    • spanIdToDatadog

      public static Optional<String> spanIdToDatadog(String spanId)
      Datadog span-id form: the decimal string of a 16-hex OpenTelemetry span id.
      Parameters:
      spanId - hex span id, may be null
      Returns:
      the decimal form, or empty for invalid input
    • datadogIdToHex

      public static Optional<String> datadogIdToHex(Object id, int length)
      Converts a Datadog id (decimal string of up to 64 bits, or already lower hex) to lower hex padded to length characters.
      Parameters:
      id - the id: a string or an integral number, may be null
      length - 16 (span) or 32 (trace)
      Returns:
      the hex id, or empty for invalid or zero ids
    • datadogStatus

      public static String datadogStatus(LogRecord record)
      Datadog status of a record: its severity text lower-cased, else the canonical severity name.
      Parameters:
      record - the record
      Returns:
      the status, for example warn
    • severityFromStatus

      public static Severity severityFromStatus(String status)
      Severity of a Datadog status (unknown values give Severity.INFO).
      Parameters:
      status - status word, may be null
      Returns:
      the severity
    • buildDdtags

      public static Optional<String> buildDdtags(String env, String version, List<String> tags)
      Builds the ddtags value: env:, version:, then the tags; comma-joined, de-duplicated, whitespace and commas inside a tag replaced by _.
      Parameters:
      env - environment, may be null
      version - version, may be null
      tags - extra tags
      Returns:
      the value, or empty when there is no tag
    • toDatadogEntry

      public static Map<String,Object> toDatadogEntry(LogRecord record, DatadogLogsConfig config)
      Converts a record into a Datadog intake entry. Record resource values win over the config defaults (service, hostname, env, version); other resource keys are not sent.
      Parameters:
      record - the record
      config - defaults for service, hostname, env, version, tags and source
      Returns:
      the entry, in wire order
    • serializeEntry

      public static Optional<DatadogLogMapping.SerializedEntry> serializeEntry(Map<String,Object> entry, int maxBytes)
      Serializes an entry so it is at most maxBytes (DECISIONS 7). An oversized entry gets its message cut at a character boundary and grow.truncated: true.
      Parameters:
      entry - the entry from toDatadogEntry(codes.grow.packages.datadoglogs.logging.LogRecord, codes.grow.packages.datadoglogs.DatadogLogsConfig)
      maxBytes - maximum UTF-8 size
      Returns:
      the serialized entry, or empty when even an empty message does not fit
    • utf8Length

      public static int utf8Length(String text)
      UTF-8 byte length of a string.
      Parameters:
      text - text
      Returns:
      the length in bytes
    • parseLogEvent

      public static Optional<DatadogLogEvent> parseLogEvent(com.fasterxml.jackson.databind.JsonNode item)
      Parses one data[] item of a search response.
      Parameters:
      item - the JSON item
      Returns:
      the event, or empty when the item is not an object
    • eventToLogRecord

      public static LogRecord eventToLogRecord(DatadogLogEvent event)
      Normalizes a search event into a LogRecord: status to severity, service / host / env: / version: to the resource, custom attributes plus datadog.log_id and datadog.tags; trace ids from otel.trace_id / otel.span_id, else from dd.trace_id / dd.span_id (decimal or hex; only the lower 64 bits survive the decimal form).
      Parameters:
      event - the event
      Returns:
      the record