1313//!
1414//! # Usage
1515//!
16- //! Use `ReporterFactory::create()` to instantiate a reporter, or `ReporterFactory::create_as_ptr()`
17- //! to obtain a raw pointer suitable for static/global usage. The reporter sends events to a remote
18- //! collector, with the destination specified by the `KEY_DESTINATION` environment variable .
16+ //! Use `ReporterFactory::create()` to instantiate a reporter with a socket address , or
17+ //! `ReporterFactory::create_as_ptr()` to obtain a raw pointer suitable for static/global usage.
18+ //! The reporter sends events to a remote collector at the specified address .
1919
20- use crate :: environment:: KEY_DESTINATION ;
2120use crate :: intercept:: { Event , tcp} ;
2221use std:: net:: SocketAddr ;
2322use std:: sync:: atomic:: AtomicPtr ;
@@ -48,17 +47,8 @@ impl ReporterFactory {
4847 /// Creates a new TCP-based reporter using the destination from the environment.
4948 ///
5049 /// The created reporter is not connected yet; it only stores the destination address.
51- /// It is safe to presume that the existence of the instance does not imply it is
52- /// consuming resources until the `report` method is called.
53- pub fn create ( ) -> Result < tcp:: ReporterOnTcp , ReporterCreationError > {
54- let address_str = std:: env:: var ( KEY_DESTINATION )
55- . map_err ( |_| ReporterCreationError :: MissingEnvironmentVariable ( KEY_DESTINATION ) ) ?;
56- let address = address_str
57- . parse :: < SocketAddr > ( )
58- . map_err ( |_| ReporterCreationError :: MissingEnvironmentVariable ( KEY_DESTINATION ) ) ?;
59-
60- let reporter = tcp:: ReporterOnTcp :: new ( address) ;
61- Ok ( reporter)
50+ pub fn create ( address : SocketAddr ) -> impl Reporter {
51+ tcp:: ReporterOnTcp :: new ( address)
6252 }
6353
6454 /// Creates a new reporter and returns it as an atomic pointer.
@@ -74,11 +64,11 @@ impl ReporterFactory {
7464 ///
7565 /// Returns a null pointer if reporter creation fails. Caller must check for null
7666 /// before dereferencing.
77- pub fn create_as_ptr ( ) -> AtomicPtr < tcp:: ReporterOnTcp > {
78- match Self :: create ( ) {
79- Ok ( reporter ) => {
67+ pub fn create_as_ptr ( address_str : & str ) -> AtomicPtr < tcp:: ReporterOnTcp > {
68+ match address_str . parse :: < SocketAddr > ( ) {
69+ Ok ( address ) => {
8070 // Leak the reporter to get a stable pointer for the lifetime of the program
81- let boxed_reporter = Box :: new ( reporter ) ;
71+ let boxed_reporter = Box :: new ( tcp :: ReporterOnTcp :: new ( address ) ) ;
8272 let ptr = Box :: into_raw ( boxed_reporter) ;
8373
8474 AtomicPtr :: new ( ptr)
@@ -90,12 +80,3 @@ impl ReporterFactory {
9080 }
9181 }
9282}
93-
94- /// Errors that can occur during reporter initialization.
95- #[ derive( Error , Debug ) ]
96- pub enum ReporterCreationError {
97- #[ error( "Environment variable '{0}' is missing" ) ]
98- MissingEnvironmentVariable ( & ' static str ) ,
99- #[ error( "Network error: {0}" ) ]
100- Network ( #[ from] std:: io:: Error ) ,
101- }
0 commit comments