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

Add dummy feature for non supported platforms #39

Merged
merged 3 commits into from
Jan 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
2 changes: 1 addition & 1 deletion .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
security_audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: |
Expand Down
31 changes: 30 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runner: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: |
Expand All @@ -29,3 +29,32 @@ jobs:
- name: Test
run: |
cargo build --release --all-features

build-dummy:
strategy:
matrix:
# Targets that this crate does NOT support
target: [x86_64-unknown-freebsd]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Update local toolchain
run: |
rustup update
rustup target add ${{ matrix.target }}
- name: Toolchain info
run: |
rustc --version
cargo --version --verbose
- name: Test
run: |
cargo build --release --features dummy --target ${{ matrix.target }}
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: |
Expand All @@ -32,7 +32,7 @@ jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: |
Expand Down Expand Up @@ -63,7 +63,7 @@ jobs:
RUNNER: ${{ matrix.runner }}
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: |
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ keywords = [ "cross-platform", "metrics", "prometheus", "open-metrics", "process
[package.metadata.docs.rs]
all-features = true

[features]
# Enable a `dummy` collector that always return an empty `Metrics` for non supported platforms
dummy = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Go ([client_golang]) provides.
| `process_start_time_seconds` | x | x | x |
| `process_threads` | x | x | |

Please note that if you only need to compile this crate on non-supported
platforms, you can use the `dummy` feature. Enabling this feature activates a
dummy collector, which returns an empty `Metrics`.

[client_golang]: https://github.com/prometheus/client_golang

## Usage
Expand Down Expand Up @@ -115,6 +119,14 @@ async fn main() {
}
```

## Features

This crate offers the following features:

| Feature Name | Description |
| ------------ | ------------------------------------------------------------------------------------- |
| `dummy` | Enables a dummy collector that returns an empty `Metrics` on non-supported platforms. |

## Difference from [metrics-process-promstyle]

It appears that [metrics-process-promstyle] only supports Linux, but this crate
Expand Down
50 changes: 34 additions & 16 deletions src/collector.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#[cfg(target_os = "macos")]
mod macos;
#[cfg_attr(target_os = "macos", path = "collector/macos.rs")]
#[cfg_attr(target_os = "linux", path = "collector/linux.rs")]
#[cfg_attr(target_os = "windows", path = "collector/windows.rs")]
#[allow(unused_attributes)]
#[cfg_attr(feature = "dummy", path = "collector/dummy.rs")]
mod collector;

#[cfg(target_os = "linux")]
mod linux;
#[cfg(all(
not(feature = "dummy"),
not(any(target_os = "macos", target_os = "linux", target_os = "windows"))
))]
compile_error!(
"A feature \"dummy\" must be enabled to compile this crate on non supported platforms."
);

#[cfg(target_os = "windows")]
#[path = "collector/windows.rs"]
mod win;
pub use collector::collect;

/// Process metrics
/// https://prometheus.io/docs/instrumenting/writing_clientlibs/#process-metrics
Expand All @@ -32,15 +39,6 @@ pub struct Metrics {
pub threads: Option<u64>,
}

#[cfg(target_os = "macos")]
pub use macos::collect;

#[cfg(target_os = "linux")]
pub use linux::collect;

#[cfg(target_os = "windows")]
pub use win::collect;

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -54,6 +52,7 @@ mod tests {
}
}

#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
#[test]
fn test_collect_internal_ok() {
fibonacci(40);
Expand All @@ -70,4 +69,23 @@ mod tests {
#[cfg(not(target_os = "windows"))]
assert_matches!(m.threads, Some(_));
}

#[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "linux"))]
#[cfg(not(target_os = "windows"))]
#[cfg(feature = "dummy")]
#[test]
fn test_collect_internal_ok_dummy() {
fibonacci(40);
let m = collect();
dbg!(&m);
assert_matches!(m.cpu_seconds_total, None);
assert_matches!(m.open_fds, None);
assert_matches!(m.max_fds, None);
assert_matches!(m.virtual_memory_bytes, None);
assert_matches!(m.virtual_memory_max_bytes, None);
assert_matches!(m.resident_memory_bytes, None);
assert_matches!(m.start_time_seconds, None);
assert_matches!(m.threads, None);
}
}
5 changes: 5 additions & 0 deletions src/collector/dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use super::Metrics;

pub fn collect() -> Metrics {
Metrics::default()
}