|
| 1 | +/* |
| 2 | +Copyright 2025 The Hyperlight Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +extern crate alloc; |
| 17 | + |
| 18 | +use alloc::sync::Arc; |
| 19 | + |
| 20 | +use spin::Mutex; |
| 21 | +use tracing_core::span::{Attributes, Id, Record}; |
| 22 | +use tracing_core::subscriber::Subscriber; |
| 23 | +use tracing_core::{Event, Metadata}; |
| 24 | + |
| 25 | +use crate::state::GuestState; |
| 26 | + |
| 27 | +/// The subscriber is used to collect spans and events in the guest. |
| 28 | +pub(crate) struct GuestSubscriber { |
| 29 | + /// Internal state that holds the spans and events |
| 30 | + /// Protected by a Mutex for inner mutability |
| 31 | + /// A reference to this state is stored in a static variable |
| 32 | + state: Arc<Mutex<GuestState>>, |
| 33 | +} |
| 34 | + |
| 35 | +impl GuestSubscriber { |
| 36 | + pub(crate) fn new(guest_start_tsc: u64) -> Self { |
| 37 | + Self { |
| 38 | + state: Arc::new(Mutex::new(GuestState::new(guest_start_tsc))), |
| 39 | + } |
| 40 | + } |
| 41 | + pub(crate) fn state(&self) -> &Arc<Mutex<GuestState>> { |
| 42 | + &self.state |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl Subscriber for GuestSubscriber { |
| 47 | + fn enabled(&self, _md: &Metadata<'_>) -> bool { |
| 48 | + true |
| 49 | + } |
| 50 | + |
| 51 | + fn new_span(&self, attrs: &Attributes<'_>) -> Id { |
| 52 | + self.state.lock().new_span(attrs) |
| 53 | + } |
| 54 | + |
| 55 | + fn record(&self, id: &Id, values: &Record<'_>) { |
| 56 | + self.state.lock().record(id, values) |
| 57 | + } |
| 58 | + |
| 59 | + fn event(&self, event: &Event<'_>) { |
| 60 | + self.state.lock().event(event) |
| 61 | + } |
| 62 | + |
| 63 | + fn enter(&self, id: &Id) { |
| 64 | + self.state.lock().enter(id) |
| 65 | + } |
| 66 | + |
| 67 | + fn exit(&self, id: &Id) { |
| 68 | + self.state.lock().exit(id) |
| 69 | + } |
| 70 | + |
| 71 | + fn try_close(&self, id: Id) -> bool { |
| 72 | + self.state.lock().try_close(id) |
| 73 | + } |
| 74 | + |
| 75 | + fn record_follows_from(&self, _span: &Id, _follows: &Id) { |
| 76 | + // no-op: we don't track follows-from relationships |
| 77 | + } |
| 78 | +} |
0 commit comments