instream icon indicating copy to clipboard operation
instream copied to clipboard

Encoding series to a string

Open D4no0 opened this issue 3 years ago • 1 comments

It is possible currently to take a Instream.Series implementation and encode it to string?

The use-case is to send metrics from server to a metrics poller. Meaning that the poller will actually insert the data in database and all I have to do is send series encoded as string.

D4no0 avatar Nov 18 '21 13:11 D4no0

That conversion is definitely possible.

Depending on the string format you need you should receive something usable by taking the following steps (both last v1.0.0 release and current development version):

# create/query your point with values
point = %MySeries{}

# convert it to a plain map
data = %{
  measurement: MySeries.__meta__(:measurement),
  fields: Map.from_struct(point.fields),
  tags: Map.from_struct(point.tags),
  timestamp: timestamp
}

# convert it to InfluxDB line format
line = Instream.Encoder.Line.encode(data)

I will update the line format encoder to not require the conversion to plain maps and have public documentation.

Untested but if you want to generate a JSON string the following could work if you are using :jason:

defimpl Jason.Encoder, for: MySeries do
  def encode(point, opts) do
    Jason.Encode.map(
      %{
        measurement: MySeries.__meta__(:measurement),
        fields: Map.from_struct(point.fields),
        tags: Map.from_struct(point.tags),
        timestamp: timestamp
      },
      opts
    )
  end
end

mneudert avatar Nov 19 '21 15:11 mneudert