Last active
October 9, 2024 11:42
-
-
Save maltoe/62c1b8b13f39552627f009b60f0486c2 to your computer and use it in GitHub Desktop.
Appsignal: Handling exceptions in plug in endpoint
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
@impl Plug | |
# naively copied version from Appsignal.Plug | |
def call(%{path_info: ["webhook", "my-webhook"]} = conn, opts) do | |
Appsignal.instrument(fn span -> | |
_ = Appsignal.Span.set_namespace(span, "http_request") | |
try do | |
MyUnderlyingPlug.call(conn, opts) | |
catch | |
kind, reason -> | |
stack = __STACKTRACE__ | |
_ = | |
# handle error condensed a bit, as the Webhook will never raise a WrapperError | |
span | |
|> Appsignal.Span.add_error(kind, reason, stack) | |
|> Appsignal.Tracer.close_span() | |
Appsignal.Tracer.ignore() | |
:erlang.raise(kind, reason, stack) | |
end | |
end) | |
end |
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
@impl Plug | |
def call(%{path_info: ["webhook", "my-webhook"]} = conn, opts) do | |
span = | |
"http_request" | |
|> Span.create_root(self()) | |
|> Span.set_name("MyApp.WebhookPlugWrapper#call") | |
conn = | |
try do | |
MyUnderlyingPlug.call(conn, opts) | |
catch | |
kind, reason -> | |
Span.add_error(span, kind, reason, __STACKTRACE__) | |
conn | |
|> Plug.Conn.send_resp(500, "Internal Server Error") | |
|> Plug.Conn.halt() | |
end | |
Span.close(span) | |
conn | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment