Skip to content

Commit

Permalink
Merge pull request #1 from tembo-io/nhudson/add_ci
Browse files Browse the repository at this point in the history
Initial code import with CI, publish and examples
  • Loading branch information
nhudson authored Aug 8, 2023
2 parents 57fd204 + 42168f9 commit b63cec5
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @ChuckHend @ianstanton @ryw @sjmiller609 @nhudson
79 changes: 79 additions & 0 deletions .github/actions/publish-crate/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: 'Publish to crates.io'
description: 'Publish cratest to crates.io and some other crates.io-related actions, like checking if a version is already published.'
inputs:
working-directory:
required: false
description: "In which directory should we run 'cargo publish'?"
default: "."
cargo-registry-token:
required: true
description: "The CARGO_REGISTRY_TOKEN to use"
fail-if-version-published:
description: "If the version is already published, should we fail, or ignore? By default, ignore."
required: false
default: false
dry-run:
description: "Use --dry-run flag on cargo publish?"
required: false
default: false
toolchain:
description: "Which Rust toolchain to use?"
default: "stable"
required: false
outputs: {}
runs:
using: "composite"
steps:
- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ inputs.toolchain }}
- name: Install TOML parser
shell: bash
run: |
set -xe
wget https://github.com/freshautomations/stoml/releases/download/v0.7.1/stoml_linux_amd64 &> /dev/null
mv stoml_linux_amd64 stoml
chmod +x stoml
sudo mv stoml /usr/local/bin/
- name: Publish
shell: bash
working-directory: ${{ inputs.working-directory }}
run: |
set -xe
# If package.publish is false, skip the rest
SHOULD_PUBLISH=$(stoml Cargo.toml package.publish)
if [[ "${SHOULD_PUBLISH}" == "false" ]]; then
echo "Found package.publish is false. Skipping."
exit 0
fi
# Get crate information
NAME=$(stoml Cargo.toml package.name)
VERSION=$(stoml Cargo.toml package.version)
VERSIONS_INFO=$(curl https://crates.io/api/v1/crates/${NAME} 2> /dev/null)
# "|| true" handles the case where this crate hasn't yet been published
PUBLISHED_VERSIONS=$(echo ${VERSIONS_INFO} | jq -r '.versions[] | .num' || true)
echo ${VERSIONS_INFO}
# If this version is already published...
if echo ${VERSIONS_INFO} | jq -r ".versions[] | .num | . == \"${VERSION}\"" | grep true; then
echo "The version '${VERSION}' of '${NAME}' is already published."
if [ "${{ inputs.fail-if-version-published }}" == "true" ]; then
exit 1
else
echo "Skipping the rest of the action because inputs.fail-if-version-published is false."
exit 0
fi
fi
echo "Did not detect the version ${VERSION} to be already published."
echo "The list of known versions:"
echo $PUBLISHED_VERSIONS
# Set --dry-run flag, if configured
DRY_RUN=""
if [ "${{ inputs.dry-run }}" == "true" ]; then
DRY_RUN="--dry-run"
fi
cargo publish ${DRY_RUN} --token ${{ inputs.cargo-registry-token }}
48 changes: 48 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Release and Publish
on:
pull_request:
branches:
- main
- "release/[0-9]+.[0-9]+"
push:
branches:
- 'main'
- 'release/[0-9]+.[0-9]+'
jobs:
changelog:
runs-on: ubuntu-latest
steps:
- name: "✏️ Generate release changelog"
uses: heinrichreimer/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}

cargo-publish:
name: Cargo publish
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Determine which flags to use on cargo publish
id: cargo_flags
run: |
set -x
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
if [ "${BRANCH_NAME}" == "main" ]; then
echo "dry_run=false" >> $GITHUB_OUTPUT
echo "fail_if_version_published=false" >> $GITHUB_OUTPUT
elif [[ "${BRANCH_NAME}" == release/* ]]; then
echo "dry_run=false" >> $GITHUB_OUTPUT
echo "fail_if_version_published=false" >> $GITHUB_OUTPUT
else
echo "dry_run=true" >> $GITHUB_OUTPUT
echo "fail_if_version_published=true" >> $GITHUB_OUTPUT
fi
- name: Publish or validate
uses: ./.github/actions/publish-crate
with:
dry-run: ${{ steps.cargo_flags.outputs.dry_run }}
fail-if-version-published: ${{ steps.cargo_flags.outputs.fail_if_version_published }}
cargo-registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
84 changes: 84 additions & 0 deletions .github/workflows/rust-lint-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: tembo-telemetry ci workflow

defaults:
run:
shell: bash
on:
push:
branches:
- main
pull_request:
branches:
- main

env:
# Disable incremental compilation.
#
# Incremental compilation is useful as part of an edit-build-test-edit cycle,
# as it lets the compiler avoid recompiling code that hasn't changed. However,
# on CI, we're not making small edits; we're almost always building the entire
# project from scratch. Thus, incremental compilation on CI actually
# introduces *additional* overhead to support making future builds
# faster...but no future builds will ever occur in any given CI environment.
#
# See https://matklad.github.io/2021/09/04/fast-rust-builds.html#ci-workflow
# for details.
CARGO_INCREMENTAL: 0
# Allow more retries for network requests in cargo (downloading crates) and
# rustup (installing toolchains). This should help to reduce flaky CI failures
# from transient network timeouts or other issues.
CARGO_NET_RETRY: 10
RUSTUP_MAX_RETRIES: 10
# Don't emit giant backtraces in the CI logs.
RUST_BACKTRACE: short

jobs:
check:
name: cargo check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- name: Check
run: cargo check --all --tests --benches

style:
name: cargo fmt
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
components: rustfmt
- name: rustfmt
run: cargo +nightly fmt --all -- --check

warnings:
# Check for any warnings. This is informational and thus is allowed to fail.
runs-on: ubuntu-latest
needs: check
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all --examples --tests --benches -- -D warnings

tests:
name: Run tests
runs-on: ubuntu-22.04
needs: style
steps:
- uses: actions/checkout@v2
- name: Install Rust stable toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: run all tests
run: cargo test
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/target
/Cargo.lock
examples/*/Cargo.lock
examples/*/target
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
async-trait = "0.1"
tracing = "0.1"
opentelemetry = { version = "0.19", features = ["rt-tokio", "metrics"] }
opentelemetry = { version = "0.19", features = ["rt-tokio-current-thread", "metrics"] }
opentelemetry-otlp = { version = "0.12", features = ["tonic", "trace", "metrics", "tls", "tls-roots"] }
tracing-bunyan-formatter = "0.3"
tracing-subscriber = { version = "0.3", features = ["registry", "env-filter", "fmt"] }
Expand Down
11 changes: 11 additions & 0 deletions examples/actix-basic-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "trace"
version = "0.1.0"
edition = "2021"

[dependencies]
actix-web = "4"
tracing = "0.1"
tracing-actix-web = { version="0.7", features = ["opentelemetry_0_19"] }
opentelemetry = { version = "0.19", features = ["metrics"] }
tembo-telemetry = { version = "0.1.0", path = "../../" }
66 changes: 66 additions & 0 deletions examples/actix-basic-http/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use actix_web::{get, web, App, HttpResponse, HttpServer, Responder};
use opentelemetry::global;
use std::io;
use tembo_telemetry::{TelemetryConfig, TelemetryInit};
use tracing::*;
use tracing_actix_web::TracingLogger;

const TRACER_NAME: &str = "tembo.io/trace";

#[get("/")]
#[instrument]
async fn hello(tc: web::Data<TelemetryConfig>) -> impl Responder {
let trace_id = tc.get_trace_id();
Span::current().record("trace_id", &field::display(&trace_id));
info!("Received request for /hello");
HttpResponse::Ok().json("Hello World!")
}

#[actix_web::main]
async fn main() -> io::Result<()> {
// Setup Telemetry and Logging
let telemetry_config = if let Ok(otlp_endpoint) = std::env::var("OPENTELEMETRY_ENDPOINT_URL") {
let tc = TelemetryConfig {
app_name: std::env::var("CARGO_BIN_NAME").unwrap_or_else(|_| "basic".to_string()),
env: std::env::var("ENV").unwrap_or_else(|_| "development".to_string()),
endpoint_url: Some(otlp_endpoint),
tracer_id: Some(TRACER_NAME.to_string()),
};
println!("{:?}", tc);
let _telemetry = TelemetryInit::init(&tc).await;
tc
} else {
let tc = TelemetryConfig {
app_name: std::env::var("ENV").unwrap_or_else(|_| "development".to_string()),
env: std::env::var("ENV").unwrap_or_else(|_| "development".to_string()),
endpoint_url: None,
tracer_id: Some(TRACER_NAME.to_string()),
};
let _telemetry = TelemetryInit::init(&tc).await;
tc
};

let server_bind_address = "0.0.0.0:3001".to_string();
let server = HttpServer::new({
let telemerty_config = web::Data::new(telemetry_config);
move || {
App::new()
.app_data(telemerty_config.clone())
.service(hello)
.wrap(TracingLogger::default())
}
})
.bind(server_bind_address.clone())?
.shutdown_timeout(5)
.run();

info!(
"Starting HTTP server at https://{}/",
server_bind_address.clone()
);
server.await?;

global::shutdown_tracer_provider();

Ok(())
}
Empty file.
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

use async_trait::async_trait;
use opentelemetry::{
global, sdk::propagation::TraceContextPropagator, sdk::trace, sdk::Resource, trace::TraceId,
KeyValue,
global, runtime::TokioCurrentThread, sdk::propagation::TraceContextPropagator, sdk::trace,
sdk::Resource, trace::TraceId, KeyValue,
};
use opentelemetry_otlp::WithExportConfig;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
Expand Down Expand Up @@ -93,7 +93,7 @@ impl TelemetryInit for TelemetryConfig {
.tracing()
.with_exporter(exporter)
.with_trace_config(trace_config)
.install_batch(opentelemetry::runtime::Tokio)?;
.install_batch(TokioCurrentThread)?;
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
if self.env == "development" {
let logger = fmt::layer().compact();
Expand Down

0 comments on commit b63cec5

Please sign in to comment.