-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathmain.rs
More file actions
253 lines (203 loc) · 8.54 KB
/
main.rs
File metadata and controls
253 lines (203 loc) · 8.54 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
Copyright 2025 The Hyperlight Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#![allow(clippy::disallowed_macros)]
//use opentelemetry_sdk::resource::ResourceBuilder;
use opentelemetry_sdk::trace::SdkTracerProvider;
use tracing::{Level, span};
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
extern crate hyperlight_host;
use std::error::Error;
use std::io::stdin;
use std::sync::{Arc, Barrier, Mutex};
use std::thread::{JoinHandle, spawn};
use hyperlight_host::sandbox::uninitialized::UninitializedSandbox;
use hyperlight_host::{GuestBinary, Result as HyperlightResult};
use hyperlight_testing::simple_guest_as_string;
use opentelemetry::trace::TracerProvider;
use opentelemetry::{KeyValue, global};
use opentelemetry_otlp::{Protocol, SpanExporter, WithExportConfig};
//use opentelemetry_sdk::runtime::Tokio;
use opentelemetry_sdk::Resource;
use opentelemetry_semantic_conventions::attribute::SERVICE_VERSION;
use tracing_subscriber::EnvFilter;
use uuid::Uuid;
const ENDPOINT_ADDR: &str = "http://localhost:4318/v1/traces";
fn fn_writer(_msg: String) -> HyperlightResult<i32> {
Ok(0)
}
// Shows how to send tracing events to an OTLP collector using the opentelemetry crate.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let provider = init_tracing_subscriber(ENDPOINT_ADDR)?;
run_example(true)?;
provider.shutdown()?;
Ok(())
}
fn init_tracing_subscriber(
addr: &str,
) -> Result<SdkTracerProvider, Box<dyn Error + Send + Sync + 'static>> {
let exporter = SpanExporter::builder()
.with_http()
.with_protocol(Protocol::HttpBinary)
.with_endpoint(addr)
.build()?;
let version = KeyValue::new(SERVICE_VERSION, env!("CARGO_PKG_VERSION"));
let resource = Resource::builder()
.with_service_name("hyperlight_otel_example")
.with_attribute(version)
.build();
let provider = opentelemetry_sdk::trace::SdkTracerProvider::builder()
.with_resource(resource)
.with_batch_exporter(exporter)
.build();
global::set_tracer_provider(provider.clone());
let tracer = provider.tracer("trace-demo");
let otel_layer = OpenTelemetryLayer::new(tracer);
// Try using the environment otherwise set default filters
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| {
EnvFilter::from_default_env()
.add_directive("hyperlight_host=info".parse().unwrap())
.add_directive("tracing=info".parse().unwrap())
});
tracing_subscriber::registry()
.with(filter)
.with(otel_layer)
.try_init()?;
Ok(provider)
}
fn run_example(wait_input: bool) -> HyperlightResult<()> {
// Get the path to a simple guest binary.
let hyperlight_guest_path =
simple_guest_as_string().expect("Cannot find the guest binary at the expected location.");
let mut join_handles: Vec<JoinHandle<HyperlightResult<()>>> = vec![];
// Construct a new span named "hyperlight otel tracing example" with INFO level.
let span = span!(Level::INFO, "hyperlight otel tracing example");
let _entered = span.enter();
let should_exit = Arc::new(Mutex::new(false));
for i in 0..10 {
let path = hyperlight_guest_path.clone();
let exit = Arc::clone(&should_exit);
let handle = spawn(move || -> HyperlightResult<()> {
while !*exit.try_lock().unwrap() {
// Construct a new span named "hyperlight tracing example thread" with INFO level.
let id = Uuid::new_v4();
let span = span!(
Level::INFO,
"hyperlight tracing example thread",
context = format!("Thread number {} GUID {}", i, id),
uuid = %id,
);
let _entered = span.enter();
// Create a new sandbox.
let mut usandbox =
UninitializedSandbox::new(GuestBinary::FilePath(path.clone()), None)?;
usandbox.register_print(fn_writer)?;
// Initialize the sandbox.
let mut multiuse_sandbox = usandbox.evolve()?;
// Call a guest function 5 times to generate some log entries.
for _ in 0..5 {
multiuse_sandbox
.call::<String>("Echo", "a".to_string())
.unwrap();
}
// Define a message to send to the guest.
let msg = "Hello, World!!\n".to_string();
// Call a guest function that calls the HostPrint host function 5 times to generate some log entries.
for _ in 0..5 {
multiuse_sandbox
.call::<i32>("PrintOutput", msg.clone())
.unwrap();
}
// Call a function that gets cancelled by the host function 5 times to generate some log entries.
const NUM_CALLS: i32 = 5;
let barrier = Arc::new(Barrier::new(2));
let barrier2 = barrier.clone();
let interrupt_handle = multiuse_sandbox.interrupt_handle();
let thread = std::thread::spawn(move || {
for _ in 0..NUM_CALLS {
barrier2.wait();
// Sleep for a short time to allow the guest function to run.
std::thread::sleep(std::time::Duration::from_millis(500));
// Cancel the host function call.
interrupt_handle.kill();
}
});
for i in 0..NUM_CALLS {
let id = Uuid::new_v4();
// Construct a new span named "hyperlight tracing call cancellation example thread" with INFO level.
let span = span!(
Level::INFO,
"hyperlight tracing call cancellation example thread",
context = format!("Thread number {} GUID {}", i, id),
uuid = %id,
);
let _entered = span.enter();
barrier.wait();
multiuse_sandbox.call::<()>("Spin", ()).unwrap_err();
}
thread.join().expect("Thread panicked");
}
Ok(())
});
join_handles.push(handle);
}
if wait_input {
println!("Press enter to exit...");
let mut input = String::new();
stdin().read_line(&mut input)?;
}
*should_exit.try_lock().unwrap() = true;
for join_handle in join_handles {
let result = join_handle.join();
assert!(result.is_ok());
}
Ok(())
}
#[cfg(test)]
mod test {
use hyperlight_host::{HyperlightError, Result};
use tokio::io::AsyncReadExt;
use tokio::net::{TcpListener, TcpStream};
use super::*;
const TESTER_ADDR: &str = "127.0.0.1:4318";
async fn handle(mut stream: TcpStream) -> Result<()> {
let mut buf = Vec::with_capacity(128);
let size = stream.read_buf(&mut buf).await?;
if size > 0 {
Ok(())
} else {
Err(HyperlightError::Error("Cannot read req body".to_string()))
}
}
async fn check_otl_connection(addr: &str) -> Result<()> {
let listener = TcpListener::bind(addr).await?;
let (stream, _) = listener.accept().await?;
handle(stream).await
}
#[tokio::test]
async fn test_subscriber() {
// Create task that generates spans
let task = tokio::spawn(async move {
let _ = init_tracing_subscriber(ENDPOINT_ADDR);
// No need to wait for input, just generate some spans and exit
let _ = run_example(false);
});
// Create server that listens and checks to see if traces are received
let result = check_otl_connection(TESTER_ADDR).await;
// Abort task in case it doesn't finish
task.abort();
assert!(result.is_ok());
}
}