From 5d501197f0bff94c38c8a35c8f95f8de6f714d47 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Fri, 19 Jul 2024 19:35:07 +0000 Subject: [PATCH] feat: add opentelemetry-collector service feat: add opentelemetry-collector service feat: add opentelemetry-collector service feat: add opentelemetry-collector service --- .github/workflows/opentelemetry-collector.yml | 35 +++++++++++ Cargo.lock | 10 ++++ Cargo.toml | 2 + README.md | 1 + opentelemetry-collector/Cargo.toml | 20 +++++++ opentelemetry-collector/LICENSE | 19 ++++++ opentelemetry-collector/README.md | 54 +++++++++++++++++ opentelemetry-collector/fluentci.toml | 11 ++++ .../otel-config.yaml.template | 35 +++++++++++ opentelemetry-collector/src/helpers.rs | 60 +++++++++++++++++++ opentelemetry-collector/src/lib.rs | 49 +++++++++++++++ opentelemetry-collector/target | 1 + 12 files changed, 297 insertions(+) create mode 100644 .github/workflows/opentelemetry-collector.yml create mode 100644 opentelemetry-collector/Cargo.toml create mode 100644 opentelemetry-collector/LICENSE create mode 100644 opentelemetry-collector/README.md create mode 100644 opentelemetry-collector/fluentci.toml create mode 100644 opentelemetry-collector/otel-config.yaml.template create mode 100644 opentelemetry-collector/src/helpers.rs create mode 100644 opentelemetry-collector/src/lib.rs create mode 120000 opentelemetry-collector/target diff --git a/.github/workflows/opentelemetry-collector.yml b/.github/workflows/opentelemetry-collector.yml new file mode 100644 index 0000000..9091a6d --- /dev/null +++ b/.github/workflows/opentelemetry-collector.yml @@ -0,0 +1,35 @@ +name: ci +on: + push: + branches: + - main + paths: + - opentelemetry-collector/** + - .github/workflows/opentelemetry-collector.yml +jobs: + opentelemetry-collector-test: + strategy: + matrix: + os: [ubuntu-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v3 + - name: Setup Fluent CI + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: rust + args: | + target_add wasm32-unknown-unknown + build --release --target wasm32-unknown-unknown + working-directory: opentelemetry-collector + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Start opentelemetry-collector Server + run: | + fluentci run target/wasm32-unknown-unknown/release/opentelemetry_collector.wasm start + fluentci run target/wasm32-unknown-unknown/release/opentelemetry_collector.wasm stop + working-directory: opentelemetry-collector + env: + GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NIX_CONFIG: extra-access-tokens = github.com=${{ secrets.GH_ACCESS_TOKEN }} diff --git a/Cargo.lock b/Cargo.lock index 278fe10..23335df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,6 +446,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "opentelemetry-collector" +version = "0.1.0" +dependencies = [ + "anyhow", + "extism-pdk", + "fluentci-pdk", + "fluentci-types", +] + [[package]] name = "paste" version = "1.0.15" diff --git a/Cargo.toml b/Cargo.toml index 1ac0f83..9094bfe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ default-members = [ "nats", "nginx", "nsq", + "opentelemetry-collector", "php", "postgres", "quickwit", @@ -60,6 +61,7 @@ members = [ "nats", "nginx", "nsq", + "opentelemetry-collector", "php", "postgres", "quickwit", diff --git a/README.md b/README.md index 1c17961..5c550d7 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ fluentci run --wasm postgres start | [nats](./nats) | A simple, secure and high performance messaging system | | [nginx](./nginx) | HTTP and reverse proxy server | | [nsq](./nsq) | A realtime distributed messaging platform | +| [opentelemetry-collector](./opentelemetry-collector) | OpenTelemetry Collector superset with additional collectors | | [postgres](./postgres) | A database management system that is object-relational | | [php-fpm](./php) | PHP FastCGI Process Manager | | [quickwit](./quickwit) | A distributed search engine | diff --git a/opentelemetry-collector/Cargo.toml b/opentelemetry-collector/Cargo.toml new file mode 100644 index 0000000..0b7f429 --- /dev/null +++ b/opentelemetry-collector/Cargo.toml @@ -0,0 +1,20 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Opentelemetry Collector plugin for FluentCI" +edition = "2021" +license = "MIT" +name = "opentelemetry-collector" +version = "0.1.0" + +[lib] +crate-type = [ + "cdylib", +] + +[dependencies] +anyhow = "1.0.82" +extism-pdk = "1.1.0" +fluentci-pdk = "0.2.1" +fluentci-types = "0.1.7" diff --git a/opentelemetry-collector/LICENSE b/opentelemetry-collector/LICENSE new file mode 100644 index 0000000..515dd5f --- /dev/null +++ b/opentelemetry-collector/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Tsiry Sandratraina + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/opentelemetry-collector/README.md b/opentelemetry-collector/README.md new file mode 100644 index 0000000..4564157 --- /dev/null +++ b/opentelemetry-collector/README.md @@ -0,0 +1,54 @@ +# Opentelemetry Collector Plugin + +[![fluentci pipeline](https://shield.fluentci.io/x/opentelemetry-collector)](https://pkg.fluentci.io/opentelemetry-collector) +[![ci](https://github.com/fluentci-io/services/actions/workflows/opentelemetry-collector.yml/badge.svg)](https://github.com/fluentci-io/services/actions/workflows/opentelemetry-collector.yml) + +Opentelemetry service plugin for FluentCI. + +## 🚀 Usage + +Add the following command to your CI configuration file: + +```bash +fluentci run --wasm opentelemetry-collector start +``` + +## Functions + +| Name | Description | +| ------ | --------------------------------------------| +| start | Start Opentelemetry Collector | +| stop | Stop Opentelemetry Collector | + +## Code Usage + +Add `fluentci-pdk` crate to your `Cargo.toml`: + +```toml +[dependencies] +fluentci-pdk = "0.2.1" +``` + +Use the following code to call the plugin: + +```rust +use fluentci_pdk::dag; + +// ... + +dag().call("https://pkg.fluentci.io/opentelemetry-collector@v0.1.0?wasm=1", "start", vec![])?; +``` + +## 📚 Examples + +Github Actions: + +```yaml +- name: Setup Fluent CI CLI + uses: fluentci-io/setup-fluentci@v5 + with: + wasm: true + plugin: opentelemetry-collector + args: | + start +``` diff --git a/opentelemetry-collector/fluentci.toml b/opentelemetry-collector/fluentci.toml new file mode 100644 index 0000000..b74ff8a --- /dev/null +++ b/opentelemetry-collector/fluentci.toml @@ -0,0 +1,11 @@ +[package] +authors = [ + "Tsiry Sandratraina ", +] +description = "Opentelemetry Collector Plugin for FluentCI" +keywords = [ + "opentelemetry", +] +license = "MIT" +name = "opentelemetry-collector" +version = "0.1.0" diff --git a/opentelemetry-collector/otel-config.yaml.template b/opentelemetry-collector/otel-config.yaml.template new file mode 100644 index 0000000..b069742 --- /dev/null +++ b/opentelemetry-collector/otel-config.yaml.template @@ -0,0 +1,35 @@ +receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:$OPENTELEMETRY_PORT + http: + endpoint: 0.0.0.0:$OPENTELEMETRY_HTTP_PORT +processors: + batch: + +exporters: + otlp: + endpoint: otelcol:4317 + +extensions: + health_check: + endpoint: localhost:13133 + pprof: + zpages: + +service: + extensions: [health_check, pprof, zpages] + pipelines: + traces: + receivers: [otlp] + processors: [batch] + exporters: [otlp] + metrics: + receivers: [otlp] + processors: [batch] + exporters: [otlp] + logs: + receivers: [otlp] + processors: [batch] + exporters: [otlp] diff --git a/opentelemetry-collector/src/helpers.rs b/opentelemetry-collector/src/helpers.rs new file mode 100644 index 0000000..0b81d64 --- /dev/null +++ b/opentelemetry-collector/src/helpers.rs @@ -0,0 +1,60 @@ +use anyhow::Error; +use fluentci_pdk::dag; + +pub fn setup_flox() -> Result<(), Error> { + let os = dag().get_os()?; + if os == "macos" { + dag() + .pipeline("setup-flox")? + .with_exec(vec![r#"type brew > /dev/null 2> /dev/null || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)""#])? + .with_exec(vec!["type flox > /dev/null 2> /dev/null || brew install flox"])? + .stdout()?; + } + Ok(()) +} + +pub fn setup() -> Result { + setup_flox()?; + dag() + .pipeline("setup")? + .with_exec(vec!["mkdir", "-p", ".fluentci/opentelemetry-collector"])? + .stdout()?; + + let pwd = dag().get_env("PWD")?; + let otel_port = dag().get_env("OPENTELEMETRY_PORT")?; + let otel_http_port = dag().get_env("OPENTELEMETRY_HTTP_PORT")?; + let otel_config = dag().get_env("OPENTELEMETRY_CONFIG")?; + + if otel_port.is_empty() { + dag().set_envs(vec![("OPENTELEMETRY_PORT".into(), "4317".into())])?; + } + if otel_http_port.is_empty() { + dag().set_envs(vec![("OPENTELEMETRY_HTTP_PORT".into(), "4318".into())])?; + } + + if otel_config.is_empty() { + dag().set_envs(vec![( + "OPENTELEMETRY_CONFIG".into(), + format!("{}/otel-config.yaml", pwd), + )])?; + } + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/opentelemetry-collector")? + .with_exec(vec![ + "flox", + "install", + "opentelemetry-collector-contrib", + "overmind", + "tmux", + ])? + .with_exec(vec!["[ -f otel-config.yaml.template ] || pkgx wget https://raw.githubusercontent.com/fluentci-io/services/main/opentelemetry-collector/otel-config.yaml.template"])? + .with_exec(vec!["[ -f $OPENTELEMETRY_CONFIG ] || pkgx envsubst < otel-config.yaml.template > $OPENTELEMETRY_CONFIG "])? + .with_exec(vec![ + "grep -q opentelemetry-collector: Procfile || echo -e 'opentelemetry-collector: otelcontribcol --config $OPENTELEMETRY_CONFIG \\n' >> Procfile", + ])? + .stdout()?; + + Ok(stdout) +} diff --git a/opentelemetry-collector/src/lib.rs b/opentelemetry-collector/src/lib.rs new file mode 100644 index 0000000..57c33a0 --- /dev/null +++ b/opentelemetry-collector/src/lib.rs @@ -0,0 +1,49 @@ +use extism_pdk::*; +use fluentci_pdk::dag; + +pub mod helpers; + +#[plugin_fn] +pub fn start(_args: String) -> FnResult { + helpers::setup()?; + + let port = dag().get_env("OPENTELEMETRY_PORT")?; + let http_port = dag().get_env("OPENTELEMETRY_HTTP_PORT")?; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/opentelemetry-collector")? + .with_exec(vec!["overmind", "--version"])? + .with_exec(vec!["type", "overmind"])? + .with_exec(vec!["type", "otelcontribcol"])? + .with_exec(vec!["otelcontribcol", "--version"])? + .with_exec(vec![ + "echo -e \"OpenTelemetry Collector starting on port $OPENTELEMETRY_PORT\"", + ])? + .with_exec(vec![ + "overmind start -f Procfile --daemonize || flox activate -- overmind restart opentelemetry-collector", + ])? + .wait_on(port.parse()?, None)? + .wait_on(http_port.parse()?, None)? + .with_exec(vec!["overmind", "status"])? + .stdout()?; + Ok(stdout) +} + +#[plugin_fn] +pub fn stop(args: String) -> FnResult { + helpers::setup()?; + + let args = if args.is_empty() { + "opentelemetry-collector".to_string() + } else { + args + }; + + let stdout = dag() + .flox()? + .with_workdir(".fluentci/opentelemetry-collector")? + .with_exec(vec!["overmind", "stop", &args])? + .stdout()?; + Ok(stdout) +} diff --git a/opentelemetry-collector/target b/opentelemetry-collector/target new file mode 120000 index 0000000..78bc337 --- /dev/null +++ b/opentelemetry-collector/target @@ -0,0 +1 @@ +../target \ No newline at end of file