Add bandit telemetry support for opentelemetry-phoenix (#249)
* Add bandit telemetry support for opentelemetry-phoenix --------- Co-authored-by: Bryan Naegele <bryannaegele@users.noreply.github.com>
This commit is contained in:
parent
b12a464b5f
commit
5658c54eb9
|
@ -85,6 +85,9 @@ opentelemetry_oban:
|
||||||
opentelemetry_phoenix:
|
opentelemetry_phoenix:
|
||||||
- instrumentation/opentelemetry_phoenix/**/*
|
- instrumentation/opentelemetry_phoenix/**/*
|
||||||
|
|
||||||
|
opentelemetry_bandit:
|
||||||
|
- instrumentation/opentelemetry_bandit/**/*
|
||||||
|
|
||||||
opentelemetry_process_propagator:
|
opentelemetry_process_propagator:
|
||||||
- propagators/opentelemetry_process_propagator/**/*
|
- propagators/opentelemetry_process_propagator/**/*
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
_extends: opentelemetry-erlang-contrib:.github/release-drafter.yml
|
||||||
|
name-template: 'Opentelemetry Bandit - v$RESOLVED_VERSION'
|
||||||
|
tag-template: 'opentelemetry-bandit-v$RESOLVED_VERSION'
|
||||||
|
tag-prefix: opentelemetry-bandit-v
|
||||||
|
include-paths:
|
||||||
|
- instrumentation/opentelemetry_bandit/
|
||||||
|
|
||||||
|
footer: |
|
||||||
|
|
||||||
|
---
|
||||||
|
[Changelog](https://$REPOSITORY/blob/main/instrumentation/opentelemetry_bandit/CHANGELOG.MD)
|
|
@ -328,6 +328,45 @@ jobs:
|
||||||
- name: Test
|
- name: Test
|
||||||
run: mix test
|
run: mix test
|
||||||
|
|
||||||
|
opentelemetry-bandit:
|
||||||
|
needs: [test-matrix]
|
||||||
|
if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_bandit'))
|
||||||
|
env:
|
||||||
|
app: "opentelemetry_bandit"
|
||||||
|
defaults:
|
||||||
|
run:
|
||||||
|
working-directory: instrumentation/${{ env.app }}
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
name: Opentelemetry Bandit test on Elixir ${{ matrix.elixir_version }} (OTP ${{ matrix.otp_version }})
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix: ${{ fromJson(needs.test-matrix.outputs.matrix) }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- uses: erlef/setup-beam@v1
|
||||||
|
with:
|
||||||
|
version-type: strict
|
||||||
|
otp-version: ${{ matrix.otp_version }}
|
||||||
|
elixir-version: ${{ matrix.elixir_version }}
|
||||||
|
rebar3-version: ${{ matrix.rebar3_version }}
|
||||||
|
- name: Cache
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/deps
|
||||||
|
~/_build
|
||||||
|
key: ${{ runner.os }}-build-${{ matrix.otp_version }}-${{ matrix.elixir_version }}-v3-${{ hashFiles('**/mix.lock') }}
|
||||||
|
- name: Fetch deps
|
||||||
|
if: steps.deps-cache.outputs.cache-hit != 'true'
|
||||||
|
run: mix deps.get
|
||||||
|
- name: Compile project
|
||||||
|
run: mix compile --warnings-as-errors
|
||||||
|
- name: Check formatting
|
||||||
|
run: mix format --check-formatted
|
||||||
|
if: matrix.check_formatted
|
||||||
|
- name: Test
|
||||||
|
run: mix test
|
||||||
|
|
||||||
opentelemetry-redix:
|
opentelemetry-redix:
|
||||||
needs: [test-matrix]
|
needs: [test-matrix]
|
||||||
if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_redix'))
|
if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_redix'))
|
||||||
|
|
|
@ -126,6 +126,16 @@ jobs:
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
opentelemetry-bandit-release:
|
||||||
|
name: '[opentelemetry-bandit-release] Draft release'
|
||||||
|
runs-on: ubuntu-22.04
|
||||||
|
steps:
|
||||||
|
- uses: release-drafter/release-drafter@v5
|
||||||
|
with:
|
||||||
|
config-name: release-drafter-templates/opentelemetry-bandit.yml
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
opentelemetry-process-propagator-release:
|
opentelemetry-process-propagator-release:
|
||||||
name: '[opentelemetry-process-propagator-release] Draft release'
|
name: '[opentelemetry-process-propagator-release] Draft release'
|
||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
|
@ -0,0 +1,27 @@
|
||||||
|
# The directory Mix will write compiled artifacts to.
|
||||||
|
/_build/
|
||||||
|
|
||||||
|
# If you run "mix test --cover", coverage assets end up here.
|
||||||
|
/cover/
|
||||||
|
|
||||||
|
# The directory Mix downloads your dependencies sources to.
|
||||||
|
/deps/
|
||||||
|
|
||||||
|
# Where third-party dependencies like ExDoc output generated docs.
|
||||||
|
/doc/
|
||||||
|
|
||||||
|
# Ignore .fetch files in case you like to edit your project deps locally.
|
||||||
|
/.fetch
|
||||||
|
|
||||||
|
# If the VM crashes, it generates a dump, let's ignore it too.
|
||||||
|
erl_crash.dump
|
||||||
|
|
||||||
|
# Also ignore archive artifacts (built via "mix archive.build").
|
||||||
|
*.ez
|
||||||
|
|
||||||
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
|
*.tar
|
||||||
|
|
||||||
|
# Temporary files, for example, from tests.
|
||||||
|
/tmp/
|
||||||
|
.DS_Store
|
|
@ -0,0 +1,5 @@
|
||||||
|
# Changelog
|
||||||
|
|
||||||
|
## [0.1.4] - 2023-12-14
|
||||||
|
### Changed
|
||||||
|
- Prepare to the public release
|
|
@ -0,0 +1,177 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
|
@ -0,0 +1,37 @@
|
||||||
|
# OpentelemetryBandit
|
||||||
|
|
||||||
|
[![EEF Observability WG project](https://img.shields.io/badge/EEF-Observability-black)](https://github.com/erlef/eef-observability-wg)
|
||||||
|
[![Hex.pm](https://img.shields.io/hexpm/v/opentelemetry_bandit)](https://hex.pm/packages/opentelemetry_bandit)
|
||||||
|
![Build Status](https://github.com/opentelemetry-beam/opentelemetry_bandit/workflows/Tests/badge.svg)
|
||||||
|
|
||||||
|
Telemetry handler that creates Opentelemetry spans from [Bandit events](https://hexdocs.pm/bandit/Bandit.Telemetry.html#content).
|
||||||
|
|
||||||
|
After installing, setup the handler in your application behaviour before your top-level supervisor starts.
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
OpentelemetryBandit.setup()
|
||||||
|
```
|
||||||
|
|
||||||
|
When phoenix is used, setup telemetry this way:
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
OpentelemetryBandit.setup()
|
||||||
|
OpentelemetryPhoenix.setup(adapter: :bandit)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Compatibility Matrix
|
||||||
|
|
||||||
|
| OpentelemetryPhoenix Version | Otel Version | Notes |
|
||||||
|
| :--------------------------- | :----------- | :---- |
|
||||||
|
| | | |
|
||||||
|
| v0.1.4 | v1.0 | |
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
def deps do
|
||||||
|
[
|
||||||
|
{:opentelemetry_bandit, "~> 0.1.4"}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
```
|
|
@ -0,0 +1,7 @@
|
||||||
|
import Config
|
||||||
|
|
||||||
|
try do
|
||||||
|
import_config "#{Mix.env()}.exs"
|
||||||
|
rescue
|
||||||
|
_ -> :ok
|
||||||
|
end
|
|
@ -0,0 +1,6 @@
|
||||||
|
import Config
|
||||||
|
|
||||||
|
config :opentelemetry,
|
||||||
|
processors: [{:otel_simple_processor, %{}}]
|
||||||
|
|
||||||
|
config :logger, level: :error
|
|
@ -0,0 +1,177 @@
|
||||||
|
defmodule OpentelemetryBandit do
|
||||||
|
@moduledoc """
|
||||||
|
OpentelemetryBandit uses [telemetry](https://hexdocs.pm/telemetry/) handlers to create `OpenTelemetry` spans.
|
||||||
|
|
||||||
|
Supported:
|
||||||
|
1. :bandit, :request, :stop
|
||||||
|
2. :bandit, :request, :exception
|
||||||
|
3. :bandit, :websocket, :stop
|
||||||
|
"""
|
||||||
|
|
||||||
|
alias OpenTelemetry.SemanticConventions.Trace
|
||||||
|
require Trace
|
||||||
|
require OpenTelemetry.Tracer
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Initializes and configures the telemetry handlers.
|
||||||
|
"""
|
||||||
|
@spec setup(any) :: :ok
|
||||||
|
def setup(_opts \\ []) do
|
||||||
|
:telemetry.attach(
|
||||||
|
{__MODULE__, :request_stop},
|
||||||
|
[:bandit, :request, :stop],
|
||||||
|
&__MODULE__.handle_request_stop/4,
|
||||||
|
%{}
|
||||||
|
)
|
||||||
|
|
||||||
|
:telemetry.attach(
|
||||||
|
{__MODULE__, :request_exception},
|
||||||
|
[:bandit, :request, :exception],
|
||||||
|
&__MODULE__.handle_request_exception/4,
|
||||||
|
%{}
|
||||||
|
)
|
||||||
|
|
||||||
|
:telemetry.attach(
|
||||||
|
{__MODULE__, :websocket_stop},
|
||||||
|
[:bandit, :websocket, :stop],
|
||||||
|
&__MODULE__.handle_websocket_stop/4,
|
||||||
|
%{}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_request_stop(_event, measurements, meta, _config) do
|
||||||
|
conn = Map.get(meta, :conn)
|
||||||
|
duration = measurements.duration
|
||||||
|
end_time = :opentelemetry.timestamp()
|
||||||
|
start_time = end_time - duration
|
||||||
|
|
||||||
|
url = extract_url(meta, conn)
|
||||||
|
request_path = extract_request_path(meta, conn)
|
||||||
|
|
||||||
|
attributes =
|
||||||
|
if Map.has_key?(meta, :error) do
|
||||||
|
%{
|
||||||
|
Trace.http_url() => url,
|
||||||
|
Trace.http_method() => meta.method,
|
||||||
|
Trace.net_transport() => :"IP.TCP",
|
||||||
|
Trace.http_response_content_length() => measurements.resp_body_bytes,
|
||||||
|
Trace.http_status_code() => meta.status
|
||||||
|
}
|
||||||
|
else
|
||||||
|
%{
|
||||||
|
Trace.http_url() => url,
|
||||||
|
Trace.http_client_ip() => client_ip(conn),
|
||||||
|
Trace.http_scheme() => conn.scheme,
|
||||||
|
Trace.net_peer_name() => conn.host,
|
||||||
|
Trace.net_peer_port() => conn.port,
|
||||||
|
Trace.http_target() => conn.request_path,
|
||||||
|
Trace.http_method() => meta.method,
|
||||||
|
Trace.http_status_code() => meta.status,
|
||||||
|
Trace.http_response_content_length() => measurements.resp_body_bytes,
|
||||||
|
Trace.net_transport() => :"IP.TCP",
|
||||||
|
Trace.http_user_agent() => user_agent(conn)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
span_kind = if Map.has_key?(meta, :error), do: :error, else: :server
|
||||||
|
|
||||||
|
span_id = "HTTP #{meta.method} #{request_path}" |> String.trim()
|
||||||
|
|
||||||
|
OpenTelemetry.Tracer.start_span(span_id, %{
|
||||||
|
attributes: attributes,
|
||||||
|
start_time: start_time,
|
||||||
|
end_time: end_time,
|
||||||
|
kind: span_kind
|
||||||
|
})
|
||||||
|
|> set_span_status(meta, Map.get(meta, :error, ""))
|
||||||
|
|> OpenTelemetry.Span.end_span()
|
||||||
|
|
||||||
|
OpenTelemetry.Ctx.clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_request_exception(_event, _measurements, meta, _config) do
|
||||||
|
OpenTelemetry.Tracer.start_span("HTTP exception #{inspect(meta.exception.__struct__)}", %{
|
||||||
|
kind: :error,
|
||||||
|
status: :error
|
||||||
|
})
|
||||||
|
|> set_span_status(meta, inspect(meta.stacktrace))
|
||||||
|
|> OpenTelemetry.Span.end_span()
|
||||||
|
|
||||||
|
OpenTelemetry.Ctx.clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
def handle_websocket_stop(_event, measurements, meta, _config) do
|
||||||
|
duration = measurements.duration
|
||||||
|
end_time = :opentelemetry.timestamp()
|
||||||
|
start_time = end_time - duration
|
||||||
|
|
||||||
|
attributes = %{
|
||||||
|
:"websocket.recv.binary.frame.bytes" => Map.get(measurements, :send_binary_frame_bytes, 0),
|
||||||
|
:"websocket.send.binary.frame.bytes" => Map.get(measurements, :recv_binary_frame_bytes, 0),
|
||||||
|
Trace.net_transport() => :websocket
|
||||||
|
}
|
||||||
|
|
||||||
|
span_kind = if Map.has_key?(meta, :error), do: :error, else: :server
|
||||||
|
|
||||||
|
OpenTelemetry.Tracer.start_span("Websocket", %{
|
||||||
|
attributes: attributes,
|
||||||
|
start_time: start_time,
|
||||||
|
end_time: end_time,
|
||||||
|
kind: span_kind
|
||||||
|
})
|
||||||
|
|> set_span_status(meta, Map.get(meta, :error, ""))
|
||||||
|
|> OpenTelemetry.Span.end_span()
|
||||||
|
|
||||||
|
OpenTelemetry.Ctx.clear()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp set_span_status(span, meta, message) do
|
||||||
|
status = if Map.has_key?(meta, :error) || message != "", do: :error, else: :ok
|
||||||
|
|
||||||
|
OpenTelemetry.Span.set_status(span, OpenTelemetry.status(status, message))
|
||||||
|
span
|
||||||
|
end
|
||||||
|
|
||||||
|
defp extract_url(%{error: _} = meta, _conn) do
|
||||||
|
case Map.get(meta, :request_target) do
|
||||||
|
nil -> ""
|
||||||
|
{scheme, host, port, path} -> build_url(scheme, host, port, path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp extract_url(_meta, conn) do
|
||||||
|
build_url(conn.scheme, conn.host, conn.port, conn.request_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp extract_request_path(%{error: _} = meta, _conn) do
|
||||||
|
case Map.get(meta, :request_target) do
|
||||||
|
nil -> ""
|
||||||
|
{_, _, _, path} -> path || ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp extract_request_path(_meta, conn) do
|
||||||
|
conn.request_path
|
||||||
|
end
|
||||||
|
|
||||||
|
defp build_url(scheme, host, port, path), do: "#{scheme}://#{host}:#{port}#{path}"
|
||||||
|
|
||||||
|
defp user_agent(conn) do
|
||||||
|
case Plug.Conn.get_req_header(conn, "user-agent") do
|
||||||
|
[] -> ""
|
||||||
|
[head | _] -> head
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp client_ip(%{remote_ip: remote_ip} = conn) do
|
||||||
|
case Plug.Conn.get_req_header(conn, "x-forwarded-for") do
|
||||||
|
[] ->
|
||||||
|
remote_ip
|
||||||
|
|> :inet_parse.ntoa()
|
||||||
|
|> to_string()
|
||||||
|
|
||||||
|
[ip_address | _] ->
|
||||||
|
ip_address
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,88 @@
|
||||||
|
defmodule OpentelemetryBandit.MixProject do
|
||||||
|
use Mix.Project
|
||||||
|
|
||||||
|
@version "0.1.4"
|
||||||
|
|
||||||
|
def project do
|
||||||
|
[
|
||||||
|
app: :opentelemetry_bandit,
|
||||||
|
version: @version,
|
||||||
|
elixir: "~> 1.14",
|
||||||
|
aliases: aliases(),
|
||||||
|
start_permanent: Mix.env() == :prod,
|
||||||
|
description: description(),
|
||||||
|
package: package(),
|
||||||
|
elixirc_paths: elixirc_path(Mix.env()),
|
||||||
|
deps: deps(),
|
||||||
|
test_coverage: [tool: ExCoveralls],
|
||||||
|
preferred_cli_env: [
|
||||||
|
coveralls: :test,
|
||||||
|
"coveralls.detail": :test,
|
||||||
|
"coveralls.post": :test,
|
||||||
|
"coveralls.html": :test,
|
||||||
|
"coveralls.cobertura": :test
|
||||||
|
],
|
||||||
|
docs: [
|
||||||
|
main: "readme",
|
||||||
|
extras: ["README.md"]
|
||||||
|
]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
# Run "mix help compile.app" to learn about applications.
|
||||||
|
def application do
|
||||||
|
[
|
||||||
|
extra_applications: [:logger]
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
defp description do
|
||||||
|
"""
|
||||||
|
Telemetry handler that creates Opentelemetry spans from Bandit events.
|
||||||
|
"""
|
||||||
|
end
|
||||||
|
|
||||||
|
defp package do
|
||||||
|
[
|
||||||
|
files: ~w(lib .formatter.exs mix.exs LICENSE* README* CHANGELOG*),
|
||||||
|
maintainers: ["Artem Solomatin"],
|
||||||
|
licenses: ["Apache-2.0"],
|
||||||
|
|
||||||
|
links: %{
|
||||||
|
"GitHub" =>
|
||||||
|
"https://github.com/open-telemetry/opentelemetry-erlang-contrib/tree/main/instrumentation/opentelemetry_bandit",
|
||||||
|
"OpenTelemetry Erlang" => "https://github.com/open-telemetry/opentelemetry-erlang",
|
||||||
|
"OpenTelemetry Erlang Contrib" =>
|
||||||
|
"https://github.com/open-telemetry/opentelemetry-erlang-contrib",
|
||||||
|
"OpenTelemetry.io" => "https://opentelemetry.io"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
defp elixirc_path(:test), do: ["lib/", "test/support"]
|
||||||
|
defp elixirc_path(_), do: ["lib/"]
|
||||||
|
|
||||||
|
# Run "mix help deps" to learn about dependencies.
|
||||||
|
defp deps do
|
||||||
|
[
|
||||||
|
{:opentelemetry_api, "~> 1.2"},
|
||||||
|
{:opentelemetry_semantic_conventions, "~> 0.2"},
|
||||||
|
{:opentelemetry_telemetry, "~> 1.0"},
|
||||||
|
{:plug, ">= 1.15.0"},
|
||||||
|
{:telemetry, "~> 1.2"},
|
||||||
|
|
||||||
|
# dev dependencies
|
||||||
|
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
|
||||||
|
{:excoveralls, "~> 0.18", only: :test},
|
||||||
|
{:bandit, "~> 1.0", only: [:dev, :test], runtime: false},
|
||||||
|
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
|
||||||
|
{:opentelemetry, "~> 1.0", only: [:dev, :test]},
|
||||||
|
{:opentelemetry_exporter, "~> 1.0", only: [:dev, :test]},
|
||||||
|
{:req, "~> 0.3", only: [:dev, :test]}
|
||||||
|
]
|
||||||
|
end
|
||||||
|
|
||||||
|
defp aliases do
|
||||||
|
["test.coverage": ["coveralls.cobertura"]]
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,40 @@
|
||||||
|
%{
|
||||||
|
"acceptor_pool": {:hex, :acceptor_pool, "1.0.0", "43c20d2acae35f0c2bcd64f9d2bde267e459f0f3fd23dab26485bf518c281b21", [:rebar3], [], "hexpm", "0cbcd83fdc8b9ad2eee2067ef8b91a14858a5883cb7cd800e6fcd5803e158788"},
|
||||||
|
"bandit": {:hex, :bandit, "1.0.0", "2bd87bbf713d0eed0090f2fa162cd1676198122e6c2b68a201c706e354a6d5e5", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "32acf6ac030fee1f99fd9c3fcf81671911ae8637e0a61c98111861b466efafdb"},
|
||||||
|
"castore": {:hex, :castore, "1.0.4", "ff4d0fb2e6411c0479b1d965a814ea6d00e51eb2f58697446e9c41a97d940b28", [:mix], [], "hexpm", "9418c1b8144e11656f0be99943db4caf04612e3eaecefb5dae9a2a87565584f8"},
|
||||||
|
"chatterbox": {:hex, :ts_chatterbox, "0.13.0", "6f059d97bcaa758b8ea6fffe2b3b81362bd06b639d3ea2bb088335511d691ebf", [:rebar3], [{:hpack, "~> 0.2.3", [hex: :hpack_erl, repo: "hexpm", optional: false]}], "hexpm", "b93d19104d86af0b3f2566c4cba2a57d2e06d103728246ba1ac6c3c0ff010aa7"},
|
||||||
|
"ctx": {:hex, :ctx, "0.6.0", "8ff88b70e6400c4df90142e7f130625b82086077a45364a78d208ed3ed53c7fe", [:rebar3], [], "hexpm", "a14ed2d1b67723dbebbe423b28d7615eb0bdcba6ff28f2d1f1b0a7e1d4aa5fc2"},
|
||||||
|
"dialyxir": {:hex, :dialyxir, "1.4.1", "a22ed1e7bd3a3e3f197b68d806ef66acb61ee8f57b3ac85fc5d57354c5482a93", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "84b795d6d7796297cca5a3118444b80c7d94f7ce247d49886e7c291e1ae49801"},
|
||||||
|
"earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"},
|
||||||
|
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
|
||||||
|
"ex_doc": {:hex, :ex_doc, "0.31.0", "06eb1dfd787445d9cab9a45088405593dd3bb7fe99e097eaa71f37ba80c7a676", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "5350cafa6b7f77bdd107aa2199fe277acf29d739aba5aee7e865fc680c62a110"},
|
||||||
|
"excoveralls": {:hex, :excoveralls, "0.18.0", "b92497e69465dc51bc37a6422226ee690ab437e4c06877e836f1c18daeb35da9", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1109bb911f3cb583401760be49c02cbbd16aed66ea9509fc5479335d284da60b"},
|
||||||
|
"finch": {:hex, :finch, "0.16.0", "40733f02c89f94a112518071c0a91fe86069560f5dbdb39f9150042f44dcfb1a", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f660174c4d519e5fec629016054d60edd822cdfe2b7270836739ac2f97735ec5"},
|
||||||
|
"gproc": {:hex, :gproc, "0.8.0", "cea02c578589c61e5341fce149ea36ccef236cc2ecac8691fba408e7ea77ec2f", [:rebar3], [], "hexpm", "580adafa56463b75263ef5a5df4c86af321f68694e7786cb057fd805d1e2a7de"},
|
||||||
|
"grpcbox": {:hex, :grpcbox, "0.16.0", "b83f37c62d6eeca347b77f9b1ec7e9f62231690cdfeb3a31be07cd4002ba9c82", [:rebar3], [{:acceptor_pool, "~> 1.0.0", [hex: :acceptor_pool, repo: "hexpm", optional: false]}, {:chatterbox, "~> 0.13.0", [hex: :ts_chatterbox, repo: "hexpm", optional: false]}, {:ctx, "~> 0.6.0", [hex: :ctx, repo: "hexpm", optional: false]}, {:gproc, "~> 0.8.0", [hex: :gproc, repo: "hexpm", optional: false]}], "hexpm", "294df743ae20a7e030889f00644001370a4f7ce0121f3bbdaf13cf3169c62913"},
|
||||||
|
"hpack": {:hex, :hpack_erl, "0.2.3", "17670f83ff984ae6cd74b1c456edde906d27ff013740ee4d9efaa4f1bf999633", [:rebar3], [], "hexpm", "06f580167c4b8b8a6429040df36cc93bba6d571faeaec1b28816523379cbb23a"},
|
||||||
|
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
|
||||||
|
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
|
||||||
|
"makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"},
|
||||||
|
"makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"},
|
||||||
|
"makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"},
|
||||||
|
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
|
||||||
|
"mint": {:hex, :mint, "1.5.1", "8db5239e56738552d85af398798c80648db0e90f343c8469f6c6d8898944fb6f", [:mix], [{:castore, "~> 0.1.0 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "4a63e1e76a7c3956abd2c72f370a0d0aecddc3976dea5c27eccbecfa5e7d5b1e"},
|
||||||
|
"nimble_options": {:hex, :nimble_options, "1.0.2", "92098a74df0072ff37d0c12ace58574d26880e522c22801437151a159392270e", [:mix], [], "hexpm", "fd12a8db2021036ce12a309f26f564ec367373265b53e25403f0ee697380f1b8"},
|
||||||
|
"nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"},
|
||||||
|
"nimble_pool": {:hex, :nimble_pool, "1.0.0", "5eb82705d138f4dd4423f69ceb19ac667b3b492ae570c9f5c900bb3d2f50a847", [:mix], [], "hexpm", "80be3b882d2d351882256087078e1b1952a28bf98d0a287be87e4a24a710b67a"},
|
||||||
|
"opentelemetry": {:hex, :opentelemetry, "1.3.1", "f0a342a74379e3540a634e7047967733da4bc8b873ec9026e224b2bd7369b1fc", [:rebar3], [{:opentelemetry_api, "~> 1.2.2", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:opentelemetry_semantic_conventions, "~> 0.2", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}], "hexpm", "de476b2ac4faad3e3fe3d6e18b35dec9cb338c3b9910c2ce9317836dacad3483"},
|
||||||
|
"opentelemetry_api": {:hex, :opentelemetry_api, "1.2.2", "693f47b0d8c76da2095fe858204cfd6350c27fe85d00e4b763deecc9588cf27a", [:mix, :rebar3], [{:opentelemetry_semantic_conventions, "~> 0.2", [hex: :opentelemetry_semantic_conventions, repo: "hexpm", optional: false]}], "hexpm", "dc77b9a00f137a858e60a852f14007bb66eda1ffbeb6c05d5fe6c9e678b05e9d"},
|
||||||
|
"opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.6.0", "f4fbf69aa9f1541b253813221b82b48a9863bc1570d8ecc517bc510c0d1d3d8c", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.3", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.2", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.18", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "1802d1dca297e46f21e5832ecf843c451121e875f73f04db87355a6cb2ba1710"},
|
||||||
|
"opentelemetry_semantic_conventions": {:hex, :opentelemetry_semantic_conventions, "0.2.0", "b67fe459c2938fcab341cb0951c44860c62347c005ace1b50f8402576f241435", [:mix, :rebar3], [], "hexpm", "d61fa1f5639ee8668d74b527e6806e0503efc55a42db7b5f39939d84c07d6895"},
|
||||||
|
"opentelemetry_telemetry": {:hex, :opentelemetry_telemetry, "1.0.0", "d5982a319e725fcd2305b306b65c18a86afdcf7d96821473cf0649ff88877615", [:mix, :rebar3], [{:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_registry, "~> 0.3.0", [hex: :telemetry_registry, repo: "hexpm", optional: false]}], "hexpm", "3401d13a1d4b7aa941a77e6b3ec074f0ae77f83b5b2206766ce630123a9291a9"},
|
||||||
|
"plug": {:hex, :plug, "1.15.1", "b7efd81c1a1286f13efb3f769de343236bd8b7d23b4a9f40d3002fc39ad8f74c", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "459497bd94d041d98d948054ec6c0b76feacd28eec38b219ca04c0de13c79d30"},
|
||||||
|
"plug_crypto": {:hex, :plug_crypto, "2.0.0", "77515cc10af06645abbfb5e6ad7a3e9714f805ae118fa1a70205f80d2d70fe73", [:mix], [], "hexpm", "53695bae57cc4e54566d993eb01074e4d894b65a3766f1c43e2c61a1b0f45ea9"},
|
||||||
|
"req": {:hex, :req, "0.4.4", "a17b6bec956c9af4f08b5d8e8a6fc6e4edf24ccc0ac7bf363a90bba7a0f0138c", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.9", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "2618c0493444fee927d12073afb42e9154e766b3f4448e1011f0d3d551d1a011"},
|
||||||
|
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
|
||||||
|
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
|
||||||
|
"telemetry_registry": {:hex, :telemetry_registry, "0.3.1", "14a3319a7d9027bdbff7ebcacf1a438f5f5c903057b93aee484cca26f05bdcba", [:mix, :rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6d0ca77b691cf854ed074b459a93b87f4c7f5512f8f7743c635ca83da81f939e"},
|
||||||
|
"thousand_island": {:hex, :thousand_island, "1.0.0", "63fc8807d8607c9d74fa670996897c8c8a1f2022c8c68d024182e45249acd756", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "996320c72ba8f34d7be9b02900622e44341649f24359e0f67643e4dda8f23995"},
|
||||||
|
"tls_certificate_check": {:hex, :tls_certificate_check, "1.20.0", "1ac0c53f95e201feb8d398ef9d764ae74175231289d89f166ba88a7f50cd8e73", [:rebar3], [{:ssl_verify_fun, "~> 1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "ab57b74b1a63dc5775650699a3ec032ec0065005eff1f020818742b7312a8426"},
|
||||||
|
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
|
||||||
|
}
|
|
@ -0,0 +1,245 @@
|
||||||
|
defmodule OpentelemetryBanditTest do
|
||||||
|
use ExUnit.Case, async: true
|
||||||
|
|
||||||
|
require OpenTelemetry.Tracer
|
||||||
|
require OpenTelemetry.Span
|
||||||
|
require Record
|
||||||
|
|
||||||
|
use ServerHelper
|
||||||
|
|
||||||
|
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
|
||||||
|
Record.defrecord(name, spec)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "http integration" do
|
||||||
|
test "default span generation for 200" do
|
||||||
|
Req.get("http://localhost:4000/hello")
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "HTTP GET /hello",
|
||||||
|
kind: :server,
|
||||||
|
status: {:status, :ok, _},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"net.peer.name": "localhost",
|
||||||
|
"http.method": "GET",
|
||||||
|
"http.target": "/hello",
|
||||||
|
"http.scheme": :http,
|
||||||
|
"http.status_code": 200
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "default span generation for 200 without user-agent" do
|
||||||
|
{:ok, {{_, 200, _}, _, _}} =
|
||||||
|
:httpc.request(:get, {~c"http://localhost:4000/hello", []}, [], [])
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "HTTP GET /hello",
|
||||||
|
kind: :server,
|
||||||
|
status: {:status, :ok, _},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"net.peer.name": "localhost",
|
||||||
|
"http.method": "GET",
|
||||||
|
"http.target": "/hello",
|
||||||
|
"http.scheme": :http,
|
||||||
|
"http.status_code": 200,
|
||||||
|
"http.client_ip": "127.0.0.1"
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "default span generation for 200 with x-forwarded-for" do
|
||||||
|
Req.get("http://localhost:4000/hello", headers: %{x_forwarded_for: "127.0.0.1"})
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "HTTP GET /hello",
|
||||||
|
kind: :server,
|
||||||
|
status: {:status, :ok, _},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"net.peer.name": "localhost",
|
||||||
|
"http.method": "GET",
|
||||||
|
"http.target": "/hello",
|
||||||
|
"http.scheme": :http,
|
||||||
|
"http.status_code": 200,
|
||||||
|
"http.client_ip": "127.0.0.1"
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "default span generation for halted connection" do
|
||||||
|
Req.get("http://localhost:4000/fail", retry: false)
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "HTTP GET /fail",
|
||||||
|
kind: :server,
|
||||||
|
status: {:status, :ok, _},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"net.peer.name": "localhost",
|
||||||
|
"http.method": "GET",
|
||||||
|
"http.target": "/fail",
|
||||||
|
"http.scheme": :http,
|
||||||
|
"http.status_code": 500
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "default span generation for 500 response" do
|
||||||
|
:telemetry.execute(
|
||||||
|
[:bandit, :request, :stop],
|
||||||
|
%{duration: 444, resp_body_bytes: 10},
|
||||||
|
%{
|
||||||
|
conn: nil,
|
||||||
|
status: 500,
|
||||||
|
error: "Internal Server Error",
|
||||||
|
method: "GET",
|
||||||
|
request_target: {nil, nil, nil, "/not_existing_route"}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "HTTP GET /not_existing_route",
|
||||||
|
kind: :error,
|
||||||
|
status: {:status, :error, "Internal Server Error"},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"http.url": _,
|
||||||
|
"http.method": "GET",
|
||||||
|
"http.status_code": 500,
|
||||||
|
"http.response_content_length": 10,
|
||||||
|
"net.transport": :"IP.TCP"
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "span when request_target is empty" do
|
||||||
|
:telemetry.execute(
|
||||||
|
[:bandit, :request, :stop],
|
||||||
|
%{duration: 444, resp_body_bytes: 10},
|
||||||
|
%{
|
||||||
|
conn: nil,
|
||||||
|
status: 500,
|
||||||
|
error: "Internal Server Error",
|
||||||
|
method: "GET",
|
||||||
|
request_target: nil
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "HTTP GET",
|
||||||
|
kind: :error,
|
||||||
|
status: {:status, :error, "Internal Server Error"},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"http.url": _,
|
||||||
|
"http.method": "GET",
|
||||||
|
"http.status_code": 500,
|
||||||
|
"http.response_content_length": 10,
|
||||||
|
"net.transport": :"IP.TCP"
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "exception catch span" do
|
||||||
|
Req.get("http://localhost:4000/exception", retry: false)
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "HTTP exception RuntimeError",
|
||||||
|
kind: :error,
|
||||||
|
status: {:status, :error, _}
|
||||||
|
)}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "websocket integration" do
|
||||||
|
test "span when request finished successfully" do
|
||||||
|
:telemetry.execute(
|
||||||
|
[:bandit, :websocket, :stop],
|
||||||
|
%{
|
||||||
|
duration: 444,
|
||||||
|
send_binary_frame_bytes: 10,
|
||||||
|
recv_binary_frame_bytes: 15
|
||||||
|
},
|
||||||
|
%{}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "Websocket",
|
||||||
|
kind: :server,
|
||||||
|
status: {:status, :ok, _},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"net.transport": :websocket,
|
||||||
|
"websocket.recv.binary.frame.bytes": 10,
|
||||||
|
"websocket.send.binary.frame.bytes": 15
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "span when error is set" do
|
||||||
|
:telemetry.execute(
|
||||||
|
[:bandit, :websocket, :stop],
|
||||||
|
%{
|
||||||
|
duration: 444,
|
||||||
|
send_binary_frame_bytes: 10,
|
||||||
|
recv_binary_frame_bytes: 15
|
||||||
|
},
|
||||||
|
%{error: "Internal Server Error"}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_receive {:span,
|
||||||
|
span(
|
||||||
|
name: "Websocket",
|
||||||
|
kind: :error,
|
||||||
|
status: {:status, :error, _},
|
||||||
|
attributes: attributes
|
||||||
|
)}
|
||||||
|
|
||||||
|
assert %{
|
||||||
|
"net.transport": :websocket,
|
||||||
|
"websocket.recv.binary.frame.bytes": 10,
|
||||||
|
"websocket.send.binary.frame.bytes": 15
|
||||||
|
} = :otel_attributes.map(attributes)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
setup do
|
||||||
|
:otel_simple_processor.set_exporter(:otel_exporter_pid, self())
|
||||||
|
|
||||||
|
{:ok, _} = start_supervised({Bandit, plug: __MODULE__, port: 4000, startup_log: false})
|
||||||
|
|
||||||
|
OpentelemetryBandit.setup()
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
def hello(conn) do
|
||||||
|
conn |> send_resp(200, "OK")
|
||||||
|
end
|
||||||
|
|
||||||
|
def fail(conn) do
|
||||||
|
conn |> send_resp(500, "Internal Server Error") |> halt()
|
||||||
|
end
|
||||||
|
|
||||||
|
def exception(_conn) do
|
||||||
|
raise "boom"
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,18 @@
|
||||||
|
defmodule ServerHelper do
|
||||||
|
defmacro __using__(_) do
|
||||||
|
quote location: :keep do
|
||||||
|
import Plug.Conn
|
||||||
|
|
||||||
|
def init(opts) do
|
||||||
|
opts
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(conn, []) do
|
||||||
|
function = String.to_atom(List.first(conn.path_info))
|
||||||
|
apply(__MODULE__, function, [conn])
|
||||||
|
end
|
||||||
|
|
||||||
|
defoverridable init: 1, call: 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1 @@
|
||||||
|
ExUnit.start()
|
|
@ -6,7 +6,7 @@ defmodule OpentelemetryPhoenix do
|
||||||
doc: "The endpoint prefix in your endpoint."
|
doc: "The endpoint prefix in your endpoint."
|
||||||
],
|
],
|
||||||
adapter: [
|
adapter: [
|
||||||
type: {:in, [:cowboy2, nil]},
|
type: {:in, [:cowboy2, :bandit, nil]},
|
||||||
default: nil,
|
default: nil,
|
||||||
doc: "The phoenix server adapter being used.",
|
doc: "The phoenix server adapter being used.",
|
||||||
type_doc: ":atom"
|
type_doc: ":atom"
|
||||||
|
@ -32,8 +32,6 @@ defmodule OpentelemetryPhoenix do
|
||||||
span from being started and the existing cowboy span to be continued. This is the recommended
|
span from being started and the existing cowboy span to be continued. This is the recommended
|
||||||
setup for measuring accurate latencies.
|
setup for measuring accurate latencies.
|
||||||
|
|
||||||
`Bandit.PhoenixAdapter` is not currently supported.
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
In your application start:
|
In your application start:
|
||||||
|
@ -69,7 +67,7 @@ defmodule OpentelemetryPhoenix do
|
||||||
@type endpoint_prefix :: {:endpoint_prefix, [atom()]}
|
@type endpoint_prefix :: {:endpoint_prefix, [atom()]}
|
||||||
|
|
||||||
@typedoc "The phoenix server adapter being used. Optional"
|
@typedoc "The phoenix server adapter being used. Optional"
|
||||||
@type adapter :: {:adapter, :cowboy2 | term()}
|
@type adapter :: {:adapter, :cowboy2 | :bandit | term()}
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Initializes and configures the telemetry handlers.
|
Initializes and configures the telemetry handlers.
|
||||||
|
@ -162,6 +160,9 @@ defmodule OpentelemetryPhoenix do
|
||||||
:cowboy2 ->
|
:cowboy2 ->
|
||||||
cowboy2_start()
|
cowboy2_start()
|
||||||
|
|
||||||
|
:bandit ->
|
||||||
|
bandit_start()
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
default_start(meta)
|
default_start(meta)
|
||||||
end
|
end
|
||||||
|
@ -172,6 +173,11 @@ defmodule OpentelemetryPhoenix do
|
||||||
|> OpenTelemetry.Ctx.attach()
|
|> OpenTelemetry.Ctx.attach()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp bandit_start() do
|
||||||
|
OpentelemetryProcessPropagator.fetch_parent_ctx()
|
||||||
|
|> OpenTelemetry.Ctx.attach()
|
||||||
|
end
|
||||||
|
|
||||||
defp default_start(meta) do
|
defp default_start(meta) do
|
||||||
%{conn: conn} = meta
|
%{conn: conn} = meta
|
||||||
:otel_propagator_text_map.extract(conn.req_headers)
|
:otel_propagator_text_map.extract(conn.req_headers)
|
||||||
|
@ -209,6 +215,9 @@ defmodule OpentelemetryPhoenix do
|
||||||
:cowboy2 ->
|
:cowboy2 ->
|
||||||
:ok
|
:ok
|
||||||
|
|
||||||
|
:bandit ->
|
||||||
|
:ok
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
default_stop(meta)
|
default_stop(meta)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue