Skip to content

Commit

Permalink
feat: add opentelemetry-collector service
Browse files Browse the repository at this point in the history
feat: add opentelemetry-collector service

feat: add opentelemetry-collector service

feat: add opentelemetry-collector service
  • Loading branch information
tsirysndr committed Jul 19, 2024
1 parent 8ffff62 commit 5d50119
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/opentelemetry-collector.yml
Original file line number Diff line number Diff line change
@@ -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 }}
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ default-members = [
"nats",
"nginx",
"nsq",
"opentelemetry-collector",
"php",
"postgres",
"quickwit",
Expand Down Expand Up @@ -60,6 +61,7 @@ members = [
"nats",
"nginx",
"nsq",
"opentelemetry-collector",
"php",
"postgres",
"quickwit",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
20 changes: 20 additions & 0 deletions opentelemetry-collector/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
authors = [
"Tsiry Sandratraina <tsiry.sndr@fluentci.io>",
]
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"
19 changes: 19 additions & 0 deletions opentelemetry-collector/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2024 Tsiry Sandratraina <tsiry.sndr@fluentci.io>

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.
54 changes: 54 additions & 0 deletions opentelemetry-collector/README.md
Original file line number Diff line number Diff line change
@@ -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
```
11 changes: 11 additions & 0 deletions opentelemetry-collector/fluentci.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
authors = [
"Tsiry Sandratraina <tsiry.sndr@fluentci.io>",
]
description = "Opentelemetry Collector Plugin for FluentCI"
keywords = [
"opentelemetry",
]
license = "MIT"
name = "opentelemetry-collector"
version = "0.1.0"
35 changes: 35 additions & 0 deletions opentelemetry-collector/otel-config.yaml.template
Original file line number Diff line number Diff line change
@@ -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]
60 changes: 60 additions & 0 deletions opentelemetry-collector/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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<String, Error> {
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)
}
49 changes: 49 additions & 0 deletions opentelemetry-collector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use extism_pdk::*;
use fluentci_pdk::dag;

pub mod helpers;

#[plugin_fn]
pub fn start(_args: String) -> FnResult<String> {
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<String> {
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)
}
1 change: 1 addition & 0 deletions opentelemetry-collector/target

0 comments on commit 5d50119

Please sign in to comment.