pub struct PrometheusServer { /* private fields */ }
Expand description
Manager for a Prometheus metrics server instance.
This struct handles the lifecycle of a Prometheus server, which can either run as a Docker container or as an embedded server within the application process. It provides methods for starting, stopping, and monitoring the server.
Implementations§
Source§impl PrometheusServer
impl PrometheusServer
Sourcepub fn new(
config: PrometheusServerConfig,
metrics_registry: Option<Arc<Registry>>,
enhanced_metrics_provider: Arc<EnhancedMetricsProvider>,
) -> Result<Self>
pub fn new( config: PrometheusServerConfig, metrics_registry: Option<Arc<Registry>>, enhanced_metrics_provider: Arc<EnhancedMetricsProvider>, ) -> Result<Self>
Creates a new Prometheus server manager with the provided configuration and metrics registry.
This constructor prepares the Prometheus server infrastructure but does not start the server.
The actual server (Docker container or embedded) is started when start()
is called.
§Parameters
config
- The configuration settings for the Prometheus servermetrics_registry
- Optional Prometheus registry for the embedded server modeenhanced_metrics_provider
- Provider that generates metrics data
§Errors
Returns an error if the Docker manager connection fails to initialize
Sourcepub async fn create_docker_container(&self) -> Result<()>
pub async fn create_docker_container(&self) -> Result<()>
Creates and configures a new Docker container for the Prometheus server.
Sets up a new container with appropriate port mappings, volume mounts, and configuration based on the server settings. This does not start the container, only creates it.
§Parameters
network
- Optional Docker network to attach the container to
§Errors
Returns an error if the Docker API fails to create the container
§Panics
Panics if mutex locks cannot be acquired
Sourcepub fn registry(&self) -> Option<Arc<Registry>>
pub fn registry(&self) -> Option<Arc<Registry>>
Returns a reference to the metrics registry used by the embedded Prometheus server.
This registry contains all the metrics that are exposed by the embedded Prometheus server. Returns None if no registry was provided during initialization.
Sourcepub fn container_id(&self) -> Option<String>
pub fn container_id(&self) -> Option<String>
Returns the Docker container ID if the server is running in Docker mode.
This identifier can be used to reference the specific Prometheus container for management operations via Docker API. The container ID is only available when the server is configured to run in Docker mode and after the container has been created.
§Returns
Some(String)
- Container ID if the server is using Docker and a container has been createdNone
- If the server is not using Docker, the container hasn’t been created yet, or if there was an error acquiring the lock on the container ID
Sourcepub fn is_docker_based(&self) -> bool
pub fn is_docker_based(&self) -> bool
Checks if this server is configured to use Docker.
The Prometheus server can operate in two modes:
- Docker mode: Runs Prometheus in a separate Docker container, providing isolation and easier management but requiring Docker to be available
- Embedded mode: Runs a Prometheus-compatible metrics HTTP server directly within the application process, with no external dependencies
§Returns
true
- If the server is configured to use Dockerfalse
- If the server is using the embedded mode
Trait Implementations§
Source§impl Clone for PrometheusServer
impl Clone for PrometheusServer
Source§fn clone(&self) -> PrometheusServer
fn clone(&self) -> PrometheusServer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Drop for PrometheusServer
impl Drop for PrometheusServer
Source§impl ServerManager for PrometheusServer
impl ServerManager for PrometheusServer
Source§async fn start(
&self,
network: Option<&str>,
bind_ip: Option<String>,
) -> Result<()>
async fn start( &self, network: Option<&str>, bind_ip: Option<String>, ) -> Result<()>
Starts the Prometheus server in either Docker or embedded mode.
Docker
– Creates (or reuses) a container, mounts configuration/data volumes, optionally connects it to a network, and waits for the container health-check.Embedded
– Binds an in-process HTTP server tohost:port
and serves the/metrics
endpoint backed by the supplied registry.
The call blocks until the server is ready.
§Parameters
network
– Optional Docker network name (Docker
mode only).
§Errors
Returns an error if the container or server fails to start, health-check fails, the port is already in use, or required configuration is missing.
network
- Optional Docker network name to connect the container to. Only used inDocker
mode. When provided, the container will be connected to this network to allow service discovery between services (e.g., Prometheus to scrape metrics from other containers).
§Errors
Returns an error if:
- Docker container creation or startup fails (permission issues, Docker daemon not running, image not found)
- Port binding fails for the embedded server (port already in use or insufficient permissions)
- Server startup times out or fails for any other reason
- Configuration file generation fails or specified paths are invalid (Docker mode only)
- Health check fails for Docker container
- No metrics registry is provided for embedded server mode
Source§async fn stop(&self) -> Result<()>
async fn stop(&self) -> Result<()>
Stops the running Prometheus server and performs necessary cleanup.
This method safely terminates the Prometheus server instance based on its operational mode:
§Docker Mode
When running as a Docker container:
- Retrieves the container ID from internal state
- Stops the Docker container using the Docker API
- Removes the container to free up resources
- Clears the internal container ID reference
- Handles cases where container is already stopped gracefully
§Embedded Mode
When running as an embedded server:
- Acquires lock on the server instance
- Triggers a graceful shutdown of the HTTP server
- Waits for in-flight requests to complete
- Releases resources associated with the server
The method is idempotent and safe to call multiple times, even if the server is not running.
§Errors
Returns an error if:
- Docker API encounters errors when stopping or removing the container
- Communication with Docker daemon fails
- Container removal fails due to permission issues
Source§fn url(&self) -> String
fn url(&self) -> String
Returns the fully qualified URL where the Prometheus server can be accessed.
Constructs a URL using the configured host and port values in the format:
http://{host}:{port}
This URL can be used to:
- Access the Prometheus web UI for manual query and visualization
- Configure Grafana datasources programmatically
- Set up health checks or monitoring of the Prometheus instance
- Access the Prometheus HTTP API for programmatic queries
The URL is valid regardless of whether the server is running in Docker mode or embedded mode, as both modes expose the same interface on the configured host:port combination.
Source§async fn is_running(&self) -> Result<bool>
async fn is_running(&self) -> Result<bool>
Checks if the Prometheus server is currently running.
For Docker-based servers, checks the container status. For embedded servers, checks if the server instance exists.
§Errors
Returns an error if checking the server status fails or if the Docker API reports an error.
Source§async fn wait_until_ready(&self, timeout_secs: u64) -> Result<()>
async fn wait_until_ready(&self, timeout_secs: u64) -> Result<()>
Waits until the Prometheus server is ready to accept connections.
Periodically checks the server status until it’s ready or until the timeout expires. For Docker-based servers, performs HTTP health checks. For embedded servers, verifies the server is bound and responding.
§Parameters
timeout_secs
- Maximum time to wait in seconds
§Errors
Returns an error if the server fails to become ready within the timeout period or if health checks fail.
Auto Trait Implementations§
impl Freeze for PrometheusServer
impl !RefUnwindSafe for PrometheusServer
impl Send for PrometheusServer
impl Sync for PrometheusServer
impl Unpin for PrometheusServer
impl !UnwindSafe for PrometheusServer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedConversion for T
impl<T> CheckedConversion for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<T, Outer> IsWrappedBy<Outer> for T
impl<T, Outer> IsWrappedBy<Outer> for T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
Source§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
Source§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T
. Read moreSource§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.Source§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
Source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from
.Source§impl<T, S> UniqueSaturatedInto<T> for S
impl<T, S> UniqueSaturatedInto<T> for S
Source§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T
.