11use pyo3:: exceptions:: { PyRuntimeError , PyValueError } ;
22use pyo3:: prelude:: * ;
3+ use std:: collections:: HashMap ;
34use std:: net:: SocketAddr ;
45use std:: str:: FromStr ;
5- use temporal_sdk_core:: { telemetry_init, TelemetryOptions , TelemetryOptionsBuilder } ;
6+ use temporal_sdk_core:: {
7+ telemetry_init, Logger , MetricsExporter , OtelCollectorOptions , TelemetryOptions ,
8+ TelemetryOptionsBuilder , TraceExporter ,
9+ } ;
610use url:: Url ;
711
812#[ pyclass]
913pub struct TelemetryRef {
1014 // TODO(cretz): This is private
11- // telemetry: &'static temporal_sdk_core::telemetry::GlobalTelemDat,
15+ // telemetry: &'static temporal_sdk_core::telemetry::GlobalTelemDat,
1216}
1317
1418#[ derive( FromPyObject ) ]
1519pub struct TelemetryConfig {
16- otel_collector_url : Option < String > ,
1720 tracing_filter : Option < String > ,
21+ otel_tracing : Option < OtelCollectorConfig > ,
22+ log_console : bool ,
1823 log_forwarding_level : Option < String > ,
19- prometheus_export_bind_address : Option < String > ,
24+ otel_metrics : Option < OtelCollectorConfig > ,
25+ prometheus_metrics : Option < PrometheusMetricsConfig > ,
26+ }
27+
28+ #[ derive( FromPyObject ) ]
29+ pub struct OtelCollectorConfig {
30+ url : String ,
31+ headers : HashMap < String , String > ,
32+ }
33+
34+ #[ derive( FromPyObject ) ]
35+ pub struct PrometheusMetricsConfig {
36+ bind_address : String ,
2037}
2138
2239pub fn init_telemetry ( config : TelemetryConfig ) -> PyResult < TelemetryRef > {
@@ -34,28 +51,52 @@ impl TryFrom<TelemetryConfig> for TelemetryOptions {
3451
3552 fn try_from ( conf : TelemetryConfig ) -> PyResult < Self > {
3653 let mut build = TelemetryOptionsBuilder :: default ( ) ;
37- if let Some ( ref v) = conf. otel_collector_url {
38- build. otel_collector_url (
39- Url :: parse ( v)
40- . map_err ( |err| PyValueError :: new_err ( format ! ( "Invalid OTel URL: {}" , err) ) ) ?,
41- ) ;
42- }
4354 if let Some ( v) = conf. tracing_filter {
4455 build. tracing_filter ( v) ;
4556 }
57+ if let Some ( v) = conf. otel_tracing {
58+ build. tracing ( TraceExporter :: Otel ( v. try_into ( ) ?) ) ;
59+ }
4660 if let Some ( ref v) = conf. log_forwarding_level {
47- build. log_forwarding_level (
48- log:: LevelFilter :: from_str ( v)
49- . map_err ( |err| PyValueError :: new_err ( format ! ( "Invalid log level: {}" , err) ) ) ?,
50- ) ;
61+ if conf. log_console {
62+ return Err ( PyValueError :: new_err (
63+ "Cannot have log forwarding level and log console" ,
64+ ) ) ;
65+ }
66+ build. logging ( Logger :: Forward ( log:: LevelFilter :: from_str ( v) . map_err (
67+ |err| PyValueError :: new_err ( format ! ( "Invalid log level: {}" , err) ) ,
68+ ) ?) ) ;
69+ } else if conf. log_console {
70+ build. logging ( Logger :: Console ) ;
5171 }
52- if let Some ( ref v) = conf. prometheus_export_bind_address {
53- build. prometheus_export_bind_address ( SocketAddr :: from_str ( v) . map_err ( |err| {
54- PyValueError :: new_err ( format ! ( "Invalid Prometheus address: {}" , err) )
55- } ) ?) ;
72+ if let Some ( v) = conf. otel_metrics {
73+ if conf. prometheus_metrics . is_some ( ) {
74+ return Err ( PyValueError :: new_err (
75+ "Cannot have OTel and Prometheus metrics" ,
76+ ) ) ;
77+ }
78+ build. metrics ( MetricsExporter :: Otel ( v. try_into ( ) ?) ) ;
79+ } else if let Some ( v) = conf. prometheus_metrics {
80+ build. metrics ( MetricsExporter :: Prometheus (
81+ SocketAddr :: from_str ( & v. bind_address ) . map_err ( |err| {
82+ PyValueError :: new_err ( format ! ( "Invalid Prometheus address: {}" , err) )
83+ } ) ?,
84+ ) ) ;
5685 }
5786 build
5887 . build ( )
5988 . map_err ( |err| PyValueError :: new_err ( format ! ( "Invalid telemetry config: {}" , err) ) )
6089 }
6190}
91+
92+ impl TryFrom < OtelCollectorConfig > for OtelCollectorOptions {
93+ type Error = PyErr ;
94+
95+ fn try_from ( conf : OtelCollectorConfig ) -> PyResult < Self > {
96+ Ok ( OtelCollectorOptions {
97+ url : Url :: parse ( & conf. url )
98+ . map_err ( |err| PyValueError :: new_err ( format ! ( "Invalid OTel URL: {}" , err) ) ) ?,
99+ headers : conf. headers ,
100+ } )
101+ }
102+ }
0 commit comments