Skip to content

Commit 8291a49

Browse files
RamResosandhose
andauthored
resolves #19403 Report the rust compiler version used in the prometheus metrics (#19643)
# What is done? - resolves #19403 - Adds build-time Rust compiler detection and captures the rustc --version value during the build. - Exposes the captured compiler version from the Rust extension via a new Python-callable function. - Exports a new Prometheus metric for rustc version. # How to test? - compile `poetry install` - add `enable_metrics: true` and ```yaml resources: - compress: false names: - client - federation - metrics ``` to homeserver.yaml - start synapse - find the rustc version at `http://localhost:8008/_synapse/metrics` ### Pull Request Checklist <!-- Please read https://element-hq.github.io/synapse/latest/development/contributing_guide.html before submitting your pull request --> * [x] Pull request is based on the develop branch * [x] Pull request includes a [changelog file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog). The entry should: - Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from `EventStore` to `EventWorkerStore`.". - Use markdown where necessary, mostly for `code blocks`. - End with either a period (.) or an exclamation mark (!). - Start with a capital letter. - Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry. * [x] [Code style](https://element-hq.github.io/synapse/latest/code_style.html) is correct (run the [linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters)) --------- Co-authored-by: Quentin Gliech <quenting@element.io>
1 parent 62f23fe commit 8291a49

7 files changed

Lines changed: 42 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

changelog.d/19643.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Report the Rust compiler version used in the Prometheus metrics. Contributed by Noah Markert.

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ default = ["extension-module"]
6464
[build-dependencies]
6565
blake2 = "0.10.4"
6666
hex = "0.4.3"
67+
rustc_version = "0.4.1"

rust/build.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use std::path::PathBuf;
99

1010
use blake2::{Blake2b512, Digest};
11+
use rustc_version::version_meta;
1112

1213
fn main() -> Result<(), std::io::Error> {
1314
let mut dirs = vec![PathBuf::from("src")];
@@ -48,6 +49,11 @@ fn main() -> Result<(), std::io::Error> {
4849
let hex_digest = hex::encode(hasher.finalize());
4950
println!("cargo:rustc-env=SYNAPSE_RUST_DIGEST={hex_digest}");
5051

52+
let rustc_version = version_meta()
53+
.map(|v| v.short_version_string)
54+
.unwrap_or_else(|_| "unknown".to_string());
55+
println!("cargo:rustc-env=SYNAPSE_RUSTC_VERSION={}", rustc_version,);
56+
5157
// The default rules don't pick up trivial changes to the workspace config
5258
// files, but we need to rebuild if those change to pick up the changed
5359
// hashes.

rust/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ fn get_rust_file_digest() -> &'static str {
3030
env!("SYNAPSE_RUST_DIGEST")
3131
}
3232

33+
/// Returns the `rustc` version used when this native module was built.
34+
///
35+
/// This value is embedded at build time, so it can be exported as a prometheus metrics.
36+
#[pyfunction]
37+
pub fn get_rustc_version() -> &'static str {
38+
env!("SYNAPSE_RUSTC_VERSION")
39+
}
40+
3341
/// Formats the sum of two numbers as string.
3442
#[pyfunction]
3543
#[pyo3(text_signature = "(a, b, /)")]
@@ -50,6 +58,7 @@ fn reset_logging_config() {
5058
fn synapse_rust(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
5159
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
5260
m.add_function(wrap_pyfunction!(get_rust_file_digest, m)?)?;
61+
m.add_function(wrap_pyfunction!(get_rustc_version, m)?)?;
5362
m.add_function(wrap_pyfunction!(reset_logging_config, m)?)?;
5463

5564
acl::register_module(py, m)?;

synapse/metrics/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,17 @@
6262
import synapse.metrics._reactor_metrics # noqa: F401
6363
from synapse.metrics._gc import MIN_TIME_BETWEEN_GCS, install_gc_manager
6464
from synapse.metrics._types import Collector
65+
from synapse.synapse_rust import get_rustc_version
6566
from synapse.types import StrSequence
6667
from synapse.util import SYNAPSE_VERSION
6768

6869
logger = logging.getLogger(__name__)
6970

7071
METRICS_PREFIX = "/_synapse/metrics"
7172

73+
# Rust version used for compilation
74+
RUSTC_VERSION = get_rustc_version()
75+
7276
HAVE_PROC_SELF_STAT = os.path.exists("/proc/self/stat")
7377

7478
SERVER_NAME_LABEL = "server_name"
@@ -672,15 +676,17 @@ def collect(self) -> Iterable[Metric]:
672676
# consider this process-level because all Synapse homeservers running in the process
673677
# will use the same Synapse version.
674678
build_info = Gauge( # type: ignore[missing-server-name-label]
675-
"synapse_build_info", "Build information", ["pythonversion", "version", "osversion"]
679+
"synapse_build_info",
680+
"Build information",
681+
["pythonversion", "version", "osversion", "rustcversion"],
676682
)
677683
build_info.labels(
678684
" ".join([platform.python_implementation(), platform.python_version()]),
679685
SYNAPSE_VERSION,
680686
" ".join([platform.system(), platform.release()]),
687+
RUSTC_VERSION,
681688
).set(1)
682689

683-
684690
synapse_server_name_info = Gauge(
685691
"synapse_server_name_info",
686692
"Maps Synapse `server_name`s to the `instance`s they're hosted on",

synapse/synapse_rust/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
def sum_as_string(a: int, b: int) -> str: ...
22
def get_rust_file_digest() -> str: ...
33
def reset_logging_config() -> None: ...
4+
def get_rustc_version() -> str: ...

0 commit comments

Comments
 (0)