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

feat: add labels to opentelemetry append #59

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
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
25 changes: 17 additions & 8 deletions src/append/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::borrow::Cow;
use std::sync::Arc;
use std::time::Duration;
use std::time::SystemTime;
Expand Down Expand Up @@ -46,20 +47,26 @@ pub enum OpentelemetryWireProtocol {
#[derive(Debug)]
pub struct OpentelemetryLog {
name: String,
category: String,
library: Arc<InstrumentationLibrary>,
provider: LoggerProvider,
}

impl OpentelemetryLog {
pub fn new(
name: impl Into<String>,
category: impl Into<String>,
otlp_endpoint: impl Into<String>,
protocol: OpentelemetryWireProtocol,
) -> Result<Self, opentelemetry::logs::LogError> {
Self::new_with_labels(name, otlp_endpoint, protocol, [])
}

pub fn new_with_labels(
name: impl Into<String>,
otlp_endpoint: impl Into<String>,
protocol: OpentelemetryWireProtocol,
labels: impl IntoIterator<Item = (Cow<'static, str>, Cow<'static, str>)>,
Comment on lines 55 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are public APIs. It'd be better we add comments to explain the constructor's arguments.

) -> Result<Self, opentelemetry::logs::LogError> {
let name = name.into();
let category = category.into();
let otlp_endpoint = otlp_endpoint.into();

let exporter = match protocol {
Expand Down Expand Up @@ -91,13 +98,18 @@ impl OpentelemetryLog {

let provider = LoggerProvider::builder()
.with_batch_exporter(exporter, opentelemetry_sdk::runtime::Tokio)
.with_resource(opentelemetry_sdk::Resource::new(labels.into_iter().map(
|(key, value)| opentelemetry::KeyValue {
key: key.into(),
value: value.into(),
},
)))
.build();

let library = Arc::new(InstrumentationLibrary::builder(name.clone()).build());

Ok(Self {
name,
category,
library,
provider,
})
Expand Down Expand Up @@ -158,10 +170,7 @@ impl Append for OpentelemetryLog {
.into_iter()
.filter_map(|r| r.err())
{
eprintln!(
"failed to flush logger ({}@{}): {}",
self.name, self.category, err
);
eprintln!("failed to flush logger {}: {}", self.name, err);
}
}
}
Expand Down