Implemented AWS X-Ray TraceId Generator (#95)

This commit is contained in:
Ygor Castor 2022-07-21 19:40:47 +02:00 committed by GitHub
parent 6591b28166
commit 59664d87c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 174 additions and 0 deletions

3
.github/labeler.yml vendored
View File

@ -67,3 +67,6 @@ opentelemetry_redix:
opentelemetry_telemetry: opentelemetry_telemetry:
- utilities/opentelemetry_telemetry/**/* - utilities/opentelemetry_telemetry/**/*
opentelemetry_xray:
- utilities/opentelemetry_xray/**/*

View File

@ -82,3 +82,34 @@ jobs:
run: rebar3 get-deps run: rebar3 get-deps
- name: Test - name: Test
run: rebar3 ct run: rebar3 ct
opentelemetry-aws-xray:
needs: [test-matrix]
if: (contains(github.event.pull_request.labels.*.name, 'erlang') && contains(github.event.pull_request.labels.*.name, 'opentelemetry_aws_xray'))
env:
app: 'opentelemetry_aws_xray'
defaults:
run:
working-directory: utilities/${{ env.app }}
runs-on: ubuntu-18.04
name: Opentelemetry AWS X-Ray test on OTP ${{ matrix.otp_version }} with Rebar3 ${{ matrix.rebar3_version }}
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.test-matrix.outputs.matrix) }}
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp_version }}
rebar3-version: ${{ matrix.rebar3_version }}
- name: Cache
uses: actions/cache@v2
with:
path: |
~/_build
key: ${{ runner.os }}-build-${{ matrix.otp_version }}-${{ matrix.rebar3_version }}-v3-${{ hashFiles('**/rebar.lock') }}
- name: Fetch deps
if: steps.deps-cache.outputs.cache-hit != 'true'
run: rebar3 get-deps
- name: Test
run: rebar3 ct

1
rebar.lock Normal file
View File

@ -0,0 +1 @@
[].

View File

@ -0,0 +1,36 @@
# OpentelemetryXray
Implements an Id Generator compatible with [AWS X-Ray TraceId](https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids)
## Installation
The package can be installed by adding `opentelemetry_xray` to your list of
dependencies in `mix.exs` for elixir and `rebar.config` for erlang :
```erlang
{deps, [opentelemetry_xray]}.
```
```elixir
def deps do
[
{:opentelemetry_xray, "~> 0.1"}
]
end
```
## Usage
Configure the OpenTelemetry to use the X-Ray Id Generator
```elixir
config :opentelemetry,
id_generator: :otel_aws_xray_id_generator
```
```erlang
[
{opentelemetry,
[{id_generator, otel_aws_xray_id_generator}]}
].
```

View File

@ -0,0 +1,15 @@
{erl_opts, [debug_info]}.
{deps, [{opentelemetry, "~> 1.0"}]}.
{project_plugins, [{rebar_covertool, "1.1.0"}]}.
{profiles, [{test, [{erl_opts, [nowarn_export_all]},
{deps, [{opentelemetry, "~> 1.0"}]}]}]}.
{cover_enabled, true}.
{cover_opts, [verbose]}.
{cover_export_enabled, true}.
{covertool, [{coverdata_files, ["ct.coverdata"]}]}.
{ct_opts, [{ct_hooks, [cth_surefire]}]}.

View File

@ -0,0 +1,11 @@
{"1.2.0",
[{<<"opentelemetry">>,{pkg,<<"opentelemetry">>,<<"1.0.5">>},0},
{<<"opentelemetry_api">>,{pkg,<<"opentelemetry_api">>,<<"1.0.3">>},1}]}.
[
{pkg_hash,[
{<<"opentelemetry">>, <<"F0CD36AC8B30B68E8D70CEC5BB88801ED7F3FE79AAC67597054ED5490542E810">>},
{<<"opentelemetry_api">>, <<"77F9644C42340CD8B18C728CDE4822ED55AE136F0D07761B78E8C54DA46AF93A">>}]},
{pkg_hash_ext,[
{<<"opentelemetry">>, <<"3B17F8933A58E1246F42A0C215840FD8218AEBBCABDB0AAC62B0C766FE85542E">>},
{<<"opentelemetry_api">>, <<"4293E06BD369BC004E6FAD5EDBB56456D891F14BD3F9F1772B18F1923E0678EA">>}]}
].

View File

@ -0,0 +1,15 @@
{application, opentelemetry_aws_xray, [
{description, "OpenTelemetry AWS X-Ray TraceId Generator"},
{vsn, "0.1.0"},
{registered, []},
{applications, [
kernel,
stdlib,
opentelemetry
]},
{env, []},
{modules, []},
{licenses, ["Apache-2.0"]},
{links, [{"GitHub", "https://github.com/open-telemetry/opentelemetry-erlang-contrib"}]}
]}.

View File

@ -0,0 +1,29 @@
-module(otel_aws_xray_id_generator).
-behavior(otel_id_generator).
-export([
generate_trace_id/0,
generate_span_id/0
]).
%% Generates a trace_id compatible with AWS X-Ray
%%
%% See More: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids
%%
%% The first 32 bits is the timestamp, the remaining 96 bits are random
-spec generate_trace_id() -> opentelemetry:trace_id().
generate_trace_id() ->
Random = rand:uniform(2 bsl (95 - 1)),
TimeStamp = os:system_time(second),
HiBytes = binary:encode_unsigned(TimeStamp),
LoBytes = binary:encode_unsigned(Random),
TraceIdBytes = <<HiBytes/binary, LoBytes/binary>>,
binary:decode_unsigned(TraceIdBytes).
-spec generate_span_id() -> opentelemetry:span_id().
generate_span_id() ->
rand:uniform(2 bsl 63 - 1).

View File

@ -0,0 +1,33 @@
-module(opentelemetry_aws_xray_SUITE).
-compile(export_all).
-compile(nowarn_export_all).
-include_lib("common_test/include/ct.hrl").
-include_lib("stdlib/include/assert.hrl").
all() ->
[generate_trace_id_should_be_xray_compatible, generate_span_id_should_be_default].
generate_trace_id_should_be_xray_compatible(_Config) ->
MaxAge = 60 * 60 * 24 * 28,
MaxSkew = 60 * 5,
TraceId = otel_aws_xray_id_generator:generate_trace_id(),
EncodedTraceId = binary:encode_unsigned(TraceId),
EpochNow = os:system_time(second),
% The Trace id should have 128 bits (16 bytes)
?assertEqual(16, byte_size(EncodedTraceId)),
% The first 4 bytes is the epoch
Epoch = binary:decode_unsigned(binary:part(EncodedTraceId, 0, 4)),
Delta = EpochNow - Epoch,
?assertEqual(false, (Delta > MaxAge) or (Delta < -MaxSkew)).
generate_span_id_should_be_default(_Config) ->
SpanId = otel_aws_xray_id_generator:generate_span_id(),
?assertEqual(8, byte_size(binary:encode_unsigned(SpanId))).