-module(datadog_client).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/datadog_client.gleam").
-export([new/1, with_site/2, encode_to_json/1, to_request/2, send/2, send_one/2]).
-export_type([client/0, send_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type client() :: {client, binary(), binary()}.
-type send_error() :: {http_error, binary()} | {api_error, integer(), binary()}.
-file("src/datadog_client.gleam", 35).
?DOC(" Build a client for the default site (`datadoghq.com`).\n").
-spec new(binary()) -> client().
new(Api_key) ->
{client, Api_key, <<"datadoghq.com"/utf8>>}.
-file("src/datadog_client.gleam", 40).
?DOC(" Override the Datadog site (e.g. `\"datadoghq.eu\"`).\n").
-spec with_site(client(), binary()) -> client().
with_site(Client, Site) ->
{client, erlang:element(2, Client), Site}.
-file("src/datadog_client.gleam", 124).
?DOC(false).
-spec encode_to_json(list(datadog_client@metric:metric())) -> binary().
encode_to_json(Metrics) ->
_pipe = gleam@json:object(
[{<<"series"/utf8>>,
gleam@json:array(Metrics, fun datadog_client@metric:to_json/1)}]
),
gleam@json:to_string(_pipe).
-file("src/datadog_client.gleam", 48).
?DOC(
" Build the HTTPS request body for `/api/v1/series` without sending it.\n"
" Use this if you want to send via your own HTTP backend.\n"
).
-spec to_request(client(), list(datadog_client@metric:metric())) -> gleam@http@request:request(binary()).
to_request(Client, Metrics) ->
_pipe = gleam@http@request:new(),
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_scheme(_pipe@1, https),
_pipe@3 = gleam@http@request:set_host(
_pipe@2,
<<"api."/utf8, (erlang:element(3, Client))/binary>>
),
_pipe@4 = gleam@http@request:set_path(_pipe@3, <<"/api/v1/series"/utf8>>),
_pipe@5 = gleam@http@request:set_header(
_pipe@4,
<<"dd-api-key"/utf8>>,
erlang:element(2, Client)
),
_pipe@6 = gleam@http@request:set_header(
_pipe@5,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_body(_pipe@6, encode_to_json(Metrics)).
-file("src/datadog_client.gleam", 63).
?DOC(" POST one or more metrics to `/api/v1/series`. Erlang target.\n").
-spec send(client(), list(datadog_client@metric:metric())) -> {ok,
gleam@http@response:response(binary())} |
{error, send_error()}.
send(Client, Metrics) ->
gleam@result:'try'(
begin
_pipe = gleam@httpc:send(to_request(Client, Metrics)),
gleam@result:map_error(
_pipe,
fun(E) -> {http_error, gleam@string:inspect(E)} end
)
end,
fun(Resp) -> case erlang:element(2, Resp) of
S when (S >= 200) andalso (S < 300) ->
{ok, Resp};
S@1 ->
{error, {api_error, S@1, erlang:element(4, Resp)}}
end end
).
-file("src/datadog_client.gleam", 80).
?DOC(" Send a single metric. Convenience over `send`. Erlang target.\n").
-spec send_one(client(), datadog_client@metric:metric()) -> {ok,
gleam@http@response:response(binary())} |
{error, send_error()}.
send_one(Client, M) ->
send(Client, [M]).