├── .github
├── images
│ ├── banner-dark.svg
│ └── banner-light.svg
└── workflows
│ └── ci.yaml
├── .gitignore
├── Cargo.toml
├── LICENSE-APACHE
├── LICENSE-MIT
├── README.md
├── deny.toml
├── examples
├── README.md
├── fmt
│ └── main.rs
├── layers
│ └── main.rs
├── noenv
│ └── main.rs
└── simple
│ └── main.rs
├── rust-toolchain
└── src
├── builder.rs
├── error.rs
└── lib.rs
/.github/images/banner-dark.svg:
--------------------------------------------------------------------------------
1 |
577 |
--------------------------------------------------------------------------------
/.github/images/banner-light.svg:
--------------------------------------------------------------------------------
1 |
387 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yaml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | pull_request:
5 | branches:
6 | - main
7 | push:
8 | branches:
9 | - main
10 | tags:
11 | - "v*"
12 |
13 | jobs:
14 | rust_fmt_check:
15 | name: Rustfmt check
16 | runs-on: ubuntu-latest
17 | steps:
18 | - uses: actions/checkout@v3
19 | - uses: actions/cache@v3
20 | with:
21 | path: |
22 | ~/.cargo/registry
23 | ~/.cargo/git
24 | target
25 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
26 | - name: Run cargo fmt
27 | uses: actions-rs/cargo@v1
28 | with:
29 | command: fmt
30 | args: -- --check
31 | clippy_check:
32 | name: Clippy check
33 | runs-on: ubuntu-latest
34 | steps:
35 | - uses: actions/checkout@v3
36 | - uses: actions/cache@v3
37 | with:
38 | path: |
39 | ~/.cargo/registry
40 | ~/.cargo/git
41 | target
42 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
43 | - name: Install clippy
44 | run: rustup component add clippy
45 | - name: Run clippy check
46 | uses: actions-rs/clippy-check@v1
47 | with:
48 | token: ${{ secrets.GITHUB_TOKEN }}
49 | test:
50 | name: Run tests
51 | runs-on: ubuntu-latest
52 | steps:
53 | - uses: actions/checkout@v3
54 | - uses: actions/cache@v3
55 | with:
56 | path: |
57 | ~/.cargo/registry
58 | ~/.cargo/git
59 | target
60 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
61 | - name: Run cargo test
62 | env:
63 | AXIOM_TOKEN: ${{ secrets.AXIOM_TOKEN }}
64 | AXIOM_URL: https://cloud.dev.axiomtestlabs.co
65 | AXIOM_DATASET: _traces
66 | run: cargo test
67 |
68 | validate-crate:
69 | name: Validate crate
70 | runs-on: ubuntu-latest
71 | steps:
72 | - uses: actions/checkout@v3
73 | - uses: actions/cache@v3
74 | with:
75 | path: |
76 | ~/.cargo/registry
77 | ~/.cargo/git
78 | target
79 | key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
80 | - name: Test publish
81 | run: cargo publish --dry-run
82 |
83 | publish_on_crates_io:
84 | name: Publish on crates.io
85 | runs-on: ubuntu-latest
86 | if: github.repository_owner == 'axiomhq' && startsWith(github.ref, 'refs/tags')
87 | needs:
88 | - rust_fmt_check
89 | - clippy_check
90 | - test
91 | steps:
92 | - uses: actions/checkout@v3
93 | - name: Publish on crates.io
94 | env:
95 | CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
96 | run: cargo publish
97 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | /Cargo.lock
3 | /examples/target
4 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "tracing-axiom"
3 | version = "0.7.0"
4 | authors = [
5 | "Arne Bahlo ",
6 | "Darach Ennis ",
7 | "Heinz Gies ",
8 | ]
9 | edition = "2021"
10 | rust-version = "1.73"
11 | license = "MIT OR Apache-2.0"
12 | description = "The tracing layer for shipping traces to Axiom"
13 | homepage = "https://axiom.co"
14 | repository = "https://github.com/axiomhq/tracing-axiom"
15 | documentation = "https://docs.rs/tracing-axiom"
16 | keywords = ["tracing", "axiom", "instrumentation", "opentelemetry"]
17 | readme = "README.md"
18 | include = [
19 | "src/**/*.rs",
20 | "examples",
21 | "README.md",
22 | "LICENSE-APACHE",
23 | "LICENSE-MIT",
24 | ]
25 | resolver = "2"
26 |
27 | [dependencies]
28 | url = "2.4.1"
29 | thiserror = "1"
30 |
31 | tracing-core = { version = "0.1", default-features = false, features = ["std"] }
32 | tracing-opentelemetry = { version = "0.23", default-features = false }
33 | tracing-subscriber = { version = "0.3", default-features = false, features = [
34 | "smallvec",
35 | "std",
36 | "registry",
37 | "fmt",
38 | "json",
39 | ] }
40 |
41 |
42 | reqwest = { version = "0.11", default-features = false }
43 | opentelemetry = { version = "0.22" }
44 | opentelemetry-otlp = { version = "0.15", features = [
45 | "prost",
46 | "tokio",
47 | "http-proto",
48 | "reqwest-client",
49 | ] }
50 | opentelemetry-semantic-conventions = "0.15"
51 | opentelemetry_sdk = { version = "0.22", features = ["rt-tokio"] }
52 |
53 | [dev-dependencies]
54 | tokio = { version = "1", features = ["full", "tracing"] }
55 | tracing = { version = "0.1", features = ["log"] }
56 | tracing-subscriber = { version = "0.3", default-features = false, features = [
57 | "smallvec",
58 | "std",
59 | "registry",
60 | "fmt",
61 | "json",
62 | "ansi",
63 | ] }
64 |
65 | # Example that demonstrates how to use the `tracing-axiom` layer with the `tracing-subscriber` crate.
66 | [[example]]
67 | name = "layers"
68 |
69 | # Simple most example use of `tracing-axiom`.
70 | [[example]]
71 | name = "simple"
72 |
73 | # Example that demonstrates using a nice color and formating
74 | [[example]]
75 | name = "fmt"
76 |
77 | # Demonstrate setting config in the code
78 | [[example]]
79 | name = "noenv"
80 |
81 | [features]
82 | default = ["rustls-tls"]
83 | default-tls = ["reqwest/default-tls"]
84 | native-tls = ["reqwest/native-tls"]
85 | rustls-tls = ["reqwest/rustls-tls"]
86 |
--------------------------------------------------------------------------------
/LICENSE-APACHE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Axiom, Inc.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # tracing-axiom
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | [](https://docs.rs/tracing-axiom/)
14 | [](https://github.com/axiomhq/tracing-axiom/actions?query=workflow%3ACI)
15 | [](https://crates.io/crates/tracing-axiom)
16 | [](LICENSE-APACHE)
17 |
18 | [Axiom](https://axiom.co) unlocks observability at any scale.
19 |
20 | - **Ingest with ease, store without limits:** Axiom’s next-generation datastore enables ingesting petabytes of data with ultimate efficiency. Ship logs from Kubernetes, AWS, Azure, Google Cloud, DigitalOcean, Nomad, and others.
21 | - **Query everything, all the time:** Whether DevOps, SecOps, or EverythingOps, query all your data no matter its age. No provisioning, no moving data from cold/archive to “hot”, and no worrying about slow queries. All your data, all. the. time.
22 | - **Powerful dashboards, for continuous observability:** Build dashboards to collect related queries and present information that’s quick and easy to digest for you and your team. Dashboards can be kept private or shared with others, and are the perfect way to bring together data from different sources
23 |
24 | For more information check out the [official documentation](https://axiom.co/docs).
25 |
26 | ## Usage
27 |
28 | Add the following to your `Cargo.toml`:
29 |
30 | ```toml
31 | [dependencies]
32 | tracing-axiom = "0.7"
33 | ```
34 |
35 | Create a dataset in Axiom and export the name as `AXIOM_DATASET`.
36 | Then create an API token with ingest permission into that dataset in
37 | [the Axiom settings](https://cloud.axiom.co/settings/profile) and export it as
38 | `AXIOM_TOKEN`.
39 |
40 | Now you can set up tracing like this:
41 |
42 | ```rust,no_run
43 | use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _, Registry};
44 | #[tokio::main]
45 | async fn main() -> Result<(), Box> {
46 | let axiom_layer = tracing_axiom::default("readme")?; // Set AXIOM_DATASET and AXIOM_TOKEN in your env!
47 | Registry::default().with(axiom_layer).init();
48 | say_hello();
49 | Ok(())
50 | }
51 |
52 | #[tracing::instrument]
53 | pub fn say_hello() {
54 | tracing::info!("Hello, world!");
55 | }
56 | ```
57 |
58 | For further examples, head over to the [examples](examples) directory.
59 |
60 | > **Note**: Due to a limitation of an underlying library, [events outside of a
61 | > span are not recorded](https://docs.rs/tracing-opentelemetry/latest/src/tracing_opentelemetry/layer.rs.html#807).
62 |
63 | ## Features
64 |
65 | The following are a list of
66 | [Cargo features](https://doc.rust-lang.org/stable/cargo/reference/features.html#the-features-section)
67 | that can be enabled or disabled:
68 |
69 | - **rustls-tls** _(enabled by default)_: Enables TLS functionality provided by `rustls`.
70 | - **default-tls**: uses reqwest default TLS library.
71 | - **native-tls**: Enables TLS functionality provided by `native-tls`.
72 |
73 | ## FAQ & Troubleshooting
74 |
75 | ### How do I log traces to the console in addition to Axiom?
76 | You can use this library to get a [`tracing-subscriber::layer`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/index.html)
77 | and combine it with other layers, for example one that prints traces to the
78 | console.
79 | You can see how this works in the [fmt example](./examples/fmt).
80 |
81 | ### My test function hangs indefinitely
82 | This can happen when you use `#[tokio::test]` as that defaults to a
83 | single-threaded executor, but the
84 | [`opentelemetry`](https://docs.rs/opentelemetry) crate requires a multi-thread
85 | executor.
86 |
87 | ## License
88 |
89 | Licensed under either of
90 |
91 | - Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or [apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0))
92 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or [opensource.org/licenses/MIT](http://opensource.org/licenses/MIT))
93 |
94 | at your option.
95 |
--------------------------------------------------------------------------------
/deny.toml:
--------------------------------------------------------------------------------
1 | #
2 | # Ensure we do not use any libraries that have licenses incompatible ASL-2 / MIT
3 | #
4 |
5 | [licenses]
6 | unlicensed = "deny"
7 | default = "deny"
8 | copyleft = "deny"
9 | allow = [
10 | # "0BSD",
11 | "Apache-2.0",
12 | # "BSD-2-Clause",
13 | "BSD-3-Clause",
14 | "BSL-1.0",
15 | # "CC0-1.0",
16 | # "ISC",
17 | "MIT",
18 | # "OpenSSL",
19 | "Unicode-DFS-2016",
20 | "Zlib"
21 | ]
22 |
23 | exceptions = [
24 | # MPL-2.0 are added case-by-case to make sure we are in compliance. To be in
25 | # compliance we cannot be modifying the source files.
26 | ]
27 |
28 | [licenses.private]
29 | ignore = true
30 |
31 | [[licenses.clarify]]
32 | name = "ring"
33 | version = "*"
34 | expression = "MIT AND ISC AND OpenSSL"
35 | license-files = [
36 | { path = "LICENSE", hash = 0xbd0eed23 }
37 | ]
38 |
39 | [advisories]
40 | ignore = [ ]
41 |
--------------------------------------------------------------------------------
/examples/README.md:
--------------------------------------------------------------------------------
1 | # Examples
2 |
3 | * [simple](./simple) - Uses defaults provided by Axiom and is a one line setup.
4 | * [fmt](./fmt) - Uses layers with out of the box local formatting and Axiom remote endpoint.
5 | * [layers](./layers) - The kitchen sink. If you have a rich tracing setup, just plug tracing-axiom into your existing setup.
6 | * [noenv]('./noenv) - Example that does not use environment variables for tracing setup.
7 |
8 | ## Setup
9 |
10 | The `sdk` and `layers` examples assume the existance of a dataset called `tracing-axiom-examples` in your axiom
11 | environment. You can setup a dataset using the `Axiom Cli` ([docs](https://axiom.co/docs/reference/cli), [github](https://github.com/axiomhq/cli)) as follows:
12 |
13 | ```shell
14 | axiom dataset create --name tracing-axiom-examples --description "Dataset for testing tracing axiom"
15 | export AXIOM_DATASET=tracing-axiom-examples
16 | ```
17 |
18 | If the environment variable is not set, the examples will fail with an error.
19 |
--------------------------------------------------------------------------------
/examples/fmt/main.rs:
--------------------------------------------------------------------------------
1 | use tracing::{info, instrument};
2 | use tracing_subscriber::prelude::*;
3 |
4 | #[tokio::main]
5 | async fn main() -> Result<(), Box> {
6 | let axiom_layer = tracing_axiom::builder("fmt").build()?;
7 | let fmt_layer = tracing_subscriber::fmt::layer().pretty();
8 | tracing_subscriber::registry()
9 | .with(fmt_layer)
10 | .with(axiom_layer)
11 | .try_init()?;
12 |
13 | say_hello();
14 |
15 | // Ensure that the tracing provider is shutdown correctly
16 | opentelemetry::global::shutdown_tracer_provider();
17 |
18 | Ok(())
19 | }
20 |
21 | #[instrument]
22 | fn say_hello() {
23 | info!("hello world")
24 | }
25 |
--------------------------------------------------------------------------------
/examples/layers/main.rs:
--------------------------------------------------------------------------------
1 | use opentelemetry::global;
2 | use opentelemetry_sdk::propagation::TraceContextPropagator;
3 | use tracing::{info, instrument};
4 | use tracing_subscriber::Registry;
5 | use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt};
6 |
7 | #[instrument]
8 | fn say_hi(id: u64, name: impl Into + std::fmt::Debug) {
9 | info!(?id, "Hello, {}!", name.into());
10 | }
11 |
12 | fn setup_tracing(tags: &[(&'static str, &'static str)]) -> Result<(), tracing_axiom::Error> {
13 | info!("Axiom OpenTelemetry tracing endpoint is configured:");
14 | // Setup an AWS CloudWatch compatible tracing layer
15 | let cloudwatch_layer = tracing_subscriber::fmt::layer()
16 | .json()
17 | .with_ansi(false)
18 | .without_time()
19 | .with_target(false);
20 |
21 | // Setup an Axiom OpenTelemetry compatible tracing layer
22 | let tag_iter = tags.iter().copied();
23 | let axiom_layer = tracing_axiom::builder("layers")
24 | .with_tags(tag_iter)
25 | .build()?;
26 |
27 | // Setup our multi-layered tracing subscriber
28 | Registry::default()
29 | .with(axiom_layer)
30 | .with(cloudwatch_layer)
31 | .init();
32 |
33 | global::set_text_map_propagator(TraceContextPropagator::new());
34 |
35 | Ok(())
36 | }
37 |
38 | const TAGS: &[(&str, &str)] = &[
39 | ("aws_region", "us-east-1"), // NOTE - example for illustrative purposes only
40 | ];
41 |
42 | #[tokio::main(flavor = "multi_thread")]
43 | async fn main() -> Result<(), Box> {
44 | setup_tracing(TAGS)?; // NOTE we depend on environment variable
45 |
46 | say_hi(42, "world");
47 |
48 | // do something with result ...
49 |
50 | // Ensure that the tracing provider is shutdown correctly
51 | opentelemetry::global::shutdown_tracer_provider();
52 |
53 | Ok(())
54 | }
55 |
--------------------------------------------------------------------------------
/examples/noenv/main.rs:
--------------------------------------------------------------------------------
1 | use tracing::{info, instrument};
2 | use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _, Registry};
3 |
4 | #[instrument]
5 | fn say_hi(id: u64, name: impl Into + std::fmt::Debug) {
6 | info!(?id, "Hello, {}!", name.into());
7 | }
8 |
9 | #[tokio::main(flavor = "multi_thread")]
10 | async fn main() -> Result<(), Box> {
11 | let axiom_layer = tracing_axiom::builder("noenv")
12 | .with_tags([("aws_region", "us-east-1")].iter().copied()) // Set otel tags
13 | .with_dataset("tracing-axiom-examples")? // Set dataset
14 | .with_token("xaat-some-valid-token")? // Set API token
15 | .with_url("http://localhost:4318")? // Set URL, can be changed to any OTEL endpoint
16 | .build()?; // Initialize tracing
17 |
18 | Registry::default().with(axiom_layer).init();
19 |
20 | say_hi(42, "world");
21 |
22 | // do something with result ...
23 |
24 | // Ensure that the tracing provider is shutdown correctly
25 | opentelemetry::global::shutdown_tracer_provider();
26 |
27 | Ok(())
28 | }
29 |
--------------------------------------------------------------------------------
/examples/simple/main.rs:
--------------------------------------------------------------------------------
1 | use tracing::{error, instrument};
2 | use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _, Registry};
3 |
4 | #[instrument]
5 | fn say_hello() {
6 | error!("hello world")
7 | }
8 |
9 | #[tokio::main]
10 | async fn main() -> Result<(), Box> {
11 | let axiom_layer = tracing_axiom::default("simple")?;
12 |
13 | Registry::default().with(axiom_layer).init();
14 |
15 | say_hello();
16 |
17 | // Ensure that the tracing provider is shutdown correctly
18 | opentelemetry::global::shutdown_tracer_provider();
19 |
20 | Ok(())
21 | }
22 |
--------------------------------------------------------------------------------
/rust-toolchain:
--------------------------------------------------------------------------------
1 | stable
--------------------------------------------------------------------------------
/src/builder.rs:
--------------------------------------------------------------------------------
1 | use crate::Error;
2 | use opentelemetry::{Key, KeyValue, Value};
3 | use opentelemetry_otlp::WithExportConfig;
4 | use opentelemetry_sdk::{
5 | trace::{Config as TraceConfig, Tracer},
6 | Resource,
7 | };
8 | use opentelemetry_semantic_conventions::resource::{
9 | SERVICE_NAME, TELEMETRY_SDK_LANGUAGE, TELEMETRY_SDK_NAME, TELEMETRY_SDK_VERSION,
10 | };
11 | use reqwest::Url;
12 | use std::{
13 | collections::HashMap,
14 | env::{self, VarError},
15 | time::Duration,
16 | };
17 | use tracing_core::Subscriber;
18 | use tracing_opentelemetry::OpenTelemetryLayer;
19 | use tracing_subscriber::registry::LookupSpan;
20 |
21 | const CLOUD_URL: &str = "https://api.axiom.co";
22 |
23 | /// Builder for creating a tracing tracer, a layer or a subscriber that sends traces to
24 | /// Axiom via the `OpenTelemetry` protocol. The API token is read from the `AXIOM_TOKEN`
25 | /// environment variable. The dataset name is read from the `AXIOM_DATASET` environment
26 | /// variable. The URL defaults to Axiom Cloud whose URL is `https://cloud.axiom.co` but
27 | /// can be overridden by setting the `AXIOM_URL` environment variable for testing purposes
28 | ///
29 | #[derive(Debug, Default)]
30 | pub struct Builder {
31 | dataset_name: Option,
32 | token: Option,
33 | url: Option,
34 | tags: Vec,
35 | trace_config: Option,
36 | service_name: Option,
37 | timeout: Option,
38 | }
39 |
40 | fn get_env(env_var_name: &'static str) -> Result