Add OpenTelemetry integration to Redix (#29)

Initial approach follows Ecto instrumentation, recording spans for all
Redix `[:redix, :pipeline, :stop]` events.

The command sanitization is inspired-by and adapted from [Java
instrumentation][1], from where I've also copied the actual commands and
what configuration should they follow.

Network attributes are tracked via a "sidecar" process, which keeps
track of connection attributes also via `telemetry`. This extra bit of
bookkeeping is needed as command events doesn't include that piece of
information, unfortunately.

[1]: b2bc41453b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/db/RedisCommandSanitizer.java
This commit is contained in:
Andrew Rosa 2022-01-04 18:39:19 -03:00 committed by GitHub
parent 50775c881e
commit 50ed370444
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 994 additions and 0 deletions

3
.github/labeler.yml vendored
View File

@ -56,6 +56,9 @@ opentelemetry_ecto:
opentelemetry_phoenix: opentelemetry_phoenix:
- instrumentation/opentelemetry_phoenix/**/* - instrumentation/opentelemetry_phoenix/**/*
opentelemetry_redix:
- instrumentation/opentelemetry_redix/**/*
opentelemetry_telemetry: opentelemetry_telemetry:
- utilities/opentelemetry_telemetry/**/* - utilities/opentelemetry_telemetry/**/*

View File

@ -105,6 +105,47 @@ jobs:
- name: Test - name: Test
run: mix test run: mix test
opentelemetry-redix:
needs: [test-matrix]
if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_redix'))
env:
app: 'opentelemetry_redix'
defaults:
run:
working-directory: instrumentation/${{ env.app }}
runs-on: ubuntu-18.04
name: Opentelemetry Redix test on Elixir ${{ matrix.elixir_version }} (OTP ${{ matrix.otp_version }})
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.test-matrix.outputs.matrix) }}
services:
redis:
image: redis:alpine
ports: ['6379:6379']
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-telemetry: opentelemetry-telemetry:
needs: [test-matrix] needs: [test-matrix]
if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_telemetry')) if: (contains(github.event.pull_request.labels.*.name, 'elixir') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_telemetry'))

View File

@ -18,4 +18,5 @@
/instrumentation/opentelemetry_ecto @bryannaegele @tsloughter /instrumentation/opentelemetry_ecto @bryannaegele @tsloughter
/instrumentation/opentelemetry_oban @indrekj /instrumentation/opentelemetry_oban @indrekj
/instrumentation/opentelemetry_phoenix @bryannaegele @tsloughter /instrumentation/opentelemetry_phoenix @bryannaegele @tsloughter
/instrumentation/opentelemetry_redix @andrewhr
/utilities/opentelemetry_telemetry @bryannaegele @tsloughter /utilities/opentelemetry_telemetry @bryannaegele @tsloughter

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

View File

@ -0,0 +1,23 @@
# 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").
opentelemetry_redix-*.tar

View File

@ -0,0 +1,5 @@
# Changelog
## 0.1.0
* Initial release

View File

@ -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.

View File

@ -0,0 +1,54 @@
# OpentelemetryRedix
OpentelemetryRedix uses [telemetry](https://hexdocs.pm/telemetry/) handlers to
create `OpenTelemetry` spans from Redix command events.
Supported events include command stop. Connection and disconnection events
are also observed to track Redis instance address.
## Note on Redix integration
A sidecar process runs under `opentelemetry_redix` application to track
Redix connection information to inside command spans. As a requirement, all
Redis connections should start after this application.
For connections started by your application, all works as expected. But some
libraries manage internally, and for those cases, you need to ensure proper
order via `extra_applications`.
One such example is [hammer_backend_redis](https://hex.pm/packages/hammer_backend_redis).
In case you depend on that library, `extra_applications` will be similar
to the following:
```elixir
def application do
[
extra_applications: [:opentelemetry_redix, :hammer_backend_redis]
]
end
```
## Installation
The package can be installed by adding `opentelemetry_redix` to your list of
dependencies in `mix.exs`:
```elixir
def deps do
[
{:opentelemetry_redix, "~> 0.1"}
]
end
```
## Compatibility Matrix
| OpentelemetryRedix Version | Otel Version | Notes |
| :------------------------- | :----------- | :---- |
| | | |
| v0.1.0 | v1.0.0 | |
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_redix](https://hexdocs.pm/opentelemetry_redix).

View File

@ -0,0 +1,34 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
import 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_redix, key: :value
#
# and access this configuration in your application as:
#
# Application.get_env(:opentelemetry_redix, :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

View File

@ -0,0 +1,4 @@
import Config
config :opentelemetry,
processors: [{:otel_simple_processor, %{}}]

View File

@ -0,0 +1,7 @@
version: "3.7"
services:
redis:
image: redis:alpine
ports:
- 6379:6379

View File

@ -0,0 +1,92 @@
defmodule OpentelemetryRedix do
@moduledoc """
OpentelemetryRedix uses [telemetry](https://hexdocs.pm/telemetry/) handlers to
create `OpenTelemetry` spans.
## Usage
In your application start:
def start(_type, _args) do
OpentelemetryRedix.setup()
# ...
end
"""
alias OpentelemetryRedix.Command
alias OpentelemetryRedix.ConnectionTracker
require OpenTelemetry.Tracer
@typedoc "Setup options"
@type opts :: []
@doc """
Initializes and configures the telemetry handlers.
"""
@spec setup(opts()) :: :ok
def setup(_opts \\ []) do
:telemetry.attach(
{__MODULE__, :pipeline_stop},
[:redix, :pipeline, :stop],
&__MODULE__.handle_pipeline_stop/4,
:no_config
)
end
@doc false
def handle_pipeline_stop(_event, measurements, meta, _config) do
duration = measurements.duration
end_time = :opentelemetry.timestamp()
start_time = end_time - duration
operation =
case meta.commands do
[[operation | _args]] -> operation
_pipeline -> "pipeline"
end
statement = Enum.map_join(meta.commands, "\n", &Command.sanitize/1)
connection = ConnectionTracker.get_connection(meta.connection)
attributes =
%{
"db.system": "redis",
"db.operation": operation,
"db.statement": statement
}
|> Map.merge(net_attributes(connection))
|> Map.merge(redix_attributes(meta))
|> Map.merge(error_attributes(meta))
s =
OpenTelemetry.Tracer.start_span(operation, %{
start_time: start_time,
kind: :client,
attributes: attributes
})
if meta[:reason] do
OpenTelemetry.Span.set_status(s, OpenTelemetry.status(:error, ""))
end
OpenTelemetry.Span.end_span(s)
end
defp net_attributes(%{address: address}) when is_binary(address) do
[host, port] = address |> String.split(":")
%{"net.peer.name": host, "net.peer.port": port}
end
defp net_attributes(_), do: %{}
defp redix_attributes(%{connection_name: nil}), do: %{}
defp redix_attributes(%{connection_name: name}), do: %{"db.redix.connection_name": name}
defp redix_attributes(_), do: %{}
defp error_attributes(%{reason: reason}), do: %{"db.redix.error": inspect(reason)}
defp error_attributes(_), do: %{}
end

View File

@ -0,0 +1,15 @@
defmodule OpentelemetryRedix.Application do
@moduledoc false
use Application
alias OpentelemetryRedix.ConnectionTracker
def start(_type, _args) do
children = [
{ConnectionTracker, []}
]
opts = [strategy: :one_for_one, name: OpentelemetryRedix.Supervisor]
Supervisor.start_link(children, opts)
end
end

View File

@ -0,0 +1,255 @@
defmodule OpentelemetryRedix.Command do
@moduledoc false
@doc """
Masks potentially sensitive information in Redis commands.
"""
def sanitize([name | args] = command) do
case strategy(name) do
:all -> Enum.join(command, " ")
{:keep, n} -> keep_args(name, args, n)
{:keyval, n} -> keep_args_keyval(name, args, n)
end
end
defp keep_args(cmd, [], _), do: cmd
defp keep_args(cmd, [_ | xs], 0), do: keep_args(cmd <> " ?", xs, 0)
defp keep_args(cmd, [x | xs], n), do: keep_args(cmd <> " #{x}", xs, n - 1)
defp keep_args_keyval(cmd, [], _), do: cmd
defp keep_args_keyval(cmd, [_], 0), do: cmd
defp keep_args_keyval(cmd, [k, _ | kvs], 0), do: keep_args_keyval(cmd <> " #{k} ?", kvs, 0)
defp keep_args_keyval(cmd, [x | xs], n), do: keep_args_keyval(cmd <> " #{x}", xs, n - 1)
# Cluster
defp strategy(cmd) when cmd in ~w(CLUSTER READONLY READWRITE), do: :all
# Connection
defp strategy(cmd) when cmd in ~w(AUTH), do: {:keep, 0}
defp strategy(cmd) when cmd in ~w(HELLO), do: {:keep, 2}
defp strategy(cmd) when cmd in ~w(CLIENT ECHO PING QUIT SELECT), do: :all
# Geo
defp strategy(cmd) when cmd in ~w(GEOADD GEODIST GEOHASH GEOPOS GEORADIUS GEORADIUSBYMEMBER),
do: :all
# Hashes
defp strategy(cmd) when cmd in ~w(HMSET HSET), do: {:keyval, 1}
defp strategy(cmd) when cmd in ~w(HSETNX), do: {:keep, 2}
defp strategy(cmd) when cmd in ~w(
DEL
HEXISTS
HGET
HGETALL
HINCRBY
HINCRBYFLOAT
HKEYS
HLEN
HMGET
HSCAN
HSTRLEN
HVAL
),
do: :all
# HyperLogLog
defp strategy(cmd) when cmd in ~w(PFADD), do: {:keep, 1}
defp strategy(cmd) when cmd in ~w(PFCOUNT PFMERGE), do: :all
# Keys
# MIGRATE can contain AUTH data
defp strategy(cmd) when cmd in ~w(MIGRATE), do: {:keep, 6}
defp strategy(cmd) when cmd in ~w(RESTORE), do: {:keep, 2}
defp strategy(cmd) when cmd in ~w(
DEL
DUMP
EXISTS
EXPIRE
EXPIREAT
KEYS
MOVE
OBJECT
PERSIST
PEXPIRE
PEXPIREAT
PTTL
RANDOMKEY
RENAME
RENAMENX
SCAN
SORT
TOUCH
TTL
TYPE
UNLINK
WAIT
),
do: :all
# Lists
defp strategy(cmd) when cmd in ~w(LINSERT), do: {:keep, 2}
defp strategy(cmd) when cmd in ~w(LPOS LPUSH LPUSHX LREM LSET RPUSH RPUSHX), do: {:keep, 1}
defp strategy(cmd) when cmd in ~w(
BLMOVE
BLPOP
BRPOP
BRPOPLPUSH
LINDEX
LLEN
LMOVE
LPOP
LRANGE
LTRIM
RPOP
RPOPLPUSH
),
do: :all
# Pub/Sub
defp strategy(cmd) when cmd in ~w(PUBLISH), do: {:keep, 1}
defp strategy(cmd) when cmd in ~w(PSUBSCRIBE PUBSUB PUNSUBSCRIBE SUBSCRIBE UNSUBSCRIBE),
do: :all
# Server
# CONFIG SET can set any property, including the master password
defp strategy(cmd) when cmd in ~w(CONFIG), do: {:keep, 2}
defp strategy(cmd) when cmd in ~w(
ACL
BGREWRITEAOF
BGSAVE
COMMAND
DBSIZE
DEBUG
FLUSHALL
FLUSHDB
INFO
LASTSAVE
LATENCY
LOLWUT
MEMORY
MODULE
MONITOR
PSYNC
REPLICAOF
ROLE
SAVE
SHUTDOWN
SLAVEOF
SLOWLOG
SWAPDB
SYNC
TIME
),
do: :all
# Sets
defp strategy(cmd) when cmd in ~w(SADD SISMEMBER SMISMEMBER SREM), do: {:keep, 1}
defp strategy(cmd) when cmd in ~w(SMOVE), do: {:keep, 1}
defp strategy(cmd) when cmd in ~w(
SCARD
SDIFF
SDIFFSTORE
SINTER
SINTERSTORE
SMEMBERS
SPOP
SRANDMEMBER
SSCAN
SUNION
SUNIONSTORE
),
do: :all
# Sorted Sets
defp strategy(cmd) when cmd in ~w(
ZADD
ZCOUNT
ZINCRBY
ZLEXCOUNT
ZMSCORE
ZRANGEBYLEX
ZRANGEBYSCORE
ZRANK
ZREM
ZREMRANGEBYLEX
ZREMRANGEBYSCORE
ZREVRANGEBYLEX
ZREVRANGEBYSCORE
ZREVRANK
ZSCORE
),
do: {:keep, 1}
defp strategy(cmd) when cmd in ~w(
BZPOPMAX
BZPOPMIN
ZCARD
ZINTER
ZINTERSTORE
ZPOPMAX
ZPOPMIN
ZRANGE
ZREMRANGEBYRANK
ZREVRANGE
ZSCAN
ZUNION
ZUNIONSTORE
),
do: :all
# Streams
defp strategy(cmd) when cmd in ~w(XADD), do: {:keyval, 2}
defp strategy(cmd) when cmd in ~w(
XACK
XCLAIM
XDEL
XGROUP
XINFO
XLEN
XPENDING
XRANGE
XREAD
XREADGROUP
XREVRANGE
XTRIM
),
do: :all
# Strings
defp strategy(cmd) when cmd in ~w(APPEND GETSET SET SETNX SETRANGE), do: {:keep, 1}
defp strategy(cmd) when cmd in ~w(PSETEX SETEX), do: {:keep, 2}
defp strategy(cmd) when cmd in ~w(MSET MSETNX), do: {:keyval, 0}
defp strategy(cmd) when cmd in ~w(
BITCOUNT
BITFIELD
BITOP
BITPOS
DECR
DECRBY
GET
GETBIT
GETRANGE
INCR
INCRBY
INCRBYFLOAT
MGET
SETBIT
STRALGO
STRLEN
),
do: :all
# Transactions
defp strategy(cmd) when cmd in ~w(DISCARD EXEC MULTI UNWATCH WATCH), do: :all
# Default
defp strategy(_cmd), do: {:keep, 0}
end

View File

@ -0,0 +1,63 @@
defmodule OpentelemetryRedix.ConnectionTracker do
@moduledoc false
use GenServer
@conn_table __MODULE__.ETS
def start_link(opts \\ []) do
name = Keyword.get(opts, :name, __MODULE__)
GenServer.start_link(__MODULE__, opts, name: name)
end
def get_connection(tab \\ @conn_table, pid) do
case :ets.lookup(tab, pid) do
[{_pid, metadata}] ->
metadata
[] ->
nil
end
end
@impl true
def init(opts) do
config = %{
tab: Keyword.get(opts, :ets_table, @conn_table)
}
:ets.new(config.tab, [:named_table, :public, read_concurrency: true])
:telemetry.attach(
{__MODULE__, :connection},
[:redix, :connection],
&__MODULE__.handle_connection/4,
config
)
:telemetry.attach(
{__MODULE__, :disconnection},
[:redix, :disconnection],
&__MODULE__.handle_disconnection/4,
config
)
Process.flag(:trap_exit, true)
{:ok, config}
end
@impl true
def terminate(_reason, _state) do
:telemetry.detach({__MODULE__, :connection})
:telemetry.detach({__MODULE__, :disconnection})
end
def handle_connection(_event, _measurements, meta, config) do
connection_attrs = Map.take(meta, [:address, :connection_name, :reconnection])
:ets.insert_new(config.tab, {meta.connection, connection_attrs})
end
def handle_disconnection(_event, _measurements, meta, config) do
:ets.delete(config.tab, meta.connection)
end
end

View File

@ -0,0 +1,58 @@
defmodule OpentelemetryRedix.MixProject do
use Mix.Project
def project do
[
app: :opentelemetry_redix,
description: description(),
version: "0.1.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps(),
elixirc_paths: elixirc_paths(Mix.env()),
package: package(),
source_url:
"https://github.com/open-telemetry/opentelemetry-erlang-contrib/tree/main/instrumentation/opentelemetry_redix"
]
end
defp description do
"Trace Redix 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_redix",
"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
[
mod: {OpentelemetryRedix.Application, []},
extra_applications: [:logger]
]
end
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
defp deps do
[
{:ex_doc, "~> 0.25.0", only: [:dev], runtime: false},
{:opentelemetry, "~> 1.0.0", only: [:test]},
{:opentelemetry_api, "~> 1.0.0"},
{:opentelemetry_exporter, "~> 1.0.0", only: [:test]},
{:redix, "~> 1.0", only: [:dev, :test]},
{:telemetry, "~> 0.4 or ~> 1.0.0"}
]
end
end

View File

@ -0,0 +1,28 @@
%{
"acceptor_pool": {:hex, :acceptor_pool, "1.0.0", "43c20d2acae35f0c2bcd64f9d2bde267e459f0f3fd23dab26485bf518c281b21", [:rebar3], [], "hexpm", "0cbcd83fdc8b9ad2eee2067ef8b91a14858a5883cb7cd800e6fcd5803e158788"},
"chatterbox": {:hex, :ts_chatterbox, "0.11.0", "b8f372c706023eb0de5bf2976764edb27c70fe67052c88c1f6a66b3a5626847f", [:rebar3], [{:hpack, "~>0.2.3", [hex: :hpack_erl, repo: "hexpm", optional: false]}], "hexpm", "722fe2bad52913ab7e87d849fc6370375f0c961ffb2f0b5e6d647c9170c382a6"},
"connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"},
"ctx": {:hex, :ctx, "0.6.0", "8ff88b70e6400c4df90142e7f130625b82086077a45364a78d208ed3ed53c7fe", [:rebar3], [], "hexpm", "a14ed2d1b67723dbebbe423b28d7615eb0bdcba6ff28f2d1f1b0a7e1d4aa5fc2"},
"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.18", "e1b2be73eb08a49fb032a0208bf647380682374a725dfb5b9e510def8397f6f2", [:mix], [], "hexpm", "114a0e85ec3cf9e04b811009e73c206394ffecfcc313e0b346de0d557774ee97"},
"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.1", "8de624ef50b2a8540252d8c60506379fbbc2707be1606853df371cf53df5d053", [: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 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2b42a32e2ce92f64aba5c88617891ab3b0ba34f3f3a503fa20009eae1a401c81"},
"ex_doc": {:hex, :ex_doc, "0.25.5", "ac3c5425a80b4b7c4dfecdf51fa9c23a44877124dd8ca34ee45ff608b1c6deb9", [: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", "688cfa538cdc146bc4291607764a7f1fcfa4cce8009ecd62de03b27197528350"},
"gproc": {:hex, :gproc, "0.8.0", "cea02c578589c61e5341fce149ea36ccef236cc2ecac8691fba408e7ea77ec2f", [:rebar3], [], "hexpm", "580adafa56463b75263ef5a5df4c86af321f68694e7786cb057fd805d1e2a7de"},
"grpcbox": {:hex, :grpcbox, "0.14.0", "3eb321bcd2275baf8b54cf381feb7b0559a50c02544de28fda039c7f2f9d1a7a", [:rebar3], [{:acceptor_pool, "~>1.0.0", [hex: :acceptor_pool, repo: "hexpm", optional: false]}, {:chatterbox, "~>0.11.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", "e24159b7b6d3f9869bbe528845c0125fed2259366ba908fd04a1f45fe81d0660"},
"hpack": {:hex, :hpack_erl, "0.2.3", "17670f83ff984ae6cd74b1c456edde906d27ff013740ee4d9efaa4f1bf999633", [:rebar3], [], "hexpm", "06f580167c4b8b8a6429040df36cc93bba6d571faeaec1b28816523379cbb23a"},
"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.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"},
"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.2.0", "b44d75e2a6542dcb6acf5d71c32c74ca88960421b6874777f79153bbbbd7dccc", [:mix], [], "hexpm", "52b2871a7515a5ac49b00f214e4165a40724cf99798d8e4a65e4fd64ebd002c1"},
"opentelemetry": {:hex, :opentelemetry, "1.0.0", "6e98f4a9230681b2e4c88d45783ce1c02d671ffc0b5ac0cba69a34a3f5ada8d8", [:rebar3], [{:opentelemetry_api, "~> 1.0.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}], "hexpm", "08d8697740f70594d05067cb62a0a8845ff568b2d47e1f8c78c46708ab58a74f"},
"opentelemetry_api": {:hex, :opentelemetry_api, "1.0.0", "6e501f750ead189f35aed07eb8023fa6655fca12f913a196102f67db4ed5172c", [:mix, :rebar3], [], "hexpm", "ac51520bde21fdea7f82cea9236ce4e88a21281c22bc23b0f1fa3b28b4352fcf"},
"opentelemetry_exporter": {:hex, :opentelemetry_exporter, "1.0.0", "12fd928e72dec8108d40214a667e7a90026827d6268db678617c9e40ec3dc931", [:rebar3], [{:grpcbox, ">= 0.0.0", [hex: :grpcbox, repo: "hexpm", optional: false]}, {:opentelemetry, "~> 1.0", [hex: :opentelemetry, repo: "hexpm", optional: false]}, {:opentelemetry_api, "~> 1.0", [hex: :opentelemetry_api, repo: "hexpm", optional: false]}, {:tls_certificate_check, "~> 1.11", [hex: :tls_certificate_check, repo: "hexpm", optional: false]}], "hexpm", "e2d2377abfb823cc99c1b68b4ce31df1ff1ce63e0c7bdbee7a7527e2d825168d"},
"postgrex": {:hex, :postgrex, "0.15.11", "50abbb50f33d22d79af402e549b9a566ba4f0451b4f5fd39b72d9bbd49743d24", [: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", "6f0e5c3ea10f97468f5ff852277cb207f068399eb68b0c06c142ef68a4e82952"},
"redix": {:hex, :redix, "1.1.5", "6fc460d66a5c2287e83e6d73dddc8d527ff59cb4d4f298b41e03a4db8c3b2bd5", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "679afdd4c14502fe9c11387ff1cdcb33065a1cf511097da1eee407f17c7a418b"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"},
"tls_certificate_check": {:hex, :tls_certificate_check, "1.11.0", "609dcd503f31170f0799dac380eb0e086388cf918fc769aaa60ddd6bbf575218", [:rebar3], [{:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "4ab962212ef7c87482619cb298e1fe06e047b57f0bd58cc417b3b299eb8d036e"},
}

View File

@ -0,0 +1,26 @@
defmodule OpentelemetryRedix.CommandTest do
use ExUnit.Case, async: true
alias OpentelemetryRedix.Command
test "sanitize commands that keep all args" do
assert Command.sanitize(~w(GET mykey)) == "GET mykey"
end
test "sanitize commands that keep no args" do
assert Command.sanitize(~w(AUTH password)) == "AUTH ?"
assert Command.sanitize(~w(AUTH username password)) == "AUTH ? ?"
end
test "sanitize commands that keep first few args" do
assert Command.sanitize(~w(SET mykey "value")) == "SET mykey ?"
end
test "sanitize commands with key-value args" do
cmd = Command.sanitize(~w(MSET key1 "Hello" key2 "World"))
assert cmd == "MSET key1 ? key2 ?"
cmd = Command.sanitize(~w(HSET myhash field1 "Hello"))
assert cmd == "HSET myhash field1 ?"
end
end

View File

@ -0,0 +1,79 @@
defmodule OpentelemetryRedixTest do
use ExUnit.Case, async: false
doctest OpentelemetryRedix
require OpenTelemetry.Tracer
require OpenTelemetry.Span
require Record
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
Record.defrecord(name, spec)
end
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry_api/include/opentelemetry.hrl") do
Record.defrecord(name, spec)
end
setup do
:otel_simple_processor.set_exporter(:otel_exporter_pid, self())
OpenTelemetry.Tracer.start_span("test")
on_exit(fn ->
OpenTelemetry.Tracer.end_span()
end)
end
test "records span on commands" do
OpentelemetryRedix.setup()
conn = start_supervised!({Redix, []})
{:ok, "OK"} = Redix.command(conn, ["SET", "foo", "bar"])
assert_receive {:span,
span(
name: "SET",
kind: :client,
attributes: attributes
)}
assert %{
"db.operation": "SET",
"db.statement": "SET foo ?",
"db.system": "redis",
"net.peer.name": "localhost",
"net.peer.port": "6379"
} = :otel_attributes.map(attributes)
end
test "records span on piplines" do
OpentelemetryRedix.setup()
conn = start_supervised!({Redix, []})
{:ok, [_, _, _, "2"]} =
Redix.pipeline(conn, [
["DEL", "counter"],
["INCR", "counter"],
["INCR", "counter"],
["GET", "counter"]
])
assert_receive {:span,
span(
name: "pipeline",
kind: :client,
attributes: attributes
)}
assert %{
"db.operation": "pipeline",
"db.statement": "DEL counter\nINCR counter\nINCR counter\nGET counter",
"db.system": "redis",
"net.peer.name": "localhost",
"net.peer.port": "6379"
} = :otel_attributes.map(attributes)
end
end

View File

@ -0,0 +1 @@
ExUnit.start()