Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…y-rust into feat/log-kvs
  • Loading branch information
KodrAus committed May 1, 2024
2 parents e647860 + 832fad4 commit 7cfca6d
Show file tree
Hide file tree
Showing 28 changed files with 554 additions and 324 deletions.
11 changes: 11 additions & 0 deletions .github/repository-settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Log of local changes

Maintainers are expected to maintain this log. This is required as per
[OpenTelemetry Community
guidelines](https://github.com/open-telemetry/community/blob/main/docs/how-to-configure-new-repository.md#collaborators-and-teams).

## April 30th 2024

Modified branch protection for main branch to require the following CI checks:
docs
test (stable)
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ observability tools.

## Project Status

| Signal | Status |
| ------- | ---------- |
| Logs | Alpha* |
| Metrics | Alpha |
| Traces | Beta |
| Signal/Component | Overall Status |
| -------------------- | ------------------ |
| Logs-API | Alpha* |
| Logs-SDK | Alpha |
| Logs-OTLP Exporter | Alpha |
| Logs-Appender-Tracing | Alpha |
| Metrics-API | Alpha |
| Metrics-SDK | Alpha |
| Metrics-OTLP Exporter | Alpha |
| Traces-API | Beta |
| Traces-SDK | Beta |
| Traces-OTLP Exporter | Beta |

*OpenTelemetry Rust is not introducing a new end user callable Logging API.
Instead, it provides [Logs Bridge
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-appender-tracing/benches/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl LogProcessor for NoopProcessor {
Ok(())
}

fn shutdown(&mut self) -> LogResult<()> {
fn shutdown(&self) -> LogResult<()> {
Ok(())
}

Expand Down
5 changes: 5 additions & 0 deletions opentelemetry-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@

[#1568]: https://github.com/open-telemetry/opentelemetry-rust/pull/1568

### Changed
- **Breaking** Remove global provider for Logs [#1691](https://github.com/open-telemetry/opentelemetry-rust/pull/1691/)
- The method OtlpLogPipeline::install_simple() and OtlpLogPipeline::install_batch() now return `LoggerProvider` instead of
`Logger`. Refer to the [basic-otlp](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp/src/main.rs) and [basic-otlp-http](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs) examples for how to initialize OTLP Log Exporter to use with OpenTelemetryLogBridge and OpenTelemetryTracingBridge respectively.

## v0.15.0

### Added
Expand Down
9 changes: 4 additions & 5 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::error::Error;
use tracing::info;
use tracing_subscriber::prelude::*;

fn init_logs() -> Result<sdklogs::Logger, opentelemetry::logs::LogError> {
fn init_logs() -> Result<sdklogs::LoggerProvider, opentelemetry::logs::LogError> {
let service_name = env!("CARGO_BIN_NAME");
opentelemetry_otlp::new_pipeline()
.logging()
Expand Down Expand Up @@ -76,13 +76,12 @@ static COMMON_ATTRIBUTES: Lazy<[KeyValue; 4]> = Lazy::new(|| {
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _ = init_tracer()?;
let _ = init_metrics()?;
let _ = init_logs();
// Opentelemetry will not provide a global API to manage the logger provider. Application users must manage the lifecycle of the logger provider on their own. Dropping logger providers will disable log emitting.
let logger_provider = init_logs().unwrap();

let tracer = global::tracer("ex.com/basic");
let meter = global::meter("ex.com/basic");

// configure the global logger to use our opentelemetry logger
let logger_provider = opentelemetry::global::logger_provider();
let layer = OpenTelemetryTracingBridge::new(&logger_provider);
tracing_subscriber::registry().with(layer).init();

Expand All @@ -108,7 +107,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
histogram.record(5.5, COMMON_ATTRIBUTES.as_ref());

global::shutdown_tracer_provider();
global::shutdown_logger_provider();
logger_provider.shutdown();
global::shutdown_meter_provider();

Ok(())
Expand Down
9 changes: 3 additions & 6 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn init_metrics() -> Result<(), MetricsError> {
}
}

fn init_logs() -> Result<opentelemetry_sdk::logs::Logger, LogError> {
fn init_logs() -> Result<opentelemetry_sdk::logs::LoggerProvider, LogError> {
let service_name = env!("CARGO_BIN_NAME");
opentelemetry_otlp::new_pipeline()
.logging()
Expand Down Expand Up @@ -93,10 +93,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _ = init_metrics()?;

// Initialize logs, which sets the global loggerprovider.
let _ = init_logs();

// Retrieve the global LoggerProvider.
let logger_provider = global::logger_provider();
let logger_provider = init_logs().unwrap();

// Create a new OpenTelemetryLogBridge using the above LoggerProvider.
let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider);
Expand Down Expand Up @@ -141,7 +138,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
info!(target: "my-target", "hello from {}. My price is {}", "apple", 1.99);

global::shutdown_tracer_provider();
global::shutdown_logger_provider();
logger_provider.shutdown();
global::shutdown_meter_provider();

Ok(())
Expand Down
6 changes: 5 additions & 1 deletion opentelemetry-otlp/src/exporter/http/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl LogExporter for OtlpHttpClient {
_ => Err(LogError::Other("exporter is already shut down".into())),
})?;

let (body, content_type) = self.build_logs_export_body(batch)?;
let (body, content_type) = { self.build_logs_export_body(batch, &self.resource)? };
let mut request = http::Request::builder()
.method(Method::POST)
.uri(&self.collector_endpoint)
Expand Down Expand Up @@ -50,4 +50,8 @@ impl LogExporter for OtlpHttpClient {
fn shutdown(&mut self) {
let _ = self.client.lock().map(|mut c| c.take());
}

fn set_resource(&mut self, resource: &opentelemetry_sdk::Resource) {
self.resource = resource.into();
}
}
15 changes: 12 additions & 3 deletions opentelemetry-otlp/src/exporter/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::{
};
use http::{HeaderName, HeaderValue, Uri};
use opentelemetry_http::HttpClient;
use opentelemetry_proto::transform::common::tonic::ResourceAttributesWithSchema;

#[cfg(feature = "logs")]
use opentelemetry_sdk::export::logs::LogData;
#[cfg(feature = "trace")]
Expand Down Expand Up @@ -274,6 +276,9 @@ struct OtlpHttpClient {
headers: HashMap<HeaderName, HeaderValue>,
protocol: Protocol,
_timeout: Duration,
#[allow(dead_code)]
// <allow dead> would be removed once we support set_resource for metrics and traces.
resource: opentelemetry_proto::transform::common::tonic::ResourceAttributesWithSchema,
}

impl OtlpHttpClient {
Expand All @@ -291,6 +296,7 @@ impl OtlpHttpClient {
headers,
protocol,
_timeout: timeout,
resource: ResourceAttributesWithSchema::default(),
}
}

Expand Down Expand Up @@ -318,12 +324,15 @@ impl OtlpHttpClient {
fn build_logs_export_body(
&self,
logs: Vec<LogData>,
resource: &opentelemetry_proto::transform::common::tonic::ResourceAttributesWithSchema,
) -> opentelemetry::logs::LogResult<(Vec<u8>, &'static str)> {
use opentelemetry_proto::tonic::collector::logs::v1::ExportLogsServiceRequest;
let resource_logs = logs
.into_iter()
.map(|log_event| (log_event, resource).into())
.collect::<Vec<_>>();
let req = ExportLogsServiceRequest { resource_logs };

let req = ExportLogsServiceRequest {
resource_logs: logs.into_iter().map(Into::into).collect(),
};
match self.protocol {
#[cfg(feature = "http-json")]
Protocol::HttpJson => match serde_json::to_string_pretty(&req) {
Expand Down
23 changes: 18 additions & 5 deletions opentelemetry-otlp/src/exporter/tonic/logs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::fmt;

use async_trait::async_trait;
use core::fmt;
use opentelemetry::logs::{LogError, LogResult};
use opentelemetry_proto::tonic::collector::logs::v1::{
logs_service_client::LogsServiceClient, ExportLogsServiceRequest,
Expand All @@ -12,6 +11,9 @@ use super::BoxInterceptor;

pub(crate) struct TonicLogsClient {
inner: Option<ClientInner>,
#[allow(dead_code)]
// <allow dead> would be removed once we support set_resource for metrics and traces.
resource: opentelemetry_proto::transform::common::tonic::ResourceAttributesWithSchema,
}

struct ClientInner {
Expand Down Expand Up @@ -43,6 +45,7 @@ impl TonicLogsClient {
client,
interceptor,
}),
resource: Default::default(),
}
}
}
Expand All @@ -62,13 +65,19 @@ impl LogExporter for TonicLogsClient {
None => return Err(LogError::Other("exporter is already shut down".into())),
};

let resource_logs = {
batch
.into_iter()
.map(|log_data| (log_data, &self.resource))
.map(Into::into)
.collect()
};

client
.export(Request::from_parts(
metadata,
extensions,
ExportLogsServiceRequest {
resource_logs: batch.into_iter().map(Into::into).collect(),
},
ExportLogsServiceRequest { resource_logs },
))
.await
.map_err(crate::Error::from)?;
Expand All @@ -79,4 +88,8 @@ impl LogExporter for TonicLogsClient {
fn shutdown(&mut self) {
let _ = self.inner.take();
}

fn set_resource(&mut self, resource: &opentelemetry_sdk::Resource) {
self.resource = resource.into();
}
}
38 changes: 15 additions & 23 deletions opentelemetry-otlp/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ use crate::exporter::http::HttpExporterBuilder;

use crate::{NoExporterConfig, OtlpPipeline};
use async_trait::async_trait;
use std::{borrow::Cow, fmt::Debug};
use std::fmt::Debug;

use opentelemetry::logs::LogError;

use opentelemetry::{
global,
logs::{LogError, LoggerProvider},
};
use opentelemetry_sdk::{export::logs::LogData, runtime::RuntimeChannel};

/// Compression algorithm to use, defaults to none.
Expand Down Expand Up @@ -103,6 +101,10 @@ impl opentelemetry_sdk::export::logs::LogExporter for LogExporter {
async fn export(&mut self, batch: Vec<LogData>) -> opentelemetry::logs::LogResult<()> {
self.client.export(batch).await
}

fn set_resource(&mut self, resource: &opentelemetry_sdk::Resource) {
self.client.set_resource(resource);
}
}

/// Recommended configuration for an OTLP exporter pipeline.
Expand Down Expand Up @@ -147,7 +149,7 @@ impl OtlpLogPipeline<LogExporterBuilder> {
/// Returns a [`Logger`] with the name `opentelemetry-otlp` and the current crate version.
///
/// [`Logger`]: opentelemetry_sdk::logs::Logger
pub fn install_simple(self) -> Result<opentelemetry_sdk::logs::Logger, LogError> {
pub fn install_simple(self) -> Result<opentelemetry_sdk::logs::LoggerProvider, LogError> {
Ok(build_simple_with_exporter(
self.exporter_builder.build_log_exporter()?,
self.log_config,
Expand All @@ -163,7 +165,7 @@ impl OtlpLogPipeline<LogExporterBuilder> {
pub fn install_batch<R: RuntimeChannel>(
self,
runtime: R,
) -> Result<opentelemetry_sdk::logs::Logger, LogError> {
) -> Result<opentelemetry_sdk::logs::LoggerProvider, LogError> {
Ok(build_batch_with_exporter(
self.exporter_builder.build_log_exporter()?,
self.log_config,
Expand All @@ -176,27 +178,22 @@ impl OtlpLogPipeline<LogExporterBuilder> {
fn build_simple_with_exporter(
exporter: LogExporter,
log_config: Option<opentelemetry_sdk::logs::Config>,
) -> opentelemetry_sdk::logs::Logger {
) -> opentelemetry_sdk::logs::LoggerProvider {
let mut provider_builder =
opentelemetry_sdk::logs::LoggerProvider::builder().with_simple_exporter(exporter);
if let Some(config) = log_config {
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let logger = provider
.logger_builder(Cow::Borrowed("opentelemetry-otlp"))
.with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION")))
.build();
let _ = global::set_logger_provider(provider);
logger
// logger would be created in the tracing appender
provider_builder.build()
}

fn build_batch_with_exporter<R: RuntimeChannel>(
exporter: LogExporter,
log_config: Option<opentelemetry_sdk::logs::Config>,
runtime: R,
batch_config: Option<opentelemetry_sdk::logs::BatchConfig>,
) -> opentelemetry_sdk::logs::Logger {
) -> opentelemetry_sdk::logs::LoggerProvider {
let mut provider_builder = opentelemetry_sdk::logs::LoggerProvider::builder();
let batch_processor = opentelemetry_sdk::logs::BatchLogProcessor::builder(exporter, runtime)
.with_batch_config(batch_config.unwrap_or_default())
Expand All @@ -206,11 +203,6 @@ fn build_batch_with_exporter<R: RuntimeChannel>(
if let Some(config) = log_config {
provider_builder = provider_builder.with_config(config);
}
let provider = provider_builder.build();
let logger = provider
.logger_builder(Cow::Borrowed("opentelemetry-otlp"))
.with_version(Cow::Borrowed(env!("CARGO_PKG_VERSION")))
.build();
let _ = global::set_logger_provider(provider);
logger
// logger would be created in the tracing appender
provider_builder.build()
}
19 changes: 18 additions & 1 deletion opentelemetry-proto/src/transform/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ pub mod tonic {
use opentelemetry::{Array, Value};
use std::borrow::Cow;

#[cfg(any(feature = "trace", feature = "logs"))]
#[derive(Debug, Default)]
pub struct ResourceAttributesWithSchema {
pub attributes: Attributes,
pub schema_url: Option<String>,
}

#[cfg(any(feature = "trace", feature = "logs"))]
impl From<&opentelemetry_sdk::Resource> for ResourceAttributesWithSchema {
fn from(resource: &opentelemetry_sdk::Resource) -> Self {
ResourceAttributesWithSchema {
attributes: resource_attributes(resource),
schema_url: resource.schema_url().map(ToString::to_string),
}
}
}

#[cfg(any(feature = "trace", feature = "logs"))]
use opentelemetry_sdk::Resource;

Expand Down Expand Up @@ -52,7 +69,7 @@ pub mod tonic {
}

/// Wrapper type for Vec<`KeyValue`>
#[derive(Default)]
#[derive(Default, Debug)]
pub struct Attributes(pub ::std::vec::Vec<crate::proto::tonic::common::v1::KeyValue>);

impl From<Vec<opentelemetry::KeyValue>> for Attributes {
Expand Down
Loading

0 comments on commit 7cfca6d

Please sign in to comment.