Skip to content

Commit

Permalink
Reduce visibility scope of types after sidecar::interface refactor
Browse files Browse the repository at this point in the history
interface.rs has been refactored in to a "service" module within
sidecar. During refactor the access level of some functions and types
was increased while they were being moved around. This should "fix" the
access level to the most restrictive possible.
  • Loading branch information
ekump committed May 8, 2024
1 parent d9311f2 commit ceeddc3
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 170 deletions.
29 changes: 13 additions & 16 deletions sidecar-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@ use datadog_sidecar::agent_remote_config::{
};
use datadog_sidecar::config;
use datadog_sidecar::config::LogMethod;
use datadog_sidecar::one_way_shared_memory::{OneWayShmReader, ReaderOpener};
use datadog_sidecar::service::{
blocking::{self, SidecarTransport},
InstanceId, QueueId, RuntimeMetadata, SerializedTracerHeaderTags, SessionConfig, SidecarAction,
};
use ddcommon::Endpoint;
use ddcommon_ffi as ffi;
use ddcommon_ffi::MaybeError;
use ddtelemetry::{
data::{self, Dependency, Integration},
worker::{LifecycleAction, TelemetryActions},
};
use ddtelemetry_ffi::try_c;
use ffi::slice::AsBytes;
use libc::c_char;
use std::convert::TryInto;
use std::ffi::c_void;
Expand All @@ -21,22 +34,6 @@ use std::os::windows::io::{FromRawHandle, RawHandle};
use std::slice;
use std::time::Duration;

use datadog_sidecar::service::{
InstanceId, QueueId, RuntimeMetadata, SerializedTracerHeaderTags, SessionConfig, SidecarAction,
};

use datadog_sidecar::one_way_shared_memory::{OneWayShmReader, ReaderOpener};
use datadog_sidecar::service::blocking::{self, SidecarTransport};
use ddcommon::Endpoint;
use ddtelemetry::{
data::{self, Dependency, Integration},
worker::{LifecycleAction, TelemetryActions},
};
use ffi::slice::AsBytes;

use ddcommon_ffi::MaybeError;
use ddtelemetry_ffi::try_c;

#[repr(C)]
pub struct NativeFile {
pub handle: Box<PlatformHandle<File>>,
Expand Down
32 changes: 18 additions & 14 deletions sidecar/src/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
// Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/
// SPDX-License-Identifier: Apache-2.0

// imports for structs defined in this file
use crate::config;
use crate::service::telemetry::enqueued_telemetry_data::EnqueuedTelemetryData;
use ddcommon::tag::Tag;
use ddcommon::Endpoint;
use ddtelemetry::metrics::MetricContext;
use ddtelemetry::worker::TelemetryActions;
use futures::future::Shared;
pub use instance_id::InstanceId;
use manual_future::ManualFuture;
pub use queue_id::QueueId;
pub use request_identification::{RequestIdentification, RequestIdentifier};
pub use runtime_info::{RuntimeInfo, SharedAppManualFut};
pub use runtime_metadata::RuntimeMetadata;
use serde::{Deserialize, Serialize};
pub use serialized_tracer_header_tags::SerializedTracerHeaderTags;
pub use session_info::SessionInfo;
pub use sidecar_interface::{
SidecarInterface, SidecarInterfaceClient, SidecarInterfaceRequest, SidecarInterfaceResponse,
};
pub use sidecar_server::SidecarServer;
use std::path::PathBuf;
use std::time::Duration;

// public types we want to bring up to top level of service:: scope
pub use instance_id::InstanceId;
pub use queue_id::QueueId;
pub use runtime_metadata::RuntimeMetadata;
pub use serialized_tracer_header_tags::SerializedTracerHeaderTags;

// public to crate types we want to bring up to top level of service:: scope
pub(crate) use request_identification::{RequestIdentification, RequestIdentifier};
pub(crate) use sidecar_server::SidecarServer;

use runtime_info::RuntimeInfo;
use session_info::SessionInfo;
use sidecar_interface::{SidecarInterface, SidecarInterfaceRequest, SidecarInterfaceResponse};

pub mod blocking;
mod instance_id;
pub mod queue_id;
mod queue_id;
mod request_identification;
mod runtime_info;
mod runtime_metadata;
mod serialized_tracer_header_tags;
mod session_info;
mod sidecar_interface;
mod sidecar_server;
pub mod telemetry;
pub(crate) mod sidecar_server;
mod telemetry;
pub(crate) mod tracing;

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions sidecar/src/service/request_identification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::service::InstanceId;

/// The `RequestIdentification` trait provides a method for extracting a request identifier.
pub trait RequestIdentification {
pub(crate) trait RequestIdentification {
/// Extracts the identifier from the request.
///
/// # Returns
Expand All @@ -17,7 +17,7 @@ pub trait RequestIdentification {
///
/// This enum is used in conjunction with the `RequestIdentification` trait to provide a flexible
/// way of identifying a request.
pub enum RequestIdentifier {
pub(crate) enum RequestIdentifier {
/// Represents a request identified by an instance ID.
InstanceId(InstanceId),
/// Represents a request identified by a session ID.
Expand Down
12 changes: 6 additions & 6 deletions sidecar/src/service/runtime_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type AppMap = HashMap<(String, String), Shared<ManualFuture<Option<AppInstance>>
/// `SharedAppManualFut` is a struct that contains a shared future of an `AppInstance` and its
/// completer. The `app_future` is a shared future that may contain an `Option<AppInstance>`.
/// The `completer` is used to complete the `app_future`.
pub struct SharedAppManualFut {
pub app_future: Shared<ManualFuture<Option<AppInstance>>>,
pub completer: Option<ManualFutureCompleter<Option<AppInstance>>>,
pub(crate) struct SharedAppManualFut {
pub(crate) app_future: Shared<ManualFuture<Option<AppInstance>>>,
pub(crate) completer: Option<ManualFutureCompleter<Option<AppInstance>>>,
}

/// `RuntimeInfo` is a struct that contains information about a runtime.
Expand All @@ -32,7 +32,7 @@ pub(crate) struct RuntimeInfo {
pub(crate) apps: Arc<Mutex<AppMap>>,
app_or_actions: Arc<Mutex<HashMap<QueueId, AppOrQueue>>>,
#[cfg(feature = "tracing")]
pub instance_id: InstanceId,
pub(crate) instance_id: InstanceId,
}

impl RuntimeInfo {
Expand Down Expand Up @@ -67,7 +67,7 @@ impl RuntimeInfo {
}
/// Shuts down the runtime.
/// This involves shutting down all the instances in the runtime.
pub async fn shutdown(self) {
pub(crate) async fn shutdown(self) {
#[cfg(feature = "tracing")]
info!(
"Shutting down runtime_id {} for session {}",
Expand Down Expand Up @@ -126,4 +126,4 @@ impl RuntimeInfo {
}
}

// TODO-EK: Add tests for RuntimeInfo or add TODO comment before merging
// TODO: APM-1079 - Add unit tests for RuntimeInfo
81 changes: 13 additions & 68 deletions sidecar/src/service/session_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::service::{InstanceId, RuntimeInfo};
/// It contains a list of runtimes, session configuration, tracer configuration, and log guards.
/// It also has methods to manage the runtimes and configurations.
#[derive(Default, Clone)]
pub struct SessionInfo {
pub(crate) struct SessionInfo {
runtimes: Arc<Mutex<HashMap<String, RuntimeInfo>>>,
pub(crate) session_config: Arc<Mutex<Option<ddtelemetry::config::Config>>>,
tracer_config: Arc<Mutex<tracer::Config>>,
Expand All @@ -37,23 +37,9 @@ impl SessionInfo {
/// # Arguments
///
/// * `runtime_id` - The ID of the runtime.
///
/// # Examples
///
/// ```
/// if cfg!(not(miri)) {
/// use datadog_sidecar::service::SessionInfo;
///
/// #[tokio::main]
/// async fn get_runtime_example() {
/// let session_info = SessionInfo::default();
/// let runtime_info = session_info.get_runtime(&"runtime1".to_string());
/// }
/// }
/// ```
// DEV-TODO: This function should likely either be refactored or have its name changed as its
// performing a get or insert operation.
pub fn get_runtime(&self, runtime_id: &String) -> RuntimeInfo {
// TODO: APM-1076 This function should either be refactored or have its name changed as its
// performing a get or create operation.
pub(crate) fn get_runtime(&self, runtime_id: &String) -> RuntimeInfo {
let mut runtimes = self.lock_runtimes();
match runtimes.get(runtime_id) {
Some(runtime) => runtime.clone(),
Expand All @@ -77,21 +63,7 @@ impl SessionInfo {
}

/// Shuts down all runtimes in the session.
///
/// # Examples
///
/// ```
/// if cfg!(not(miri)) {
/// use datadog_sidecar::service::SessionInfo;
///
/// #[tokio::main]
/// async fn shutdown_example() {
/// let session_info = SessionInfo::default();
/// session_info.shutdown().await;
/// }
/// }
/// ```
pub async fn shutdown(&self) {
pub(crate) async fn shutdown(&self) {
let runtimes: Vec<RuntimeInfo> = self
.lock_runtimes()
.drain()
Expand All @@ -105,22 +77,9 @@ impl SessionInfo {

future::join_all(runtimes_shutting_down).await;
}

/// Shuts down all running instances in the session.
///
/// # Examples
///
/// ```
/// if cfg!(not(miri)) {
/// use datadog_sidecar::service::SessionInfo;
///
/// #[tokio::main]
/// async fn shutdown_running_instances_example() {
/// let session_info = SessionInfo::default();
/// session_info.shutdown_running_instances().await;
/// }
/// }
/// ```
pub async fn shutdown_running_instances(&self) {
pub(crate) async fn shutdown_running_instances(&self) {
let runtimes: Vec<RuntimeInfo> = self
.lock_runtimes()
.drain()
Expand All @@ -140,21 +99,7 @@ impl SessionInfo {
/// # Arguments
///
/// * `runtime_id` - The ID of the runtime.
///
/// # Examples
///
/// ```
/// if cfg!(not(miri)) {
/// use datadog_sidecar::service::SessionInfo;
///
/// #[tokio::main]
/// async fn shutdown_runtime_example() {
/// let session_info = SessionInfo::default();
/// session_info.shutdown_runtime("runtime1").await;
/// }
/// }
/// ```
pub async fn shutdown_runtime(&self, runtime_id: &str) {
pub(crate) async fn shutdown_runtime(&self, runtime_id: &str) {
let maybe_runtime = {
let mut runtimes = self.lock_runtimes();
runtimes.remove(runtime_id)
Expand All @@ -165,11 +110,11 @@ impl SessionInfo {
}
}

pub fn lock_runtimes(&self) -> MutexGuard<HashMap<String, RuntimeInfo>> {
pub(crate) fn lock_runtimes(&self) -> MutexGuard<HashMap<String, RuntimeInfo>> {
self.runtimes.lock().unwrap()
}

pub fn get_telemetry_config(&self) -> MutexGuard<Option<ddtelemetry::config::Config>> {
pub(crate) fn get_telemetry_config(&self) -> MutexGuard<Option<ddtelemetry::config::Config>> {
let mut cfg = self.session_config.lock().unwrap();

if (*cfg).is_none() {
Expand All @@ -179,7 +124,7 @@ impl SessionInfo {
cfg
}

pub fn modify_telemetry_config<F>(&self, mut f: F)
pub(crate) fn modify_telemetry_config<F>(&self, mut f: F)
where
F: FnMut(&mut ddtelemetry::config::Config),
{
Expand All @@ -188,11 +133,11 @@ impl SessionInfo {
}
}

pub fn get_trace_config(&self) -> MutexGuard<tracer::Config> {
pub(crate) fn get_trace_config(&self) -> MutexGuard<tracer::Config> {
self.tracer_config.lock().unwrap()
}

pub fn modify_trace_config<F>(&self, mut f: F)
pub(crate) fn modify_trace_config<F>(&self, mut f: F)
where
F: FnMut(&mut tracer::Config),
{
Expand Down
30 changes: 1 addition & 29 deletions sidecar/src/service/sidecar_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,6 @@ impl SidecarServer {
/// # Returns
///
/// * `usize`: The number of active sessions.
///
/// # Examples
///
/// ```
/// use datadog_sidecar::service::SidecarServer;
///
/// let server = SidecarServer::default();
/// let active_sessions = server.active_session_count();
/// println!("Number of active sessions: {}", active_sessions);
/// ```
pub fn active_session_count(&self) -> usize {
self.session_counter
.lock()
Expand Down Expand Up @@ -312,25 +302,7 @@ impl SidecarServer {
/// # Returns
///
/// * `SidecarStats`: A struct containing various statistics related to the SidecarServer.
///
/// # Examples
///
/// ```
/// if cfg!(not(miri)) {
/// use datadog_sidecar::service::SidecarServer;
///
/// #[tokio::main]
/// async fn stats_example() {
/// let server = SidecarServer::default();
/// let stats = server.compute_stats().await;
/// }
/// }
/// ```
///
/// # Async
///
/// This function is async and should be awaited.
pub async fn compute_stats(&self) -> SidecarStats {
pub(crate) async fn compute_stats(&self) -> SidecarStats {
let mut telemetry_stats_errors = 0;
let telemetry_stats = join_all({
let sessions = self.lock_sessions();
Expand Down
28 changes: 14 additions & 14 deletions sidecar/src/service/telemetry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ pub mod enqueued_telemetry_data;
pub mod enqueued_telemetry_stats;

#[derive(Serialize, Deserialize)]
pub struct SidecarStats {
pub trace_flusher: TraceFlusherStats,
pub sessions: u32,
pub session_counter_size: u32,
pub runtimes: u32,
pub apps: u32,
pub active_apps: u32,
pub enqueued_apps: u32,
pub enqueued_telemetry_data: EnqueuedTelemetryStats,
pub telemetry_metrics_contexts: u32,
pub telemetry_worker: TelemetryWorkerStats,
pub telemetry_worker_errors: u32,
pub log_writer: TemporarilyRetainedMapStats,
pub log_filter: TemporarilyRetainedMapStats,
pub(crate) struct SidecarStats {
pub(crate) trace_flusher: TraceFlusherStats,
pub(crate) sessions: u32,
pub(crate) session_counter_size: u32,
pub(crate) runtimes: u32,
pub(crate) apps: u32,
pub(crate) active_apps: u32,
pub(crate) enqueued_apps: u32,
pub(crate) enqueued_telemetry_data: EnqueuedTelemetryStats,
pub(crate) telemetry_metrics_contexts: u32,
pub(crate) telemetry_worker: TelemetryWorkerStats,
pub(crate) telemetry_worker_errors: u32,
pub(crate) log_writer: TemporarilyRetainedMapStats,
pub(crate) log_filter: TemporarilyRetainedMapStats,
}
Loading

0 comments on commit ceeddc3

Please sign in to comment.