Skip to content

Commit

Permalink
fix telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
sebt3 committed Nov 6, 2024
1 parent f0a964c commit 90fd193
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 55 deletions.
36 changes: 18 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ jobs:
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
# Setup qemu for multi-arch support
- name: Docker Setup qemu-action
uses: docker/setup-qemu-action@v2
uses: docker/setup-qemu-action@v3
# Build and push with docker buildx
- name: Setup docker buildx
uses: docker/setup-buildx-action@v2

- name: Configure tags based on git tags + latest
uses: docker/metadata-action@v4
uses: docker/metadata-action@v5
id: meta
with:
images: ${{ github.repository_owner }}/kuberest
Expand All @@ -32,14 +32,14 @@ jobs:
type=semver,pattern={{major}}.{{minor}}
- name: Docker login on main origin
uses: docker/login-action@v2
uses: docker/login-action@v3
if: github.event_name != 'pull_request'
with:
username: ${{ github.repository_owner }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Docker buildx
uses: docker/build-push-action@v4
uses: docker/build-push-action@v6
with:
context: .
cache-from: type=gha,scope=base
Expand All @@ -50,7 +50,7 @@ jobs:
platforms: linux/amd64,linux/arm64

- name: Persist base image build to a tarball
uses: docker/build-push-action@v4
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64
Expand All @@ -59,24 +59,24 @@ jobs:
outputs: type=docker,dest=/tmp/image.tar

- name: Upload base docker image as artifact for e2e tests
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: controller-image
path: /tmp/image.tar

otel-docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
# Setup qemu for multi-arch support
- name: Docker Setup qemu-action
uses: docker/setup-qemu-action@v2
# Build and push with docker buildx
- name: Setup docker buildx
uses: docker/setup-buildx-action@v2
uses: docker/setup-buildx-action@v3

- name: Configure tags based on git tags + latest
uses: docker/metadata-action@v4
uses: docker/metadata-action@v5
id: meta
with:
images: ${{ github.repository_owner }}/kuberest
Expand All @@ -87,14 +87,14 @@ jobs:
type=ref,event=pr
- name: Docker login on main origin
uses: docker/login-action@v2
uses: docker/login-action@v3
if: github.event_name != 'pull_request'
with:
username: ${{ github.repository_owner }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Docker buildx
uses: docker/build-push-action@v4
uses: docker/build-push-action@v6
with:
context: .
cache-from: type=gha,scope=otel
Expand All @@ -110,10 +110,10 @@ jobs:
runs-on: ubuntu-latest
needs: [docker]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: nolar/setup-k3d-k3s@v1
with:
version: v1.27
version: v1.30
k3d-name: kube
k3d-args: "--no-lb --no-rollback --k3s-arg --disable=traefik,servicelb,metrics-server@server:*"
- run: kubectl apply -k deploy/crd
Expand Down Expand Up @@ -148,7 +148,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Install protoc
run: sudo apt-get install -y protobuf-compiler
- uses: dtolnay/rust-toolchain@stable
Expand All @@ -166,12 +166,12 @@ jobs:
integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- uses: nolar/setup-k3d-k3s@v1
with:
version: v1.25
version: v1.30
k3d-name: kube
k3d-args: "--no-lb --no-rollback --k3s-arg --disable=traefik,servicelb,metrics-server@server:*"

Expand All @@ -185,7 +185,7 @@ jobs:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: dtolnay/rust-toolchain@stable
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/rustfmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
components: rustfmt
- run: cargo +nightly fmt

- name: Create Pull Request
uses: peter-evans/create-pull-request@v3
uses: peter-evans/create-pull-request@v7
with:
commit-message: rustfmt
signoff: true
Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ path = "src/lib.rs"

[features]
default = []
telemetry = ["tonic", "opentelemetry-otlp"]
telemetry = ["opentelemetry-otlp"]

[dependencies]
actix-web = "4.9.0"
Expand All @@ -60,8 +60,8 @@ tracing = "0.1.37"
tracing-subscriber = { version = "0.3.18", features = ["json", "env-filter"] }
tracing-opentelemetry = "0.27.0"
opentelemetry = { version = "0.26.0", features = ["trace"] }
opentelemetry-otlp = { version = "0.26.0", features = ["tokio"], optional = true }
tonic = { version = "0.12", optional = true }
opentelemetry_sdk = { version = "0.26.0", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.26.0", optional = true }
thiserror = "1.0.65"
anyhow = "1.0.75"
handlebars = { version = "6.2.0", features = ["script_helper", "string_helpers"] }
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ARG RUST_VERSION=1.77
ARG DEBIAN_VERSION=bookworm
FROM --platform=${BUILDPLATFORM:-linux/amd64} rust:${RUST_VERSION}-slim-${DEBIAN_VERSION} as builder
FROM --platform=${BUILDPLATFORM:-linux/amd64} rust:${RUST_VERSION}-slim-${DEBIAN_VERSION} AS builder
ARG BUILD_DEPS="binutils libssl-dev pkg-config git build-essential"
ARG FEATURES=""
# hadolint ignore=DL3027,DL3008,DL3015
Expand All @@ -15,7 +15,7 @@ COPY . .
RUN CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build $FEATURES --release --bin controller \
&& strip target/release/controller

FROM --platform=${BUILDPLATFORM:-linux/amd64} debian:${DEBIAN_VERSION}-slim as target
FROM --platform=${BUILDPLATFORM:-linux/amd64} debian:${DEBIAN_VERSION}-slim AS target
ARG DEB_PACKAGES="openssl ca-certificates"
# hadolint ignore=DL3027,DL3008
RUN DEBIAN_FRONTEND=noninteractive apt-get update \
Expand Down
4 changes: 2 additions & 2 deletions charts/kuberest/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: kuberest
description: Allow to Control remote REST api endpoints from the confort of your cluster
type: application
version: "1.1.0"
appVersion: "1.1.0"
version: "1.1.1"
appVersion: "1.1.1"
56 changes: 29 additions & 27 deletions src/telemetry.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use opentelemetry::trace::TraceId;
#![allow(unused_imports)] // some used only for telemetry feature
use opentelemetry::trace::{TraceId, TracerProvider};
use opentelemetry_sdk::{runtime, trace as sdktrace, trace::Config, Resource};
use tracing_subscriber::{prelude::*, EnvFilter, Registry};

/// Fetch an opentelemetry::trace::TraceId as hex through the full tracing stack
pub fn get_trace_id() -> TraceId {
use opentelemetry::trace::TraceContextExt as _; // opentelemetry::Context -> opentelemetry::trace::Span
use tracing_opentelemetry::OpenTelemetrySpanExt as _; // tracing::Span to opentelemetry::Context

tracing::Span::current()
.context()
.span()
Expand All @@ -14,47 +15,48 @@ pub fn get_trace_id() -> TraceId {
}

#[cfg(feature = "telemetry")]
async fn init_tracer() -> opentelemetry::sdk::trace::Tracer {
let otlp_endpoint =
std::env::var("OPENTELEMETRY_ENDPOINT_URL").expect("Need a otel tracing collector configured");
fn resource() -> Resource {
use opentelemetry::KeyValue;
Resource::new([
KeyValue::new("service.name", env!("CARGO_PKG_NAME")),
KeyValue::new("service.version", env!("CARGO_PKG_VERSION")),
])
}

let channel = tonic::transport::Channel::from_shared(otlp_endpoint)
.unwrap()
.connect()
.await
.unwrap();
#[cfg(feature = "telemetry")]
fn init_tracer() -> sdktrace::Tracer {
use opentelemetry_otlp::WithExportConfig;
let endpoint = std::env::var("OPENTELEMETRY_ENDPOINT_URL").expect("Needs an otel collector");
let exporter = opentelemetry_otlp::new_exporter().tonic().with_endpoint(endpoint);

opentelemetry_otlp::new_pipeline()
let provider = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(opentelemetry_otlp::new_exporter().tonic().with_channel(channel))
.with_trace_config(opentelemetry::sdk::trace::config().with_resource(
opentelemetry::sdk::Resource::new(vec![opentelemetry::KeyValue::new(
"service.name",
"restpath-controller",
)]),
))
.install_batch(opentelemetry::runtime::Tokio)
.unwrap()
.with_exporter(exporter)
.with_trace_config(Config::default().with_resource(resource()))
.install_batch(runtime::Tokio)
.expect("valid tracer");

opentelemetry::global::set_tracer_provider(provider.clone());
provider.tracer("tracing-otel-subscriber")
}

/// Initialize tracing
pub async fn init() {
// Setup tracing layers
#[cfg(feature = "telemetry")]
let telemetry = tracing_opentelemetry::layer().with_tracer(init_tracer().await);
let otel = tracing_opentelemetry::OpenTelemetryLayer::new(init_tracer());

let logger = tracing_subscriber::fmt::layer().compact();
let env_filter = EnvFilter::try_from_default_env()
.or(EnvFilter::try_new("info"))
.unwrap();

// Decide on layers
let reg = Registry::default();
#[cfg(feature = "telemetry")]
let collector = Registry::default().with(telemetry).with(logger).with(env_filter);
reg.with(env_filter).with(logger).with(otel).init();
#[cfg(not(feature = "telemetry"))]
let collector = Registry::default().with(logger).with(env_filter);

// Initialize tracing
tracing::subscriber::set_global_default(collector).unwrap();
reg.with(env_filter).with(logger).init();
}

#[cfg(test)]
Expand All @@ -73,4 +75,4 @@ mod test {
}
assert_ne!(test_trace_id(), TraceId::INVALID, "valid trace");
}
}
}

0 comments on commit 90fd193

Please sign in to comment.