parent
71e1e4ab48
commit
bfee0f71a4
|
@ -41,5 +41,8 @@ scope-ci:
|
|||
opentelemetry_cowboy:
|
||||
- instrumentation/opentelemetry_cowboy/**/*
|
||||
|
||||
opentelemetry_ecto:
|
||||
- instrumentation/opentelemetry_ecto/**/*
|
||||
|
||||
opentelemetry_phoenix:
|
||||
- instrumentation/opentelemetry_phoenix/**/*
|
||||
|
|
|
@ -21,6 +21,52 @@ jobs:
|
|||
run: |
|
||||
matrixStringifiedObject="{\"otp_version\":[\"24.0.6\",\"23.3.4.7\",\"22.3.4.21\"],\"elixir_version\":[\"1.12.3\",\"1.11.4\"],\"rebar3_version\":[\"3.16.1\"],\"os\":[\"ubuntu-18.04\"],\"include\":[{\"otp_version\":\"21.3.8.24\",\"elixir_version\":\"1.10.4\",\"rebar3_version\":\"3.15.2\"},{\"otp_version\":\"21.3.8.24\",\"elixir_version\":\"1.11.4\",\"rebar3_version\":\"3.15.2\"}],\"exclude\":[{\"otp_version\":\"21.3.8.24\",\"elixir_version\":\"1.12.3\"}]}"
|
||||
echo "::set-output name=matrix::$matrixStringifiedObject"
|
||||
opentelemetry-ecto:
|
||||
needs: [test-matrix]
|
||||
if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_ecto'))
|
||||
env:
|
||||
app: 'opentelemetry_ecto'
|
||||
defaults:
|
||||
run:
|
||||
working-directory: instrumentation/${{ env.app }}
|
||||
runs-on: ubuntu-18.04
|
||||
name: Opentelemetry Ecto test on Elixir ${{ matrix.elixir_version }} (OTP ${{ matrix.otp_version }})
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix: ${{ fromJson(needs.test-matrix.outputs.matrix) }}
|
||||
services:
|
||||
postgres:
|
||||
image: circleci/postgres:11.3-alpine-ram
|
||||
ports: ['5432:5432']
|
||||
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
|
||||
env:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_DB: opentelemetry_ecto_test
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: erlef/setup-beam@v1
|
||||
with:
|
||||
otp-version: ${{ matrix.otp_version }}
|
||||
elixir-version: ${{ matrix.elixir_version }}
|
||||
rebar3-version: ${{ matrix.rebar3_version }}
|
||||
- name: Cache
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: |
|
||||
instrumentation/${{ env.app }}/deps
|
||||
instrumentation/${{ env.app }}/_build
|
||||
key: ${{ runner.os }}-build-${{ matrix.otp_version }}-${{ matrix.elixir_version }}-v3-${{ hashFiles(format('{0}{1}', github.workspace, 'instrumentation/${{ env.app }}/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
|
||||
- name: Test
|
||||
run: mix test
|
||||
|
||||
opentelemetry-phoenix:
|
||||
needs: [test-matrix]
|
||||
if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_phoenix'))
|
||||
|
@ -45,8 +91,8 @@ jobs:
|
|||
uses: actions/cache@v2
|
||||
with:
|
||||
path: |
|
||||
${{ env.app }}/deps
|
||||
${{ env.app }}/_build
|
||||
instrumentation/${{ env.app }}/deps
|
||||
instrumentation/${{ env.app }}/_build
|
||||
key: ${{ runner.os }}-build-${{ matrix.otp_version }}-${{ matrix.elixir_version }}-v3-${{ hashFiles(format('{0}{1}', github.workspace, 'instrumentation/${{ env.app }}/mix.lock')) }}
|
||||
- name: Fetch deps
|
||||
if: steps.deps-cache.outputs.cache-hit != 'true'
|
||||
|
|
|
@ -15,4 +15,5 @@
|
|||
@open-telemetry/erlang-approvers
|
||||
|
||||
/instrumentation/opentelemetry_cowboy @bryannaegele @tsloughter
|
||||
/instrumentation/opentelemetry_phoenix @bryannaegele @tsloughter
|
||||
/instrumentation/opentelemetry_ecto @bryannaegele @tsloughter
|
||||
/instrumentation/opentelemetry_phoenix @bryannaegele @tsloughter
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
# Used by "mix format"
|
||||
[
|
||||
import_deps: [:ecto],
|
||||
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{rel,config,lib,test}/**/*.{ex,exs}"],
|
||||
subdirectories: ["priv/*/migrations"],
|
||||
line_length: 120
|
||||
]
|
|
@ -0,0 +1,11 @@
|
|||
/_build
|
||||
/cover
|
||||
/deps
|
||||
/doc
|
||||
/.fetch
|
||||
erl_crash.dump
|
||||
*.ez
|
||||
*.beam
|
||||
/config/*.secret.exs
|
||||
.elixir_ls/
|
||||
.rebar3
|
|
@ -0,0 +1,201 @@
|
|||
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
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
|
@ -0,0 +1,42 @@
|
|||
# OpentelemetryEcto
|
||||
|
||||
Telemetry handler that creates Opentelemetry spans from Ecto query events. Because
|
||||
Ecto emits telemetry events only after queries have finished, OpentelemetryEcto
|
||||
estimates the start time of the span by subtracting the reported total duration
|
||||
from the current timestamp.
|
||||
|
||||
After installing, setup the handler in your application behaviour before your
|
||||
top-level supervisor starts.
|
||||
|
||||
```elixir
|
||||
OpentelemetryEcto.setup([:blog, :repo])
|
||||
```
|
||||
|
||||
See the documentation for `OpentelemetryEcto.setup/2` for additional options that
|
||||
may be supplied.
|
||||
|
||||
## Installation
|
||||
|
||||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
||||
by adding `opentelemetry_ecto` to your list of dependencies in `mix.exs`:
|
||||
|
||||
```elixir
|
||||
def deps do
|
||||
[
|
||||
{:opentelemetry_ecto, "~> 1.0.0-rc"}
|
||||
]
|
||||
end
|
||||
```
|
||||
|
||||
## Compatibility Matrix
|
||||
|
||||
| OpentelemetryEcto Version | Otel Version | Notes |
|
||||
| :------------------------ | :----------- | :---- |
|
||||
| | | |
|
||||
| v0.1.0 | <= v.0.5.0 | |
|
||||
| v1.0.0-rc.1 | v1.0.0-rc.1 | |
|
||||
|
||||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||
be found at [https://hexdocs.pm/opentelemetry_ecto](https://hexdocs.pm/opentelemetry_ecto).
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
# This file is responsible for configuring your application
|
||||
# and its dependencies with the aid of the Mix.Config module.
|
||||
use Mix.Config
|
||||
|
||||
# This configuration is loaded before any dependency and is restricted
|
||||
# to this project. If another project depends on this project, this
|
||||
# file won't be loaded nor affect the parent project. For this reason,
|
||||
# if you want to provide default values for your application for
|
||||
# third-party users, it should be done in your "mix.exs" file.
|
||||
|
||||
# You can configure your application as:
|
||||
#
|
||||
# config :opentelemetry_ecto, key: :value
|
||||
#
|
||||
# and access this configuration in your application as:
|
||||
#
|
||||
# Application.get_env(:opentelemetry_ecto, :key)
|
||||
#
|
||||
# You can also configure a third-party app:
|
||||
#
|
||||
# config :logger, level: :info
|
||||
#
|
||||
|
||||
# It is also possible to import configuration files, relative to this
|
||||
# directory. For example, you can emulate configuration per environment
|
||||
# by uncommenting the line below and defining dev.exs, test.exs and such.
|
||||
# Configuration from the imported file will override the ones defined
|
||||
# here (which is why it is important to import them last).
|
||||
#
|
||||
try do
|
||||
import_config "#{Mix.env()}.exs"
|
||||
rescue
|
||||
_ -> :ok
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
import Config
|
||||
|
||||
config :opentelemetry_ecto,
|
||||
ecto_repos: [OpentelemetryEcto.TestRepo]
|
||||
|
||||
config :opentelemetry_ecto, OpentelemetryEcto.TestRepo,
|
||||
hostname: "localhost",
|
||||
username: "postgres",
|
||||
database: "opentelemetry_ecto_test",
|
||||
pool: Ecto.Adapters.SQL.Sandbox
|
||||
|
||||
config :opentelemetry,
|
||||
sampler: {:always_on, %{}},
|
||||
tracer: :otel_tracer_default,
|
||||
processors: [{:otel_batch_processor, %{scheduled_delay_ms: 1}}]
|
|
@ -0,0 +1,10 @@
|
|||
version: "3.7"
|
||||
|
||||
services:
|
||||
postgres:
|
||||
image: circleci/postgres:11.3-alpine-ram
|
||||
environment:
|
||||
- POSTGRES_USER=postgres
|
||||
- POSTGRES_DB=opentelemetry_ecto_test
|
||||
ports:
|
||||
- 5432:5432
|
|
@ -0,0 +1,105 @@
|
|||
defmodule OpentelemetryEcto do
|
||||
@moduledoc """
|
||||
Telemetry handler for creating OpenTelemetry Spans from Ecto query events.
|
||||
"""
|
||||
|
||||
require OpenTelemetry.Tracer
|
||||
|
||||
@doc """
|
||||
Attaches the OpentelemetryEcto handler to your repo events. This should be called
|
||||
from your application behaviour on startup.
|
||||
|
||||
Example:
|
||||
|
||||
OpentelemetryEcto.setup([:blog, :repo])
|
||||
|
||||
You may also supply the following options in the second argument:
|
||||
|
||||
* `:time_unit` - a time unit used to convert the values of query phase
|
||||
timings, defaults to `:microsecond`. See `System.convert_time_unit/3`
|
||||
|
||||
* `:span_prefix` - the first part of the span name, as a `String.t`,
|
||||
defaults to the concatenation of the event name with periods, e.g.
|
||||
`"blog.repo.query"`. This will always be followed with a colon and the
|
||||
source (the table name for SQL adapters).
|
||||
"""
|
||||
def setup(event_prefix, config \\ []) do
|
||||
event = event_prefix ++ [:query]
|
||||
:telemetry.attach({__MODULE__, event}, event, &__MODULE__.handle_event/4, config)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_event(
|
||||
event,
|
||||
measurements,
|
||||
%{query: query, source: source, result: query_result, repo: repo, type: type},
|
||||
config
|
||||
) do
|
||||
# Doing all this even if the span isn't sampled so the sampler
|
||||
# could technically use the attributes to decide if it should sample or not
|
||||
|
||||
total_time = measurements.total_time
|
||||
end_time = :opentelemetry.timestamp()
|
||||
start_time = end_time - total_time
|
||||
database = repo.config()[:database]
|
||||
|
||||
url =
|
||||
case repo.config()[:url] do
|
||||
nil ->
|
||||
# TODO: add port
|
||||
URI.to_string(%URI{scheme: "ecto", host: repo.config()[:hostname]})
|
||||
|
||||
url ->
|
||||
url
|
||||
end
|
||||
|
||||
span_name =
|
||||
case Keyword.fetch(config, :span_prefix) do
|
||||
{:ok, prefix} -> prefix
|
||||
:error -> Enum.join(event, ".")
|
||||
end <> ":#{source}"
|
||||
|
||||
time_unit = Keyword.get(config, :time_unit, :microsecond)
|
||||
|
||||
db_type =
|
||||
case type do
|
||||
:ecto_sql_query -> :sql
|
||||
_ -> type
|
||||
end
|
||||
|
||||
result =
|
||||
case query_result do
|
||||
{:ok, _} -> []
|
||||
_ -> [error: true]
|
||||
end
|
||||
|
||||
# TODO: need connection information to complete the required attributes
|
||||
# net.peer.name or net.peer.ip and net.peer.port
|
||||
base_attributes =
|
||||
Keyword.merge(result,
|
||||
"db.type": db_type,
|
||||
"db.statement": query,
|
||||
source: source,
|
||||
"db.instance": database,
|
||||
"db.url": url,
|
||||
"total_time_#{time_unit}s": System.convert_time_unit(total_time, :native, time_unit)
|
||||
)
|
||||
|
||||
attributes =
|
||||
measurements
|
||||
|> Enum.into(%{})
|
||||
|> Map.take(~w(decode_time query_time queue_time)a)
|
||||
|> Enum.reject(&is_nil(elem(&1, 1)))
|
||||
|> Enum.map(fn {k, v} ->
|
||||
{String.to_atom("#{k}_#{time_unit}s"), System.convert_time_unit(v, :native, time_unit)}
|
||||
end)
|
||||
|
||||
s =
|
||||
OpenTelemetry.Tracer.start_span(span_name, %{
|
||||
start_time: start_time,
|
||||
attributes: attributes ++ base_attributes
|
||||
})
|
||||
|
||||
OpenTelemetry.Span.end_span(s)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,59 @@
|
|||
defmodule OpentelemetryEcto.MixProject do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[
|
||||
app: :opentelemetry_ecto,
|
||||
description: description(),
|
||||
version: "1.0.0-rc.1",
|
||||
elixir: "~> 1.10",
|
||||
start_permanent: Mix.env() == :prod,
|
||||
deps: deps(),
|
||||
aliases: aliases(),
|
||||
elixirc_paths: elixirc_paths(Mix.env()),
|
||||
package: package(),
|
||||
source_url:
|
||||
"https://github.com/open-telemetry/opentelemetry-erlang-contrib/tree/main/instrumentation/opentelemetry_ecto"
|
||||
]
|
||||
end
|
||||
|
||||
defp description do
|
||||
"Trace Ecto queries with OpenTelemetry."
|
||||
end
|
||||
|
||||
defp package do
|
||||
[
|
||||
files: ~w(lib .formatter.exs mix.exs README* LICENSE* CHANGELOG*),
|
||||
licenses: ["Apache-2"],
|
||||
links: %{
|
||||
"GitHub" =>
|
||||
"https://github.com/open-telemetry/opentelemetry-erlang-contrib/tree/main/instrumentation/opentelemetry_ecto",
|
||||
"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
|
||||
|
||||
def application do
|
||||
[]
|
||||
end
|
||||
|
||||
defp elixirc_paths(:test), do: ["lib", "test/support"]
|
||||
defp elixirc_paths(_), do: ["lib"]
|
||||
|
||||
defp aliases() do
|
||||
[test: ["ecto.drop -q", "ecto.create -q", "ecto.migrate --quiet", "test"]]
|
||||
end
|
||||
|
||||
defp deps do
|
||||
[
|
||||
{:telemetry, "~> 0.4 or ~> 1.0.0"},
|
||||
{:opentelemetry_api, "<= 1.0.0-rc.2"},
|
||||
{:opentelemetry, "<= 1.0.0-rc.2", only: [:dev, :test]},
|
||||
{:ex_doc, "~> 0.25.0", only: [:dev], runtime: false},
|
||||
{:ecto_sql, ">= 3.0.0", only: [:dev, :test]},
|
||||
{:postgrex, ">= 0.15.0", only: [:dev, :test]}
|
||||
]
|
||||
end
|
||||
end
|
|
@ -0,0 +1,18 @@
|
|||
%{
|
||||
"connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"},
|
||||
"db_connection": {:hex, :db_connection, "2.4.0", "d04b1b73795dae60cead94189f1b8a51cc9e1f911c234cc23074017c43c031e5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad416c21ad9f61b3103d254a71b63696ecadb6a917b36f563921e0de00d7d7c8"},
|
||||
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
|
||||
"earmark": {:hex, :earmark, "1.4.15", "2c7f924bf495ec1f65bd144b355d0949a05a254d0ec561740308a54946a67888", [:mix], [{:earmark_parser, ">= 1.4.13", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "3b1209b85bc9f3586f370f7c363f6533788fb4e51db23aa79565875e7f9999ee"},
|
||||
"earmark_parser": {:hex, :earmark_parser, "1.4.15", "b29e8e729f4aa4a00436580dcc2c9c5c51890613457c193cc8525c388ccb2f06", [:mix], [], "hexpm", "044523d6438ea19c1b8ec877ec221b008661d3c27e3b848f4c879f500421ca5c"},
|
||||
"ecto": {:hex, :ecto, "3.7.1", "a20598862351b29f80f285b21ec5297da1181c0442687f9b8329f0445d228892", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "d36e5b39fc479e654cffd4dbe1865d9716e4a9b6311faff799b6f90ab81b8638"},
|
||||
"ecto_sql": {:hex, :ecto_sql, "3.7.0", "2fcaad4ab0c8d76a5afbef078162806adbe709c04160aca58400d5cbbe8eeac6", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.7.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a26135dfa1d99bf87a928c464cfa25bba6535a4fe761eefa56077a4febc60f70"},
|
||||
"ex_doc": {:hex, :ex_doc, "0.25.2", "4f1cae793c4d132e06674b282f1d9ea3bf409bcca027ddb2fe177c4eed6a253f", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "5b0c172e87ac27f14dfd152d52a145238ec71a95efbf29849550278c58a393d6"},
|
||||
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
|
||||
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
|
||||
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
|
||||
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
|
||||
"opentelemetry": {:hex, :opentelemetry, "1.0.0-rc.2", "d3e1fd9debfd73e00b0241cac464be7cd6ca6ac2bd38ab2ebe0c92401c76a342", [:rebar3], [{:opentelemetry_api, "~> 1.0.0-rc.2", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "2f810e2eed70a9ea0c9b6943969b59e37f96a2f9e10920045a6c7676c2ab8181"},
|
||||
"opentelemetry_api": {:hex, :opentelemetry_api, "1.0.0-rc.2", "a0ec5b242bb7ce7563b4891e77dcfa529defc9e42c19a5a702574c5ac3d0c6e7", [:mix, :rebar3], [], "hexpm", "426a969c8ee2afa8ab55b58e6e40e81c1f934c064459a1acb530f54042f9a9a3"},
|
||||
"postgrex": {:hex, :postgrex, "0.15.10", "2809dee1b1d76f7cbabe570b2a9285c2e7b41be60cf792f5f2804a54b838a067", [: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", "1560ca427542f6b213f8e281633ae1a3b31cdbcd84ebd7f50628765b8f6132be"},
|
||||
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
defmodule OpentelemetryEcto.TestRepo.Migrations.SetupTables do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:users) do
|
||||
add :email, :string
|
||||
end
|
||||
|
||||
create table(:posts) do
|
||||
add :body, :text
|
||||
add :user_id, references(:users)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,106 @@
|
|||
defmodule OpentelemetryEctoTest do
|
||||
alias OpentelemetryEcto.TestRepo, as: Repo
|
||||
alias OpentelemetryEcto.TestModels.{User, Post}
|
||||
require OpenTelemetry.Tracer
|
||||
use ExUnit.Case
|
||||
|
||||
@event_name [:opentelemetry_ecto, :test_repo]
|
||||
|
||||
require Record
|
||||
|
||||
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
|
||||
Record.defrecord(name, spec)
|
||||
end
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(OpentelemetryEcto.TestRepo)
|
||||
:otel_batch_processor.set_exporter(:otel_exporter_pid, self())
|
||||
|
||||
OpenTelemetry.Tracer.start_span("test")
|
||||
|
||||
on_exit(fn ->
|
||||
OpenTelemetry.Tracer.end_span()
|
||||
end)
|
||||
end
|
||||
|
||||
test "captures basic query events" do
|
||||
attach_handler()
|
||||
|
||||
Repo.all(User)
|
||||
|
||||
assert_receive {:span,
|
||||
span(
|
||||
name: "opentelemetry_ecto.test_repo.query:users",
|
||||
attributes: list
|
||||
)}
|
||||
|
||||
assert [
|
||||
"db.instance": "opentelemetry_ecto_test",
|
||||
"db.statement": "SELECT u0.\"id\", u0.\"email\" FROM \"users\" AS u0",
|
||||
"db.type": :sql,
|
||||
"db.url": "ecto://localhost",
|
||||
decode_time_microseconds: _,
|
||||
query_time_microseconds: _,
|
||||
queue_time_microseconds: _,
|
||||
source: "users",
|
||||
total_time_microseconds: _
|
||||
] = List.keysort(list, 0)
|
||||
end
|
||||
|
||||
test "changes the time unit" do
|
||||
attach_handler(time_unit: :millisecond)
|
||||
|
||||
Repo.all(Post)
|
||||
|
||||
assert_receive {:span,
|
||||
span(
|
||||
name: "opentelemetry_ecto.test_repo.query:posts",
|
||||
attributes: list
|
||||
)}
|
||||
|
||||
assert [
|
||||
"db.instance": "opentelemetry_ecto_test",
|
||||
"db.statement": "SELECT p0.\"id\", p0.\"body\", p0.\"user_id\" FROM \"posts\" AS p0",
|
||||
"db.type": :sql,
|
||||
"db.url": "ecto://localhost",
|
||||
decode_time_milliseconds: _,
|
||||
query_time_milliseconds: _,
|
||||
queue_time_milliseconds: _,
|
||||
source: "posts",
|
||||
total_time_milliseconds: _
|
||||
] = List.keysort(list, 0)
|
||||
end
|
||||
|
||||
test "changes the span name prefix" do
|
||||
attach_handler(span_prefix: "Ecto")
|
||||
|
||||
Repo.all(User)
|
||||
|
||||
assert_receive {:span, span(name: "Ecto:users")}
|
||||
end
|
||||
|
||||
test "collects multiple spans" do
|
||||
user = Repo.insert!(%User{email: "opentelemetry@erlang.org"})
|
||||
Repo.insert!(%Post{body: "We got traced!", user: user})
|
||||
|
||||
attach_handler()
|
||||
|
||||
User
|
||||
|> Repo.all()
|
||||
|> Repo.preload([:posts])
|
||||
|
||||
assert_receive {:span, span(name: "opentelemetry_ecto.test_repo.query:users")}
|
||||
assert_receive {:span, span(name: "opentelemetry_ecto.test_repo.query:posts")}
|
||||
end
|
||||
|
||||
def attach_handler(config \\ []) do
|
||||
# For now setup the handler manually in each test
|
||||
handler = {__MODULE__, self()}
|
||||
|
||||
:telemetry.attach(handler, @event_name ++ [:query], &OpentelemetryEcto.handle_event/4, config)
|
||||
|
||||
on_exit(fn ->
|
||||
:telemetry.detach(handler)
|
||||
end)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
defmodule OpentelemetryEcto.TestModels.Post do
|
||||
use Ecto.Schema
|
||||
|
||||
schema "posts" do
|
||||
field(:body, :string)
|
||||
belongs_to(:user, OpentelemetryEcto.TestModels.User)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,9 @@
|
|||
defmodule OpentelemetryEcto.TestModels.User do
|
||||
use Ecto.Schema
|
||||
|
||||
schema "users" do
|
||||
field(:email, :string)
|
||||
|
||||
has_many(:posts, OpentelemetryEcto.TestModels.Post)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,6 @@
|
|||
defmodule OpentelemetryEcto.TestRepo do
|
||||
use Ecto.Repo,
|
||||
otp_app: :opentelemetry_ecto,
|
||||
adapter: Ecto.Adapters.Postgres,
|
||||
telemetry_prefix: [:opentelemetry_ecto, :test_repo]
|
||||
end
|
|
@ -0,0 +1,14 @@
|
|||
:application.load(:opentelemetry)
|
||||
:application.set_env(:opentelemetry, :tracer, :otel_tracer_default)
|
||||
|
||||
:application.set_env(:opentelemetry, :processors, [
|
||||
{:otel_batch_processor, %{scheduled_delay_ms: 1}}
|
||||
])
|
||||
|
||||
Application.ensure_all_started(:opentelemetry)
|
||||
Application.ensure_all_started(:telemetry)
|
||||
OpentelemetryEcto.TestRepo.start_link()
|
||||
|
||||
ExUnit.start()
|
||||
|
||||
Ecto.Adapters.SQL.Sandbox.mode(OpentelemetryEcto.TestRepo, :manual)
|
Loading…
Reference in New Issue