@@ -47,7 +47,7 @@ pub fn construct_filter_string(core_level: Level, other_level: Level) -> String
4747
4848/// Holds initialized tracing/metrics exporters, etc
4949pub struct TelemetryInstance {
50- metric_prefix : & ' static str ,
50+ metric_prefix : String ,
5151 logs_out : Option < Mutex < CoreLogsOut > > ,
5252 metrics : Option < Arc < dyn CoreMeter + ' static > > ,
5353 trace_subscriber : Arc < dyn Subscriber + Send + Sync > ,
@@ -58,7 +58,7 @@ impl TelemetryInstance {
5858 fn new (
5959 trace_subscriber : Arc < dyn Subscriber + Send + Sync > ,
6060 logs_out : Option < Mutex < CoreLogsOut > > ,
61- metric_prefix : & ' static str ,
61+ metric_prefix : String ,
6262 metrics : Option < Arc < dyn CoreMeter + ' static > > ,
6363 attach_service_name : bool ,
6464 ) -> Self {
@@ -71,8 +71,9 @@ impl TelemetryInstance {
7171 }
7272 }
7373
74- /// Returns a trace subscriber which can be used with the tracing crate, or with our own
75- /// [set_trace_subscriber_for_current_thread] function.
74+ /// Return the trace subscriber associated with the telemetry options/instance. Can be used
75+ /// to manually set the default for a thread or globally using the `tracing` crate, or with
76+ /// [set_trace_subscriber_for_current_thread]
7677 pub fn trace_subscriber ( & self ) -> Arc < dyn Subscriber + Send + Sync > {
7778 self . trace_subscriber . clone ( )
7879 }
@@ -83,21 +84,37 @@ impl TelemetryInstance {
8384 self . metrics = Some ( meter) ;
8485 }
8586
86- /// Returns our wrapper for metric meters, can be used to, ex: initialize clients
87- pub fn get_metric_meter ( & self ) -> Option < TemporalMeter > {
87+ /// Returns our wrapper for metric meters, including the `metric_prefix` from
88+ /// [TelemetryOptions]. This should be used to initialize clients or for any other
89+ /// temporal-owned metrics. User defined metrics should use [Self::get_metric_meter].
90+ pub fn get_temporal_metric_meter ( & self ) -> Option < TemporalMeter > {
8891 self . metrics . clone ( ) . map ( |m| {
89- let kvs = if self . attach_service_name {
90- vec ! [ MetricKeyValue :: new( "service_name" , TELEM_SERVICE_NAME ) ]
91- } else {
92- vec ! [ ]
93- } ;
92+ let kvs = self . default_kvs ( ) ;
9493 let attribs = MetricsAttributesOptions :: new ( kvs) ;
9594 TemporalMeter :: new (
96- Arc :: new ( PrefixedMetricsMeter :: new ( self . metric_prefix , m) ) as Arc < dyn CoreMeter > ,
95+ Arc :: new ( PrefixedMetricsMeter :: new ( self . metric_prefix . clone ( ) , m) )
96+ as Arc < dyn CoreMeter > ,
9797 attribs,
9898 )
9999 } )
100100 }
101+
102+ /// Returns our wrapper for metric meters, including attaching the service name if enabled.
103+ pub fn get_metric_meter ( & self ) -> Option < TemporalMeter > {
104+ self . metrics . clone ( ) . map ( |m| {
105+ let kvs = self . default_kvs ( ) ;
106+ let attribs = MetricsAttributesOptions :: new ( kvs) ;
107+ TemporalMeter :: new ( m, attribs)
108+ } )
109+ }
110+
111+ fn default_kvs ( & self ) -> Vec < MetricKeyValue > {
112+ if self . attach_service_name {
113+ vec ! [ MetricKeyValue :: new( "service_name" , TELEM_SERVICE_NAME ) ]
114+ } else {
115+ vec ! [ ]
116+ }
117+ }
101118}
102119
103120thread_local ! {
@@ -122,14 +139,6 @@ pub fn remove_trace_subscriber_for_current_thread() {
122139 SUB_GUARD . with ( |sg| sg. take ( ) ) ;
123140}
124141
125- fn metric_prefix ( opts : & TelemetryOptions ) -> & ' static str {
126- if opts. no_temporal_prefix_for_metrics {
127- ""
128- } else {
129- "temporal_"
130- }
131- }
132-
133142impl CoreTelemetry for TelemetryInstance {
134143 fn fetch_buffered_logs ( & self ) -> Vec < CoreLog > {
135144 if let Some ( logs_out) = self . logs_out . as_ref ( ) {
@@ -155,7 +164,6 @@ pub fn telemetry_init(opts: TelemetryOptions) -> Result<TelemetryInstance, anyho
155164 // way which is nice.
156165 // Parts of telem dat ====
157166 let mut logs_out = None ;
158- let metric_prefix = metric_prefix ( & opts) ;
159167 // =======================
160168
161169 // Tracing subscriber layers =========
@@ -211,7 +219,7 @@ pub fn telemetry_init(opts: TelemetryOptions) -> Result<TelemetryInstance, anyho
211219 Ok ( TelemetryInstance :: new (
212220 Arc :: new ( reg) ,
213221 logs_out,
214- metric_prefix,
222+ opts . metric_prefix ,
215223 opts. metrics ,
216224 opts. attach_service_name ,
217225 ) )
0 commit comments