12 releases
0.11.11 | Sep 16, 2024 |
---|---|
0.11.10 | Sep 16, 2024 |
0.11.6 | Aug 23, 2024 |
#905 in Web programming
1MB
14K
SLoC
OpenTelemetry Google Cloud Monitoring Exporter
This library provides support for exporting metrics to Google Cloud Monitoring.
For resource detection see opentelemetry-resourcedetector-gcp-rust.
Installation
cargo add opentelemetry_gcloud_monitoring_exporter
- exporter
cargo add opentelemetry_resourcedetector_gcp_rust
- gcp resource detection
or add to cargo.toml
[dependencies]
opentelemetry_gcloud_monitoring_exporter = { version = "0.11", features = [
"tokio",
"opentelemetry_0_24",
"gcp_auth",
] }
tokio = { version = "1.0", features = ["full"] }
opentelemetry = { version = "0.24", features = ["metrics"] }
opentelemetry_sdk = { version = "0.24", features = ["metrics", "rt-tokio"] }
opentelemetry_resourcedetector_gcp_rust = "0.11"
Usage
use opentelemetry::{metrics::MeterProvider as _, KeyValue};
use opentelemetry_gcloud_monitoring_exporter::{
GCPMetricsExporter, GCPMetricsExporterConfig, MonitoredResourceDataConfig,
};
use opentelemetry_resourcedetector_gcp_rust::GoogleCloudResourceDetector;
use opentelemetry_sdk::{
metrics::{PeriodicReader, SdkMeterProvider},
runtime, Resource,
};
use serde_json::json;
use std::collections::HashMap;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut cfg = GCPMetricsExporterConfig::default();
cfg.prefix = "custom.googleapis.com/test_service".to_string();
let exporter = GCPMetricsExporter::new_gcp_auth(cfg).await?;
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
let gcp_detector = GoogleCloudResourceDetector::new().await;
// if we deploy to cloud run or vm instance in gcp we should specify namespace
// if we don't have namespace we can specify it how 'default'
let res0 = Resource::new(vec![KeyValue::new("service.namespace", "default")]);
let res = Resource::default().merge(&gcp_detector.get_resource());
let res = res.merge(&res0);
SdkMeterProvider::builder()
.with_resource(res)
.with_reader(reader)
.build();
let meter = meter_provider.meter("user-event-test");
let counter = meter
.f64_counter("counter_f64_test")
.with_description("test_decription")
.with_unit("test_unit")
.init();
loop {
// Record measurements using the Counter instrument.
counter.add(
1.0,
&[
KeyValue::new("mykey1", "myvalue1"),
KeyValue::new("mykey2", "myvalue2"),
],
);
tokio::time::sleep(Duration::from_millis(300)).await;
}
}
Customize metric resource in google monitoring
let mut cfg = GCPMetricsExporterConfig::default();
cfg.prefix = "custom.googleapis.com/test_service".to_string();
// customize metric resource in google monitoring
cfg.custom_monitored_resource_data = Some(
// https://cloud.google.com/monitoring/api/resources#tag_global
MonitoredResourceDataConfig {
r#type: "global".to_string(),
labels: HashMap::from([
("project_id".to_string(), "my-project".to_string()),
]),
},
);
let exporter = GCPMetricsExporter::new_gcp_auth(cfg).await?;
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
SdkMeterProvider::builder()
.with_reader(reader)
.build();
References
Test cases from this repo
Dependencies
~21–36MB
~676K SLoC