Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: set more logger metadata, and reset it after span closes #146

Closed
wants to merge 39 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b741d10
Make priority optional
reachfh Aug 9, 2022
8989810
Use Config instead of Mix.Config
reachfh Aug 9, 2022
230254c
Upgrade dialyxir
reachfh Aug 9, 2022
e625d6e
Upgrade dialyxir
reachfh Aug 9, 2022
49bbdfd
mix format
reachfh Aug 9, 2022
0269408
Upgrade credo and inch
reachfh Aug 9, 2022
6ae8458
Upgrade libraries
reachfh Aug 9, 2022
130ea6e
PR136: Fix interpolated string span and trace names
reachfh Aug 9, 2022
71eddca
Add functions to get and set trace priority
reachfh Aug 11, 2022
fb3db25
Update CHANGELOG
reachfh Aug 20, 2022
2c7253f
Add docs
reachfh Aug 22, 2022
30a7d11
Use new names for priority constants
reachfh Aug 22, 2022
8b36101
Allow Spandex.Tracer functions to be overrided
reachfh Aug 22, 2022
004fa12
Use bind_quoted for opts
reachfh Aug 22, 2022
a07a0fa
Fix typo
reachfh Aug 22, 2022
2a0b286
Document overriding span_error
reachfh Aug 22, 2022
605d3a0
Add GitHub Action
reachfh Aug 23, 2022
99b24f0
mix format
reachfh Aug 23, 2022
6557fd0
Disable --warnings-as-errors
reachfh Aug 23, 2022
0d1dd2c
Doc
reachfh Aug 23, 2022
9b8bd5a
Fix name
reachfh Aug 23, 2022
f17ae74
Add :file and :line to test logs
reachfh Aug 23, 2022
fa41b1c
Use Elixir 1.11 test image; Run mix format check after deps.get
reachfh Aug 23, 2022
25b08d0
Require Elixir 1.11
reachfh Aug 23, 2022
ef04a4e
Update docs
reachfh Aug 24, 2022
734eb1f
Don't send coveralls report to server
reachfh Aug 24, 2022
a2fd94e
Test older versions of Elixir
reachfh Aug 24, 2022
ab8b663
Use setup-elixir instead of setup-beam
reachfh Aug 24, 2022
a40b1cb
Use later version of setup-elixir
reachfh Aug 24, 2022
80a7ce2
Specify detailed versions of Elxir and OTP
reachfh Aug 24, 2022
7174fcb
Fix variable
reachfh Aug 24, 2022
fb29312
Try setup-beam again
reachfh Aug 24, 2022
06be739
Use correct name
reachfh Aug 24, 2022
f998617
Only test on OTP 24
reachfh Aug 25, 2022
fecd449
Add service_version option support (#122)
kamilkowalski Sep 8, 2022
3956970
Prepare for 3.2.0 release (#143)
GregMefford Oct 21, 2022
2eb5639
improvement: set more logger metadata, and reset it after span closes
zachdaniel Dec 5, 2022
8fbd0ec
improvement: configurable default priority
zachdaniel Feb 1, 2023
77d8dd8
chore: undo unnecessary change
zachdaniel Feb 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Elixir CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-elixir/ for more details
version: 2
jobs:
build:
docker:
# specify the version here
- image: circleci/elixir:1.7
- image: cimg/elixir:1.11
environment:
MIX_ENV: test

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
Expand All @@ -16,12 +14,13 @@ jobs:
working_directory: ~/spandex
steps:
- checkout

# specify any bash command here prefixed with `run: `
- run: mix format --check-formatted
- run: mix local.hex --force
- run: mix local.rebar --force
- run: mix deps.get
- run: mix format --check-formatted
- run: mix compile --warnings-as-errors
- run: mix coveralls.circle
# (ExCoveralls.ReportUploadError) Failed to upload the report to 'https://coveralls.io' (reason: status_code = 422, body = {"message":"Couldn't find a repository matching this job.","error":true}).
# - run: mix coveralls.circle
- run: mix coveralls
- run: mix inch.report
- run: mix test
65 changes: 65 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
name: CI
on: push

jobs:
elixir:
name: Elixir Tests
runs-on: ubuntu-20.04
env:
MIX_ENV: test
strategy:
matrix:
elixir: ['1.11.4', '1.13.4']
otp: ['24.3']
steps:
- name: Cancel previous runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}

- name: Checkout code
uses: actions/checkout@v2

- name: Setup Elixir
uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}

- name: Get deps cache
uses: actions/cache@v2
with:
path: deps/
key: deps-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('**/mix.lock') }}

- name: Get build cache
uses: actions/cache@v2
with:
path: _build/test/
key: build-${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles('**/mix.lock') }}

- name: Install deps
run: |
mix local.rebar --force
mix local.hex --force
mix deps.get

- name: Compile code
run: |
# Use of @deprecated causes compile warnings
# mix compile --warnings-as-errors
mix compile

- name: Run tests
run: |
mix test
mix format --check-formatted
mix coveralls
mix inch.report

# - name: Publish unit test results to GitHub
# uses: EnricoMi/publish-unit-test-result-action@v2
# if: always() # always run even if tests fail
# with:
# junit_files: _build/test/lib/*/test-junit-report.xml
30 changes: 27 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](Https://conventionalcommits.org) for commit guidelines.

<!-- changelog -->

### Breaking Changes:

* Don't set default priority to support distributed traces and sampling

### Bug Fixes:
* Apply fix for interpolated string span and trace names from PR #136

### Chores
* Use new `Config` instead of `Mix.Config` in `config` files
* Update libraries

### Features:
* Add functions to get and set trace priority
## [3.2.0](https://github.com/spandex-project/spandex/compare/3.1.0...3.2.0) (2022-10-20)

### Features:

* Add Service Version Support by @kamilkowalski in https://github.com/spandex-project/spandex/pull/122

## Bug Fixes:

* Fix interpolated string span and trace names by @gerbal in https://github.com/spandex-project/spandex/pull/136

## New Contributors:
* @gerbal made their first contribution in https://github.com/spandex-project/spandex/pull/136


## [3.1.0](https://github.com/spandex-project/spandex/compare/3.0.3...3.1.0) (2021-10-23)

* Encode logger metadata as string. by @aselder in https://github.com/spandex-project/spandex/pull/127
Expand Down
57 changes: 54 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ monitoring tool that allows you get extremely granular information about the
runtime of your system. Using distributed tracing, you can also get a view of
how requests make their way through your entire ecosystem of microservices or
applications. Currently, Spandex only supports integrating with
[datadog](https://www.datadoghq.com/), but it is built to be agnostic to what
[Datadog](https://www.datadoghq.com/), but it is built to be agnostic to what
platform you choose to view your trace data. Eventually it should support Open
Zipkin, Stackdriver, and any other trace viewer/aggregation tool you'd like to
integrate with. We are still under active development, working on moving to a
Expand All @@ -38,7 +38,7 @@ This is Datadog-specific since that's currently the only adapter.
## Adapters

* [Datadog](https://github.com/spandex-project/spandex_datadog)
* Thats it so far! If you want another adapter, it should be relatively easy to
* That's it so far! If you want another adapter, it should be relatively easy to
write! This library is in charge of handling the state management of spans,
and the adapter is just in charge of generating certain values and ultimately
sending the values to the service.
Expand All @@ -47,7 +47,7 @@ This is Datadog-specific since that's currently the only adapter.

```elixir
def deps do
[{:spandex, "~> 3.0.3"}]
[{:spandex, "~> 3.2"}]
end
```

Expand Down Expand Up @@ -282,3 +282,54 @@ Check out [spandex_ecto](https://github.com/spandex-project/spandex_ecto).
## Phoenix Tracing

Check out [spandex_phoenix](https://github.com/spandex-project/spandex_phoenix).

## Sampling and Rate Limiting

When the load or cost from tracing increases, it is useful to use rate limiting
or sampling to reduce tracing. When many traces are the same, it's enough to
trace only e.g. 10% of them, reducing the bill by 90% while still preserving
the ability to troubleshoot the system. The tracing still happens, but it
may not be sent to the monitoring service, or the service may drop it or not
retain detailed information.

Spandex stores the `priority` as an integer in the top level `Trace`.

In Datadog, there are four values:
* `MANUAL_KEEP`(2) indicates that the application wants to ensure that a trace is
sampled, e.g. if there is an error
* `AUTO_KEEP` (1) indicates that a trace has been selected for sampling
* `AUTO_REJECT` (0) indicates that the trace has not been selected for sampling
* `MANUAL_REJECT` (-1) indicates that the application wants a trace to be dropped

Similarly, OpenTracing uses 0 and 1 to indicate that a trace is sampled.

In distributed tracing, multiple processes contribute to the same trace. When
sampling, the process that starts the trace can make a decision about whether
it should be sampled. It then passes that information to downstream processes
via an HTTP header.

A trace may be sampled out, i.e. priority of 0, but the application can
override the priority manually. This is usually done for requests with errors,
as they are the ones that need troubleshooting. You can also enable tracing
dynamically with a feature flag to debug a feature in production.

Spandex has functions to read and set the priority
(`Spandex.Tracer.current_priority/1` and `Spandex.Tracer.update_priority/2`).

The following code overrides the `span_error` function on the tracer to set the
priority to `MANUAL_KEEP` when there is an error on a trace:

```elixir
defmodule Foo.Tracer do
use Spandex.Tracer, otp_app: :foo

@impl Spandex.Tracer
def span_error(error, stacktrace, opts) do
super(error, stacktrace, opts)
__MODULE__.update_priority(2)
end
```

The specific details of priority and other sampling and rate limiting are specific
to the observability back end, so look to e.g.
[spandex_datadog](https://github.com/spandex-project/spandex_datadog) for details.
13 changes: 10 additions & 3 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
# and its dependencies with the aid of the Config module.
#
# This configuration file is loaded before any dependency and
# is restricted to this project.

import_config "#{Mix.env()}.exs"
# General application configuration
import Config

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
4 changes: 1 addition & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

config :git_ops,
mix_project: Spandex.Mixfile,
Expand Down
4 changes: 2 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use Mix.Config
import Config

config :logger, :console,
level: :debug,
colors: [enabled: false],
format: "$time $metadata[$level] $message\n",
metadata: [:trace_id, :span_id]
metadata: [:trace_id, :span_id, :file, :line]

config :spandex, :decorators, tracer: Spandex.Test.Support.Tracer

Expand Down
3 changes: 3 additions & 0 deletions lib/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ defmodule Spandex.Span do
:private,
:resource,
:service,
:service_version,
:services,
:sql_query,
:start,
Expand All @@ -37,6 +38,7 @@ defmodule Spandex.Span do
private: Keyword.t(),
resource: atom() | String.t(),
service: atom(),
service_version: String.t() | nil,
services: Keyword.t() | nil,
sql_query: Keyword.t() | nil,
start: Spandex.timestamp(),
Expand All @@ -57,6 +59,7 @@ defmodule Spandex.Span do
private: :keyword,
resource: [:atom, :string],
service: :atom,
service_version: :string,
services: :keyword,
sql_query: :keyword,
start: :integer,
Expand Down
4 changes: 2 additions & 2 deletions lib/span_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ defmodule Spandex.SpanContext do
@type t :: %__MODULE__{
trace_id: Spandex.id(),
parent_id: Spandex.id(),
priority: integer(),
priority: integer() | nil,
baggage: Keyword.t()
}

defstruct trace_id: nil,
parent_id: nil,
priority: 1,
priority: nil,
baggage: []
end
Loading