Skip to content

Commit

Permalink
Merge pull request #104 from andrewhr/add-telemetry-events
Browse files Browse the repository at this point in the history
Telemetry for Elasticsearch API calls
  • Loading branch information
danielberkompas committed Oct 1, 2022
2 parents 9a57010 + e73d80f commit d4ffddc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
37 changes: 32 additions & 5 deletions lib/elasticsearch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ defmodule Elasticsearch do
You should configure at least one `Elasticsearch.Cluster` in order to
use the functions in this module, or else you'll need to pass all the
configuration for the cluster into each function call.
### Telemetry
The following events are published:
* `[:elasticsearch, :request, :start]` - emitted at the beginning of the request to Elasticsearch.
* Measurement: `%{system_time: System.system_time()}`
* Metadata: `%{telemetry_span_context: term(), config: Elasticsearch.Cluster.config(),
method: Elasticsearch.API.method(), url: Elasticsearch.API.url(), data: Elasticsearch.API.data()}`
* `[:elasticsearch, :request, :stop]` - emitted at the end of the request to Elasticsearch.
* Measurement: `%{duration: native_time}`
* Metadata: `%{telemetry_span_context: term(), result: Elasticsearch.API.response()}`
* `[:elasticsearch, :request, :exception]` - emitted when an exception has been raised.
* Measurement: `%{system_time: System.system_time()}`
* Metadata: `%{telemetry_span_context: term(), kind: Exception.kind(), reason: term(),
stacktrace: Exception.stacktrace()}`
"""

alias Elasticsearch.{
Expand Down Expand Up @@ -230,7 +248,7 @@ defmodule Elasticsearch do
config = Config.get(cluster)

config
|> config.api.request(:get, url, "", opts)
|> do_request(:get, url, "", opts)
|> format()
end

Expand Down Expand Up @@ -286,7 +304,7 @@ defmodule Elasticsearch do
config = Config.get(cluster)

config
|> config.api.request(:put, url, data, opts)
|> do_request(:put, url, data, opts)
|> format()
end

Expand Down Expand Up @@ -337,7 +355,7 @@ defmodule Elasticsearch do
config = Config.get(cluster)

config
|> config.api.request(:post, url, data, opts)
|> do_request(:post, url, data, opts)
|> format()
end

Expand Down Expand Up @@ -399,7 +417,7 @@ defmodule Elasticsearch do
config = Config.get(cluster)

config
|> config.api.request(:delete, url, "", opts)
|> do_request(:delete, url, "", opts)
|> format()
end

Expand Down Expand Up @@ -454,7 +472,7 @@ defmodule Elasticsearch do
config = Config.get(cluster)

config
|> config.api.request(:head, url, "", opts)
|> do_request(:head, url, "", opts)
|> format()
end

Expand All @@ -480,6 +498,15 @@ defmodule Elasticsearch do
|> unwrap!()
end

defp do_request(config, method, url, data, opts) do
start_metadata = %{config: config, method: method, url: url, data: data}

:telemetry.span([:elasticsearch, :request], start_metadata, fn ->
result = config.api.request(config, method, url, data, opts)
{result, %{result: result}}
end)
end

defp format({:ok, %{status_code: code, body: body}})
when code >= 200 and code < 300 do
{:ok, body}
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmodule Elasticsearch.Mixfile do
[
{:poison, ">= 0.0.0", optional: true},
{:httpoison, ">= 0.0.0"},
{:telemetry, "~> 0.4.3 or ~> 1.0"},
{:vex, "~> 0.6"},
{:sigaws_otp_24, "~> 1.0", optional: true},
{:postgrex, ">= 0.0.0", only: [:dev, :test]},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"postgrex": {:hex, :postgrex, "0.15.7", "724410acd48abac529d0faa6c2a379fb8ae2088e31247687b16cacc0e0883372", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "88310c010ff047cecd73d5ceca1d99205e4b1ab1b9abfdab7e00f5c9d20ef8f9"},
"sigaws": {:hex, :sigaws, "0.7.2", "2b0bcd3979f2ae19337d0a6e52b4b1f37ac3d778201019240e641471a2d36685", [:mix], [], "hexpm", "5d3f3c9a259b052015297b3392800b95a7672c7cf3e878cf88c0d33213ac2e55"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"},
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
"vex": {:hex, :vex, "0.8.0", "0a04e3aebe5ec443525c88f3833b4481d97de272f891243b62b18efbda85b121", [:mix], [], "hexpm", "36a37f264b36fe4b0430c9f174d71c59845cf56d9e2ae32960038432b16961ea"},
}

0 comments on commit d4ffddc

Please sign in to comment.