Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encoding series to a string #72

Open
D4no0 opened this issue Nov 18, 2021 · 1 comment
Open

Encoding series to a string #72

D4no0 opened this issue Nov 18, 2021 · 1 comment

Comments

@D4no0
Copy link

D4no0 commented Nov 18, 2021

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.

@mneudert
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants