-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09a5ffa
commit 5984b9d
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule TelemetryMetricsTelegraf.Adapters.InstreamV2 do | ||
@moduledoc """ | ||
Writer adapter for 2+ versions [Instream](https://hex.pm/packages/instream). | ||
## Options | ||
* `:connection` - `Instream` connection module, required; | ||
* `:log` - controls instream queries logging, default - `false`; | ||
## Example | ||
# instream connection | ||
defmodule MyApp.MyConnection do | ||
use Instream.Connection, otp_app: :my_app | ||
end | ||
# telemetry metrics supervisor | ||
defmodule MyApp.Telemetry do | ||
use Supervisor | ||
import Telemetry.Metrics | ||
def start_link(arg) do | ||
Supervisor.start_link(__MODULE__, arg, name: __MODULE__) | ||
end | ||
@impl true | ||
def init(_arg) do | ||
children = [ | ||
{TelemetryMetricsTelegraf, | ||
metrics: metrics(), | ||
adapter: {TelemetryMetricsTelegraf.Adapters.Instream, [connection: MyApp.MyConnection]}} | ||
] | ||
Supervisor.init(children, strategy: :one_for_one) | ||
end | ||
def metrics do | ||
[ | ||
# ... | ||
] | ||
end | ||
end | ||
""" | ||
|
||
@behaviour TelemetryMetricsTelegraf.Writer | ||
|
||
@type options :: [{:connection, module()}, {:log, boolean()}] | ||
|
||
@impl TelemetryMetricsTelegraf.Writer | ||
@spec init(options()) :: options() | ||
def init(opts) do | ||
[ | ||
connection: Keyword.fetch!(opts, :connection), | ||
log: Keyword.get(opts, :log, false) | ||
] | ||
end | ||
|
||
@impl TelemetryMetricsTelegraf.Writer | ||
def write(measurement_name, tags, fields, opts) do | ||
opts[:connection].write( | ||
%{measurement: measurement_name, fields: fields, tags: tags}, | ||
log: opts[:log] | ||
) | ||
end | ||
end |