Class RemoteWriteV2Encoder

java.lang.Object
org.mockserver.metrics.remotewrite.RemoteWriteV2Encoder
All Implemented Interfaces:
RemoteWriteEncoder

public class RemoteWriteV2Encoder extends Object implements RemoteWriteEncoder
Hand-encodes a MetricSnapshots into a Prometheus Remote-Write 2.0 Request protobuf (package io.prometheus.write.v2), matching the Prometheus text-exposition naming so the pushed series line up exactly with what /mockserver/metrics serves. Pure and stateless — the interned symbol table is local to a single encode(io.prometheus.metrics.model.snapshots.MetricSnapshots, long) call, no network, no global state — so it is fully unit-testable.

Remote-Write 2.0 differs from v1 by interning every string (label names, label values, and metadata help/unit) into a shared symbols table and referencing them by uint32 index. Message shapes (field numbers are NORMATIVE and DIFFERENT from v1):

 Request    { reserved 1..3; repeated string symbols = 4; repeated TimeSeries timeseries = 5; }
 TimeSeries { repeated uint32 labels_refs = 1 [packed]; repeated Sample samples = 2; Metadata metadata = 5; int64 created_timestamp = 6; }
 Sample     { double value = 1; int64 timestamp = 2; }
 Metadata   { MetricType type = 1; uint32 help_ref = 3; uint32 unit_ref = 4; }
 
symbols[0] MUST be the empty string "" (a "no value" reference). Each nested message is built into its own byte[] and embedded in the parent via CodedOutputStream.writeByteArray(int, byte[]) — a length-delimited bytes field is wire-identical to an embedded message (and to a packed repeated field), which sidesteps manual size arithmetic.
  • Constructor Details

    • RemoteWriteV2Encoder

      public RemoteWriteV2Encoder()
  • Method Details

    • encode

      public byte[] encode(io.prometheus.metrics.model.snapshots.MetricSnapshots snapshots, long timestampMillis)
      Description copied from interface: RemoteWriteEncoder
      Encode the given metric snapshots into an uncompressed remote-write protobuf body. Every emitted sample carries timestampMillis (unix epoch milliseconds) as its timestamp.
      Specified by:
      encode in interface RemoteWriteEncoder
    • contentType

      public String contentType()
      Description copied from interface: RemoteWriteEncoder
      The HTTP Content-Type for this protocol version (v1: application/x-protobuf).
      Specified by:
      contentType in interface RemoteWriteEncoder
    • protocolVersionHeader

      public String protocolVersionHeader()
      Description copied from interface: RemoteWriteEncoder
      The value for the X-Prometheus-Remote-Write-Version header (v1: 0.1.0).
      Specified by:
      protocolVersionHeader in interface RemoteWriteEncoder