-
Notifications
You must be signed in to change notification settings - Fork 224
feat: add telemetry #298
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
Merged
Merged
feat: add telemetry #298
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3b5e072
feat: add server telemetry
polvalente 3468d17
fix: restore call arg
polvalente 03455bd
feat: add clien-side telemetry
polvalente f8d3053
test: add tests
polvalente d5f7460
wip: add livebook for grpc_prometheus replacement
polvalente bec5fcc
feat: add server telemetry prometheus conversion example
polvalente 510077e
chore: format
polvalente 24a80d6
test: test client telemetry events
polvalente d701a11
refactor: use :telemetry.span for server telemetry
polvalente 36ba166
refactor: use telemetry span for client telemetry
polvalente 697da79
Merge branch 'master' into pv-feat/add-telemetry
polvalente File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -265,7 +265,61 @@ defmodule GRPC.Stub do | |
accepted_compressors: accepted_compressors | ||
} | ||
|
||
do_call(req_stream, stream, request, opts) | ||
t0 = System.monotonic_time() | ||
|
||
try do | ||
:ok = GRPC.Telemetry.client_rpc_start(stream) | ||
do_call(req_stream, stream, request, opts) | ||
rescue | ||
e -> | ||
stacktrace = __STACKTRACE__ | ||
Logger.error(Exception.format(:error, e, stacktrace)) | ||
duration = System.monotonic_time() - t0 | ||
normalized_reason = Exception.normalize(:error, e, stacktrace) | ||
|
||
:ok = | ||
GRPC.Telemetry.client_rpc_exception( | ||
stream, | ||
:error, | ||
normalized_reason, | ||
stacktrace, | ||
duration | ||
) | ||
|
||
reraise e, __STACKTRACE__ | ||
catch | ||
kind, reason -> | ||
stacktrace = __STACKTRACE__ | ||
Logger.error(Exception.format(kind, reason, stacktrace)) | ||
duration = System.monotonic_time() - t0 | ||
normalized_reason = Exception.normalize(kind, reason, stacktrace) | ||
|
||
:ok = | ||
GRPC.Telemetry.client_rpc_exception( | ||
stream, | ||
kind, | ||
normalized_reason, | ||
stacktrace, | ||
duration | ||
) | ||
|
||
# re-raise accordingly | ||
case kind do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
:exit -> | ||
exit(reason) | ||
|
||
:throw -> | ||
throw(reason) | ||
|
||
:error -> | ||
:erlang.error(reason) | ||
end | ||
else | ||
result -> | ||
duration = System.monotonic_time() - t0 | ||
:ok = GRPC.Telemetry.client_rpc_stop(stream, duration) | ||
result | ||
end | ||
end | ||
|
||
defp do_call( | ||
|
This file contains hidden or 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,171 @@ | ||
defmodule GRPC.Telemetry do | ||
@moduledoc """ | ||
Events published by GRPC | ||
|
||
These can be divided in client-side events and server-side events. | ||
|
||
## Client-side Events | ||
|
||
* `[:grpc, :client, :rpc, :start]` - Published before all interceptors are executed. | ||
* `[:grpc, :client, :rpc, :stop]` - Published after all interceptors executed successfully. | ||
* `:duration` - the duration as measured through `System.monotonic_time()` | ||
for the whole interceptor pipeline. | ||
* `[:grpc, :client, :rpc, :exception]` - Published if any exception occurs while receiving a message. | ||
* `:duration` - the duration as measured through `System.monotonic_time()` | ||
for the execution since the start of the pipeline until the exception happened. | ||
|
||
| event | measurements | metadata | | ||
|--------------|--------------|----------| | ||
| `[:rpc, :start]` | `:count` | `:stream` | | ||
| `[:rpc, :stop]` | `:duration` | `:stream` | | ||
| `[:rpc, :exception]` | `:duration` | `:stream`, `:kind`, `:reason`, `:stacktrace` | | ||
|
||
### Metadata | ||
|
||
* `:stream` - the `%GRPC.Server.Stream{}` for the request | ||
* `:function_name` - the name of the function called | ||
* `:server` - the server module name | ||
* `:endpoint` - the endpoint module name | ||
|
||
`:exception` events also include some error metadata: | ||
|
||
* `:reason` is the error value in case of `catch` or the actual exception in case of `rescue`. | ||
* `:kind` can be one of: | ||
* `:error` — from an `{:error, error}` return value. Some Erlang functions may also throw an | ||
`:error` tuple, which will be reported as `:error`. | ||
* `:exit` — from a caught process exit. | ||
* `:throw` — from a caught value, this doesn't necessarily mean that an error occurred. | ||
|
||
## Server-side Events | ||
|
||
* `[:grpc, :server, :rpc, :start]` - Published before all interceptors are executed. | ||
* `[:grpc, :server, :rpc, :stop]` - Published after all interceptors executed successfully. | ||
* `:duration` - the duration as measured through `System.monotonic_time()` | ||
for the whole interceptor pipeline. | ||
* `[:grpc, :server, :rpc, :exception]` - Published if any exception occurs while receiving a message. | ||
* `:duration` - the duration as measured through `System.monotonic_time()` | ||
for the execution since the start of the pipeline until the exception happened. | ||
|
||
| event | measurements | metadata | | ||
|--------------|--------------|----------| | ||
| `[:rpc, :start]` | `:count` | `:stream`, `:server`, `:endpoint`, `:function_name` | | ||
| `[:rpc, :stop]` | `:duration` | `:stream`, `:server`, `:endpoint`, `:function_name` , `:result` | | ||
| `[:rpc, :exception]` | `:duration` | `:stream`, `:server`, `:endpoint`, `:function_name`, `:kind`, `:reason`, `:stacktrace` | | ||
|
||
### Metadata | ||
|
||
* `:stream` - the `%GRPC.Server.Stream{}` for the request. | ||
* `:function_name` - the name of the function called. | ||
* `:server` - the server module name. | ||
* `:endpoint` - the endpoint module name. | ||
* `:result` - the result returned from the interceptor pipeline. | ||
|
||
`:exception` events also include some error metadata: | ||
|
||
* `:reason` is the error value in case of `catch` or the actual exception in case of `rescue`. | ||
* `:kind` can be one of: | ||
* `:error` — from an `{:error, error}` return value. Some Erlang functions may also throw an | ||
`:error` tuple, which will be reported as `:error`. | ||
* `:exit` — from a caught process exit. | ||
* `:throw` — from a caught value, this doesn't necessarily mean that an error occurred. | ||
""" | ||
|
||
@server_rpc [:grpc, :server, :rpc] | ||
@client_rpc [:grpc, :client, :rpc] | ||
|
||
@server_rpc_start_name @server_rpc ++ [:start] | ||
|
||
@doc false | ||
def server_rpc_start_name, do: @server_rpc_start_name | ||
|
||
@doc false | ||
def server_rpc_start(server, endpoint, func_name, stream) do | ||
:telemetry.execute(@server_rpc_start_name, %{count: 1}, %{ | ||
server: server, | ||
endpoint: endpoint, | ||
function_name: func_name, | ||
stream: stream | ||
}) | ||
end | ||
|
||
@server_rpc_stop_name @server_rpc ++ [:stop] | ||
|
||
@doc false | ||
def server_rpc_stop_name, do: @server_rpc_stop_name | ||
|
||
@doc false | ||
def server_rpc_stop(server, endpoint, func_name, stream, result, duration) do | ||
:telemetry.execute(@server_rpc_stop_name, %{duration: duration}, %{ | ||
server: server, | ||
endpoint: endpoint, | ||
function_name: func_name, | ||
stream: stream, | ||
result: result | ||
}) | ||
end | ||
|
||
@server_rpc_exception_name @server_rpc ++ [:exception] | ||
|
||
@doc false | ||
def server_rpc_exception_name, do: @server_rpc_exception_name | ||
|
||
@doc false | ||
def server_rpc_exception( | ||
server, | ||
endpoint, | ||
func_name, | ||
stream, | ||
kind, | ||
reason, | ||
stacktrace, | ||
duration | ||
) do | ||
:telemetry.execute(@server_rpc_exception_name, %{duration: duration}, %{ | ||
server: server, | ||
endpoint: endpoint, | ||
function_name: func_name, | ||
stream: stream, | ||
kind: kind, | ||
reason: reason, | ||
stacktrace: stacktrace | ||
}) | ||
end | ||
|
||
@client_rpc_start_name @client_rpc ++ [:start] | ||
@doc false | ||
def client_rpc_start_name, do: @client_rpc_start_name | ||
|
||
@doc false | ||
def client_rpc_start(stream) do | ||
:telemetry.execute(@client_rpc_start_name, %{count: 1}, %{stream: stream}) | ||
end | ||
|
||
@client_rpc_stop_name @client_rpc ++ [:stop] | ||
@doc false | ||
def client_rpc_stop_name, do: @client_rpc_stop_name | ||
|
||
@doc false | ||
def client_rpc_stop(stream, duration) do | ||
:telemetry.execute(@client_rpc_stop_name, %{duration: duration}, %{stream: stream}) | ||
end | ||
|
||
@client_rpc_exception_name @client_rpc ++ [:exception] | ||
@doc false | ||
def client_rpc_exception_name, do: @client_rpc_exception_name | ||
|
||
@doc false | ||
def client_rpc_exception( | ||
stream, | ||
kind, | ||
reason, | ||
stacktrace, | ||
duration | ||
) do | ||
:telemetry.execute(@client_rpc_exception_name, %{duration: duration}, %{ | ||
stream: stream, | ||
kind: kind, | ||
reason: reason, | ||
stacktrace: stacktrace | ||
}) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
:telemetry.span
suitable for this case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:telemetry.span
doesn't treatrescue
separatelyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I tested locally that
raise
can becatch
-ed as an:error
, so it seems that it's indeed equivalent, though I have to check if the only metadata that changes is what span handles :) I'll keep this unresolved til I confirm this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were some changes needed anyway, but I went with span because it decouples a bit of the code and leaves basically the formatting and error-case handling to us.