-
Notifications
You must be signed in to change notification settings - Fork 186
feat: allow OTLP exporting by extending existing logger configs #835
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
Changes from all commits
ec236cf
1b48b1d
f87abf8
9ddf802
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,128 @@ | ||||||
| use opentelemetry::{trace::TracerProvider, KeyValue}; | ||||||
| use opentelemetry_sdk::{trace::SdkTracerProvider, Resource}; | ||||||
| use opentelemetry_semantic_conventions::{ | ||||||
| resource::{DEPLOYMENT_ENVIRONMENT_NAME, SERVICE_NAME, SERVICE_VERSION}, | ||||||
| SCHEMA_URL, | ||||||
| }; | ||||||
| use tracing_subscriber::Layer; | ||||||
|
|
||||||
| /// Drop guard for the OTLP exporter. This will shutdown the exporter when | ||||||
| /// dropped, and generally should be held for the lifetime of the `main` | ||||||
| /// function. | ||||||
| /// | ||||||
| /// The guard may be used to produce a [`tracing`] layer that can be added to | ||||||
| /// an existing [`tracing::Subscriber`]. | ||||||
| #[derive(Debug)] | ||||||
| pub struct OtlpGuard(SdkTracerProvider); | ||||||
|
|
||||||
| impl OtlpGuard { | ||||||
| /// Get a tracer from the provider. | ||||||
| fn tracer(&self, s: &'static str) -> opentelemetry_sdk::trace::Tracer { | ||||||
| self.0.tracer(s) | ||||||
| } | ||||||
|
|
||||||
| /// Create a filtered tracing layer. | ||||||
| pub fn layer<S>(&self) -> impl Layer<S> | ||||||
| where | ||||||
| S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>, | ||||||
| { | ||||||
| let tracer = self.tracer("tracing-otel-subscriber"); | ||||||
| tracing_opentelemetry::layer().with_tracer(tracer) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Configuration for the OTLP system. Currently this allows configuring the | ||||||
| /// deployment environment name. | ||||||
| /// | ||||||
| /// OTEL and OTLP are configured primarily via environment variables. The | ||||||
| /// standard variables are as follows: | ||||||
| /// | ||||||
| /// - `OTEL_TRACES_EXPORTER` - Set the exporter to be used. Generally should be | ||||||
| /// set to `otlp`. Other options include `none`, `zipkin`, etc. See the | ||||||
| /// [relevant documentation] for more details. | ||||||
| /// | ||||||
| /// - `OTEL_EXPORTER_OTLP_ENDPOINT` - Set to the URL endpoint to which to send | ||||||
| /// OTLP data. If not set, traces will not be exported. This will typically | ||||||
| /// be a URL ending in port 4317 (grpc) or 4318 (http). | ||||||
| /// | ||||||
| /// - `OTEL_EXPORTER_OTLP_PROTOCOL` - Specifies the OTLP transport protocol to | ||||||
| /// be used for all telemetry data. Acceptable values are `grpc`, | ||||||
| /// `http/protobuf`, and `http/json`. | ||||||
| /// | ||||||
| /// For advanced exporter configuration via environment variables, see the | ||||||
| /// [OTLP documentation]. | ||||||
| /// | ||||||
| /// [relevant documentation]: https://opentelemetry.io/docs/instrumentation/js/exporters/#environment-variables | ||||||
| /// [OTLP documentation]: https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/ | ||||||
| #[derive(Default, PartialEq, Eq, Clone, Debug, serde::Deserialize)] | ||||||
| #[non_exhaustive] | ||||||
| pub struct OtlpConfig { | ||||||
| /// The name of the deployment environment (a.k.a deployment tier), e.g. | ||||||
| /// "production", "staging". [See documentation here.] | ||||||
| /// | ||||||
| /// [See documentation here.]: https://opentelemetry.io/docs/specs/semconv/resource/deployment-environment/ | ||||||
| pub otlp_environment: String, | ||||||
| } | ||||||
|
|
||||||
| impl OtlpConfig { | ||||||
| /// Default OTEL configuration for development. | ||||||
| pub fn dev() -> Self { | ||||||
| Self { | ||||||
| otlp_environment: "development".to_owned(), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// Instantiate a new OTEL configuration, with default values. | ||||||
| pub fn new() -> Self { | ||||||
| Self::default() | ||||||
| } | ||||||
|
|
||||||
| /// Set the environment name. | ||||||
| pub fn with_environment(mut self, name: impl Into<String>) -> Self { | ||||||
| self.otlp_environment = name.into(); | ||||||
| self | ||||||
| } | ||||||
|
|
||||||
| fn resource(&self) -> Resource { | ||||||
| Resource::builder() | ||||||
| .with_schema_url( | ||||||
| [ | ||||||
| KeyValue::new(SERVICE_NAME, env!("CARGO_PKG_NAME")), | ||||||
| KeyValue::new(SERVICE_VERSION, env!("CARGO_PKG_VERSION")), | ||||||
| KeyValue::new(DEPLOYMENT_ENVIRONMENT_NAME, self.otlp_environment.clone()), | ||||||
| ], | ||||||
| SCHEMA_URL, | ||||||
| ) | ||||||
| .build() | ||||||
| } | ||||||
|
|
||||||
| /// Instantiate a new OTEL provider, with a simple batch exporter, and | ||||||
| /// start relevant tasks. Return a guard that will shut down the provider | ||||||
| /// and exporter when dropped. | ||||||
| pub fn provider(&self) -> OtlpGuard { | ||||||
| let exporter = opentelemetry_otlp::SpanExporter::builder() | ||||||
| .with_http() | ||||||
| .build() | ||||||
| .unwrap(); | ||||||
|
||||||
| .unwrap(); | |
| .expect("failed to build OTLP span exporter"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type 'Format' is ambiguous and should be fully qualified as 'Format<Full, SystemTime>' to match the pattern used in the 'builder()' method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i go the other way. including default type args you don't have to is overly verbose