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

Add OTLP LogExporter to the example #1168

Merged
merged 4 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions opentelemetry-otlp/examples/basic-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ publish = false

[dependencies]
once_cell = "1.17"
opentelemetry_api = { path = "../../../opentelemetry-api", features = ["metrics"] }
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio"] }
opentelemetry-otlp = { path = "../../../opentelemetry-otlp", features = ["tonic", "metrics"] }
opentelemetry_api = { path = "../../../opentelemetry-api", features = ["metrics", "logs"] }
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "logs"] }
opentelemetry-otlp = { path = "../../../opentelemetry-otlp", features = ["tonic", "metrics", "logs"] }
opentelemetry-semantic-conventions = { path = "../../../opentelemetry-semantic-conventions" }
tokio = { version = "1.0", features = ["full"] }
opentelemetry-appender-log = { path = "../../../opentelemetry-appender-log"}
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
log = {version = "0.4.17"}
4 changes: 2 additions & 2 deletions opentelemetry-otlp/examples/basic-otlp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ If you don't want to use `docker-compose`, you can manually run the `otel/opente
and inspect the logs to see traces being transferred.

```shell
# Run `opentelemetry-collector`
$ docker run -p4317:4317 otel/opentelemetry-collector:latest
# From the current directory, run `opentelemetry-collector`
$ docker run --rm -it -p 4317:4317 -p 4318:4318 -v $(pwd):/cfg otel/opentelemetry-collector:latest --config=/cfg/otel-collector-config.yaml

# Report spans/metrics
$ cargo run
Expand Down
31 changes: 11 additions & 20 deletions opentelemetry-otlp/examples/basic-otlp/otel-collector-config.yaml
Original file line number Diff line number Diff line change
@@ -1,36 +1,27 @@
# This is a configuration file for the OpenTelemetry Collector intended to be
# used in conjunction with the opentelemetry-otlp example.
#
# For more information about the OpenTelemetry Collector see:
# https://github.com/open-telemetry/opentelemetry-collector
#
receivers:
otlp:
protocols:
http:
grpc:
http:

exporters:
logging:
loglevel: debug

jaeger:
Copy link
Member Author

Choose a reason for hiding this comment

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

this was cleaned up as the purpose of this example is to show how to use OTLP Exporter. The config will print the telemetry to stdout which is good enough to confirm things working. Even if there is a need to use Jaeger to visualize, Jaeger can natively understand OTLP, so no need to futher export from collector.

endpoint: jaeger-all-in-one:14250
tls:
insecure: true

processors:
batch:

extensions:
health_check:
pprof:
endpoint: :1888
zpages:
endpoint: :55679

service:
extensions: [pprof, zpages, health_check]
Copy link
Member Author

Choose a reason for hiding this comment

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

removed everything unnecessary to keep the example config the the minimal.

pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [logging, jaeger]
exporters: [logging]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [logging]
logs:
receivers: [otlp]
exporters: [logging]
47 changes: 45 additions & 2 deletions opentelemetry-otlp/examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use log::{info, Level};
use once_cell::sync::Lazy;
use opentelemetry_api::global;
use opentelemetry_api::global::shutdown_tracer_provider;
use opentelemetry_api::global::{
logger_provider, shutdown_logger_provider, shutdown_tracer_provider,
};
use opentelemetry_api::logs::LogError;
use opentelemetry_api::trace::TraceError;
use opentelemetry_api::{
metrics,
trace::{TraceContextExt, Tracer},
Key, KeyValue,
};
use opentelemetry_appender_log::OpenTelemetryLogBridge;
use opentelemetry_otlp::{ExportConfig, WithExportConfig};
use opentelemetry_sdk::logs::Config;
use opentelemetry_sdk::{metrics::MeterProvider, runtime, trace as sdktrace, Resource};
use std::error::Error;

Expand All @@ -22,7 +28,7 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
.with_trace_config(
sdktrace::config().with_resource(Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
"trace-demo",
"basic-otlp-tracing-example",
)])),
)
.install_batch(runtime::Tokio)
Expand All @@ -40,9 +46,30 @@ fn init_metrics() -> metrics::Result<MeterProvider> {
.tonic()
.with_export_config(export_config),
)
.with_resource(Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
"basic-otlp-metrics-example",
)]))
.build()
}

fn init_logs() -> Result<opentelemetry_sdk::logs::Logger, LogError> {
opentelemetry_otlp::new_pipeline()
.logging()
.with_log_config(
Config::default().with_resource(Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
"basic-otlp-logging-example",
)])),
)
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.install_batch(runtime::Tokio)
}

const LEMONS_KEY: Key = Key::from_static_str("lemons");
const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another");

Expand All @@ -63,6 +90,17 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _ = init_tracer()?;
let meter_provider = init_metrics()?;

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

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

// Create a new OpenTelemetryLogBridge using the above LoggerProvider.
let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider);
log::set_boxed_logger(Box::new(otel_log_appender)).unwrap();
log::set_max_level(Level::Info.to_level_filter());

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

Expand All @@ -86,6 +124,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
);
span.set_attribute(ANOTHER_KEY.string("yes"));

info!(target: "my-target", "hello from {}. My price is {}. I am also inside a Span!", "banana", 2.99);

tracer.in_span("Sub operation...", |cx| {
let span = cx.span();
span.set_attribute(LEMONS_KEY.string("five"));
Expand All @@ -96,7 +136,10 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
});
});

info!(target: "my-target", "hello from {}. My price is {}", "apple", 1.99);

shutdown_tracer_provider();
shutdown_logger_provider();
meter_provider.shutdown()?;

Ok(())
Expand Down
Loading