-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathremote_config_fetch.rs
More file actions
102 lines (96 loc) · 3.99 KB
/
remote_config_fetch.rs
File metadata and controls
102 lines (96 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0
use datadog_remote_config::fetch::{ConfigInvariants, ConfigOptions, SingleChangesFetcher};
use datadog_remote_config::file_change_tracker::{Change, FilePath};
use datadog_remote_config::file_storage::ParsedFileStorage;
use datadog_remote_config::RemoteConfigProduct::ApmTracing;
use datadog_remote_config::{RemoteConfigParsedData, Target};
use libdd_common::tag::Tag;
use libdd_common::Endpoint;
use std::time::Duration;
use tokio::time::sleep;
const RUNTIME_ID: &str = "23e76587-5ae1-410c-a05c-137cae600a10";
const SERVICE: &str = "testservice";
const ENV: &str = "testenv";
const VERSION: &str = "1.2.3";
#[tokio::main(flavor = "current_thread")]
async fn main() {
// SingleChangesFetcher is ideal for a single static (runtime_id, service, env, version) tuple
// Otherwise a SharedFetcher (or even a MultiTargetFetcher for a potentially high number of
// targets) for multiple targets is needed. These can be manually wired together with a
// ChangeTracker to keep track of changes. The SingleChangesTracker does it for you.
let mut fetcher = SingleChangesFetcher::new(
// Use SimpleFileStorage if you desire just the raw, unparsed contents
// (e.g. to do processing directly in your language)
// For more complicated use cases, like needing to store data in shared memory, a custom
// FileStorage implementation is recommended
ParsedFileStorage::default(),
Target {
service: SERVICE.to_string(),
env: ENV.to_string(),
app_version: VERSION.to_string(),
tags: vec![Tag::new("test", "value").unwrap()],
process_tags: vec![],
},
RUNTIME_ID.to_string(),
ConfigOptions {
invariants: ConfigInvariants {
language: "awesomelang".to_string(),
tracer_version: "99.10.5".to_string(),
endpoint: Endpoint {
url: http::Uri::from_static("http://localhost:8126"),
api_key: None,
timeout_ms: 5000, // custom timeout, defaults to 3 seconds
test_token: None,
..Default::default()
},
},
products: vec![ApmTracing],
capabilities: vec![],
},
);
loop {
match fetcher.fetch_changes().await {
Ok(changes) => {
println!("Got {} changes:", changes.len());
for change in changes {
match change {
Change::Add(file) => {
println!("Added file: {} (version: {})", file.path(), file.version());
print_file_contents(&file.contents());
}
Change::Update(file, _) => {
println!(
"Got update for file: {} (version: {})",
file.path(),
file.version()
);
print_file_contents(&file.contents());
}
Change::Remove(file) => {
println!("Removing file {}", file.path());
}
}
}
}
Err(e) => {
eprintln!("Fetch failed with {e}");
}
}
sleep(Duration::from_secs(1)).await;
}
}
fn print_file_contents(contents: &anyhow::Result<Option<Box<dyn RemoteConfigParsedData>>>) {
// Note: these contents may be large. Do not actually print it fully in a non-dev env.
match contents {
Ok(Some(data)) => {
println!("File contents: {data:?}");
}
Ok(None) => {
println!("Unregistered product, no parsed data");
}
Err(e) => {
println!("Failed parsing file: {e:?}");
}
}
}