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 otlp endpoint config and cli flag #994

Merged
merged 2 commits into from
Dec 6, 2024
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
7 changes: 6 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ pub(crate) fn build_cli() -> Command {
.long("continue-on-errors")
.env("KUBEWARDEN_CONTINUE_ON_ERRORS")
.action(ArgAction::SetTrue)
.hide(true)
.hide(true),
Arg::new("otlp-endpoint")
.long("otlp-endpoint")
.env("OTEL_EXPORTER_OTLP_ENDPOINT")
.default_value("http://localhost:4317")
.help("The OTLP gRPC endpoint for exporting traces and metrics.")
];
args.sort_by(|a, b| a.get_id().cmp(b.get_id()));

Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Config {
pub log_level: String,
pub log_fmt: String,
pub log_no_color: bool,
pub otlp_endpoint: Option<String>,
pub daemon: bool,
pub enable_pprof: bool,
pub daemon_pid_file: String,
Expand Down Expand Up @@ -125,6 +126,9 @@ impl Config {
.get_one::<bool>("log-no-color")
.expect("clap should have assigned a default value")
.to_owned();

let otlp_endpoint = matches.get_one::<String>("otlp-endpoint").cloned();

let (cert_file, key_file) = tls_files(matches)?;
let tls_config = if cert_file.is_empty() {
None
Expand Down Expand Up @@ -160,6 +164,7 @@ impl Config {
log_level,
log_fmt,
log_no_color,
otlp_endpoint,
daemon,
daemon_pid_file,
daemon_stdout_file,
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@
let matches = cli::build_cli().get_matches();
let config = policy_server::config::Config::from_args(&matches)?;

setup_tracing(&config.log_level, &config.log_fmt, config.log_no_color)?;
setup_tracing(
&config.log_level,
&config.log_fmt,
config.log_no_color,
config.otlp_endpoint.as_deref(),

Check warning on line 28 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L25-L28

Added lines #L25 - L28 were not covered by tests
)?;

if config.metrics_enabled {
setup_metrics()?;
setup_metrics(config.otlp_endpoint.as_deref())?;

Check warning on line 32 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L32

Added line #L32 was not covered by tests
};

if config.daemon {
Expand Down
22 changes: 12 additions & 10 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ pub use policy_evaluations_latency::record_policy_latency;

const METER_NAME: &str = "kubewarden";

pub fn setup_metrics() -> Result<()> {
let meter_reader = opentelemetry_sdk::metrics::PeriodicReader::builder(
opentelemetry_otlp::MetricExporter::builder()
.with_tonic()
.with_export_config(ExportConfig::default())
.build()?,
runtime::Tokio,
)
.build();
pub fn setup_metrics(otlp_endpoint: Option<&str>) -> Result<()> {
let mut metric_exporter_builder = opentelemetry_otlp::MetricExporter::builder()
.with_tonic()
.with_export_config(ExportConfig::default());
if let Some(endpoint) = otlp_endpoint {
metric_exporter_builder = metric_exporter_builder.with_endpoint(endpoint);
}
let meter_reader = metric_exporter_builder.build()?;
let periodic_reader =
opentelemetry_sdk::metrics::PeriodicReader::builder(meter_reader, runtime::Tokio).build();
let meter_provider = opentelemetry_sdk::metrics::SdkMeterProvider::builder()
.with_reader(meter_reader)
.with_reader(periodic_reader)
.build();

global::set_meter_provider(meter_provider);
Ok(())
}
Expand Down
23 changes: 17 additions & 6 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use anyhow::{anyhow, Result};
use opentelemetry::trace::TracerProvider;
use opentelemetry_otlp::WithExportConfig;
use tracing_subscriber::prelude::*;
use tracing_subscriber::{fmt, EnvFilter};

use crate::config;

// Setup the tracing system. This MUST be done inside of a tokio Runtime
// because some collectors rely on it and would panic otherwise.
pub fn setup_tracing(log_level: &str, log_fmt: &str, log_no_color: bool) -> Result<()> {
pub fn setup_tracing(
log_level: &str,
log_fmt: &str,
log_no_color: bool,
otlp_endpoint: Option<&str>,
) -> Result<()> {
// setup logging
let filter_layer = EnvFilter::new(log_level)
// some of our dependencies generate trace events too, but we don't care about them ->
Expand Down Expand Up @@ -37,11 +43,16 @@ pub fn setup_tracing(log_level: &str, log_fmt: &str, log_no_color: bool) -> Resu
"otlp" => {
// Create a new OpenTelemetry pipeline sending events to a
// OpenTelemetry collector using the OTLP format.
// The collector must run on localhost (eg: use a sidecar inside of k8s)
// using GRPC
let otlp_exporter = opentelemetry_otlp::SpanExporter::builder()
.with_tonic()
.build()?;
// If no endpoint is provided, the default one is used.
// The default endpoint is "http://localhost:4317".
let mut otlp_exporter_builder =
opentelemetry_otlp::SpanExporter::builder().with_tonic();

if let Some(endpoint) = otlp_endpoint {
otlp_exporter_builder = otlp_exporter_builder.with_endpoint(endpoint);
}

let otlp_exporter = otlp_exporter_builder.build()?;

let tracer_provider = opentelemetry_sdk::trace::TracerProvider::builder()
.with_resource(opentelemetry_sdk::Resource::new(vec![
Expand Down
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub(crate) fn default_test_config() -> Config {
log_level: "info".to_owned(),
log_fmt: "json".to_owned(),
log_no_color: false,
otlp_endpoint: None,
daemon: false,
daemon_pid_file: "policy_server.pid".to_owned(),
daemon_stdout_file: None,
Expand Down
13 changes: 10 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ async fn test_otel() {
traces_output_file_path.to_str().unwrap(),
"/tmp/traces.json",
))
.with_mapped_port(4317, 4317.into())
.with_mapped_port(1337, 4317.into())
.with_cmd(vec!["--config=/etc/otel-collector-config.yaml"])
.with_startup_timeout(Duration::from_secs(30))
.start()
Expand All @@ -783,8 +783,15 @@ async fn test_otel() {
let mut config = default_test_config();
config.metrics_enabled = true;
config.log_fmt = "otlp".to_string();
setup_metrics().unwrap();
setup_tracing(&config.log_level, &config.log_fmt, config.log_no_color).unwrap();
config.otlp_endpoint = Some("http://localhost:1337".to_string());
setup_metrics(config.otlp_endpoint.as_deref()).unwrap();
setup_tracing(
&config.log_level,
&config.log_fmt,
config.log_no_color,
config.otlp_endpoint.as_deref(),
)
.unwrap();

let app = app(config).await;

Expand Down
Loading