@temporalio/core-bridge 1.4.4 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +327 -419
- package/Cargo.toml +1 -1
- package/index.js +25 -2
- package/lib/errors.d.ts +22 -0
- package/lib/errors.js +65 -0
- package/lib/errors.js.map +1 -0
- package/lib/index.d.ts +440 -0
- package/lib/index.js +8 -0
- package/lib/index.js.map +1 -0
- package/package.json +11 -5
- package/releases/aarch64-apple-darwin/index.node +0 -0
- package/releases/aarch64-unknown-linux-gnu/index.node +0 -0
- package/releases/x86_64-apple-darwin/index.node +0 -0
- package/releases/x86_64-pc-windows-msvc/index.node +0 -0
- package/releases/x86_64-unknown-linux-gnu/index.node +0 -0
- package/sdk-core/.buildkite/docker/Dockerfile +1 -1
- package/sdk-core/.buildkite/docker/docker-compose.yaml +2 -2
- package/sdk-core/bridge-ffi/Cargo.toml +1 -1
- package/sdk-core/bridge-ffi/include/sdk-core-bridge.h +0 -25
- package/sdk-core/bridge-ffi/src/lib.rs +29 -108
- package/sdk-core/bridge-ffi/src/wrappers.rs +35 -25
- package/sdk-core/client/Cargo.toml +1 -1
- package/sdk-core/client/src/lib.rs +12 -20
- package/sdk-core/client/src/raw.rs +9 -8
- package/sdk-core/client/src/retry.rs +100 -23
- package/sdk-core/core/Cargo.toml +5 -5
- package/sdk-core/core/benches/workflow_replay.rs +13 -10
- package/sdk-core/core/src/abstractions.rs +22 -22
- package/sdk-core/core/src/core_tests/activity_tasks.rs +1 -1
- package/sdk-core/core/src/core_tests/local_activities.rs +228 -6
- package/sdk-core/core/src/core_tests/queries.rs +247 -89
- package/sdk-core/core/src/core_tests/workers.rs +2 -2
- package/sdk-core/core/src/core_tests/workflow_cancels.rs +1 -1
- package/sdk-core/core/src/core_tests/workflow_tasks.rs +46 -27
- package/sdk-core/core/src/lib.rs +139 -32
- package/sdk-core/core/src/replay/mod.rs +185 -41
- package/sdk-core/core/src/telemetry/log_export.rs +190 -0
- package/sdk-core/core/src/telemetry/metrics.rs +184 -139
- package/sdk-core/core/src/telemetry/mod.rs +296 -318
- package/sdk-core/core/src/telemetry/prometheus_server.rs +4 -3
- package/sdk-core/core/src/test_help/mod.rs +9 -7
- package/sdk-core/core/src/worker/activities/local_activities.rs +2 -1
- package/sdk-core/core/src/worker/activities.rs +40 -23
- package/sdk-core/core/src/worker/client/mocks.rs +1 -1
- package/sdk-core/core/src/worker/client.rs +30 -4
- package/sdk-core/core/src/worker/mod.rs +22 -18
- package/sdk-core/core/src/worker/workflow/driven_workflow.rs +10 -19
- package/sdk-core/core/src/worker/workflow/history_update.rs +99 -25
- package/sdk-core/core/src/worker/workflow/machines/activity_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/cancel_external_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/cancel_workflow_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/child_workflow_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/complete_workflow_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/continue_as_new_workflow_state_machine.rs +2 -6
- package/sdk-core/core/src/worker/workflow/machines/fail_workflow_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/local_activity_state_machine.rs +18 -21
- package/sdk-core/core/src/worker/workflow/machines/mod.rs +12 -38
- package/sdk-core/core/src/worker/workflow/machines/modify_workflow_properties_state_machine.rs +178 -0
- package/sdk-core/core/src/worker/workflow/machines/patch_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/signal_external_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/timer_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/transition_coverage.rs +8 -2
- package/sdk-core/core/src/worker/workflow/machines/upsert_search_attributes_state_machine.rs +1 -5
- package/sdk-core/core/src/worker/workflow/machines/workflow_machines.rs +232 -216
- package/sdk-core/core/src/worker/workflow/machines/workflow_task_state_machine.rs +1 -6
- package/sdk-core/core/src/worker/workflow/managed_run/managed_wf_test.rs +4 -4
- package/sdk-core/core/src/worker/workflow/managed_run.rs +13 -5
- package/sdk-core/core/src/worker/workflow/mod.rs +61 -9
- package/sdk-core/core/src/worker/workflow/wft_poller.rs +2 -2
- package/sdk-core/core/src/worker/workflow/workflow_stream.rs +56 -11
- package/sdk-core/core-api/Cargo.toml +4 -3
- package/sdk-core/core-api/src/lib.rs +1 -43
- package/sdk-core/core-api/src/telemetry.rs +147 -0
- package/sdk-core/core-api/src/worker.rs +13 -0
- package/sdk-core/fsm/rustfsm_procmacro/tests/trybuild/no_handle_conversions_require_into_fail.stderr +1 -1
- package/sdk-core/histories/evict_while_la_running_no_interference-23_history.bin +0 -0
- package/sdk-core/histories/evict_while_la_running_no_interference-85_history.bin +0 -0
- package/sdk-core/protos/api_upstream/.github/CODEOWNERS +1 -1
- package/sdk-core/protos/api_upstream/buf.yaml +0 -3
- package/sdk-core/protos/api_upstream/temporal/api/batch/v1/message.proto +3 -7
- package/sdk-core/protos/api_upstream/temporal/api/command/v1/message.proto +8 -0
- package/sdk-core/protos/api_upstream/temporal/api/enums/v1/command_type.proto +1 -2
- package/sdk-core/protos/api_upstream/temporal/api/enums/v1/event_type.proto +2 -0
- package/sdk-core/protos/api_upstream/temporal/api/enums/v1/failed_cause.proto +3 -0
- package/sdk-core/protos/api_upstream/temporal/api/history/v1/message.proto +13 -0
- package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/request_response.proto +19 -59
- package/sdk-core/protos/api_upstream/temporal/api/operatorservice/v1/service.proto +0 -19
- package/sdk-core/protos/api_upstream/temporal/api/schedule/v1/message.proto +108 -29
- package/sdk-core/protos/api_upstream/temporal/api/taskqueue/v1/message.proto +2 -2
- package/sdk-core/protos/api_upstream/temporal/api/workflow/v1/message.proto +1 -0
- package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/request_response.proto +47 -8
- package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/service.proto +15 -1
- package/sdk-core/protos/local/temporal/sdk/core/workflow_activation/workflow_activation.proto +2 -0
- package/sdk-core/protos/local/temporal/sdk/core/workflow_commands/workflow_commands.proto +8 -1
- package/sdk-core/sdk/src/interceptors.rs +36 -3
- package/sdk-core/sdk/src/lib.rs +7 -4
- package/sdk-core/sdk/src/workflow_context.rs +13 -2
- package/sdk-core/sdk-core-protos/src/history_builder.rs +47 -1
- package/sdk-core/sdk-core-protos/src/history_info.rs +22 -22
- package/sdk-core/sdk-core-protos/src/lib.rs +49 -27
- package/sdk-core/test-utils/Cargo.toml +1 -0
- package/sdk-core/test-utils/src/lib.rs +81 -29
- package/sdk-core/tests/integ_tests/metrics_tests.rs +37 -0
- package/sdk-core/tests/integ_tests/polling_tests.rs +0 -13
- package/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +145 -4
- package/sdk-core/tests/integ_tests/workflow_tests/modify_wf_properties.rs +53 -0
- package/sdk-core/tests/integ_tests/workflow_tests/replay.rs +106 -20
- package/sdk-core/tests/integ_tests/workflow_tests.rs +18 -8
- package/sdk-core/tests/main.rs +6 -4
- package/src/conversions.rs +52 -47
- package/src/errors.rs +28 -86
- package/src/helpers.rs +3 -4
- package/src/lib.rs +2 -2
- package/src/runtime.rs +132 -61
- package/src/testing.rs +7 -4
- package/src/worker.rs +67 -50
- package/ts/errors.ts +55 -0
- package/{index.d.ts → ts/index.ts} +121 -15
- package/sdk-core/core/src/log_export.rs +0 -62
- package/sdk-core/core/src/worker/workflow/machines/mutable_side_effect_state_machine.rs +0 -127
- package/sdk-core/core/src/worker/workflow/machines/side_effect_state_machine.rs +0 -71
- package/sdk-core/protos/api_upstream/temporal/api/cluster/v1/message.proto +0 -83
- package/sdk-core/protos/api_upstream/temporal/api/enums/v1/cluster.proto +0 -40
|
@@ -1,17 +1,20 @@
|
|
|
1
|
+
//! This module helps with the initialization and management of telemetry. IE: Metrics and tracing.
|
|
2
|
+
//! Logs from core are all traces, which may be exported to the console, in memory, or externally.
|
|
3
|
+
|
|
4
|
+
mod log_export;
|
|
1
5
|
pub(crate) mod metrics;
|
|
2
6
|
mod prometheus_server;
|
|
3
7
|
|
|
4
|
-
use crate::{
|
|
5
|
-
log_export::
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
use crate::telemetry::{
|
|
9
|
+
log_export::{CoreLogExportLayer, CoreLogsOut},
|
|
10
|
+
metrics::SDKAggSelector,
|
|
11
|
+
prometheus_server::PromServer,
|
|
8
12
|
};
|
|
13
|
+
use crossbeam::channel::Receiver;
|
|
9
14
|
use itertools::Itertools;
|
|
10
|
-
use log::LevelFilter;
|
|
11
15
|
use once_cell::sync::OnceCell;
|
|
12
16
|
use opentelemetry::{
|
|
13
|
-
|
|
14
|
-
metrics::Meter,
|
|
17
|
+
metrics::{Meter, MeterProvider},
|
|
15
18
|
runtime,
|
|
16
19
|
sdk::{
|
|
17
20
|
export::metrics::aggregation::{self, Temporality, TemporalitySelector},
|
|
@@ -21,369 +24,344 @@ use opentelemetry::{
|
|
|
21
24
|
KeyValue,
|
|
22
25
|
};
|
|
23
26
|
use opentelemetry_otlp::WithExportConfig;
|
|
24
|
-
use parking_lot::
|
|
25
|
-
use std::{
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
time::Duration,
|
|
27
|
+
use parking_lot::Mutex;
|
|
28
|
+
use std::{cell::RefCell, collections::VecDeque, convert::TryInto, env, sync::Arc, time::Duration};
|
|
29
|
+
use temporal_sdk_core_api::telemetry::{
|
|
30
|
+
CoreLog, CoreTelemetry, Logger, MetricTemporality, MetricsExporter, OtelCollectorOptions,
|
|
31
|
+
TelemetryOptions, TraceExporter,
|
|
30
32
|
};
|
|
31
|
-
use temporal_sdk_core_api::CoreTelemetry;
|
|
32
33
|
use tonic::metadata::MetadataMap;
|
|
33
|
-
use
|
|
34
|
-
use
|
|
34
|
+
use tracing::{Level, Subscriber};
|
|
35
|
+
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Layer};
|
|
35
36
|
|
|
36
37
|
const TELEM_SERVICE_NAME: &str = "temporal-core-sdk";
|
|
37
|
-
static DEFAULT_FILTER: &str = "temporal_sdk_core=INFO";
|
|
38
|
-
static GLOBAL_TELEM_DAT: OnceCell<GlobalTelemDat> = OnceCell::new();
|
|
39
|
-
static TELETM_MUTEX: Mutex<()> = const_mutex(());
|
|
40
|
-
|
|
41
|
-
fn default_resource_kvs() -> &'static [KeyValue] {
|
|
42
|
-
static INSTANCE: OnceCell<[KeyValue; 1]> = OnceCell::new();
|
|
43
|
-
INSTANCE.get_or_init(|| [KeyValue::new("service.name", TELEM_SERVICE_NAME)])
|
|
44
|
-
}
|
|
45
|
-
fn default_resource() -> Resource {
|
|
46
|
-
Resource::new(default_resource_kvs().iter().cloned())
|
|
47
|
-
}
|
|
48
38
|
|
|
49
|
-
///
|
|
50
|
-
|
|
51
|
-
pub
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
39
|
+
/// Help you construct an [EnvFilter] compatible filter string which will forward all core module
|
|
40
|
+
/// traces at `core_level` and all others (from 3rd party modules, etc) at `other_levl.
|
|
41
|
+
pub fn construct_filter_string(core_level: Level, other_level: Level) -> String {
|
|
42
|
+
format!(
|
|
43
|
+
"{o},temporal_sdk_core={l},temporal_client={l},temporal_sdk={l}",
|
|
44
|
+
o = other_level,
|
|
45
|
+
l = core_level
|
|
46
|
+
)
|
|
57
47
|
}
|
|
58
48
|
|
|
59
|
-
///
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
49
|
+
/// Holds initialized tracing/metrics exporters, etc
|
|
50
|
+
pub struct TelemetryInstance {
|
|
51
|
+
metric_prefix: &'static str,
|
|
52
|
+
logs_out: Option<Mutex<CoreLogsOut>>,
|
|
53
|
+
metrics: Option<(Box<dyn MeterProvider + Send + Sync + 'static>, Meter)>,
|
|
54
|
+
trace_subscriber: Arc<dyn Subscriber + Send + Sync>,
|
|
55
|
+
_keepalive_rx: Receiver<()>,
|
|
64
56
|
}
|
|
65
57
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
/// Telemetry configuration options. Construct with [TelemetryOptionsBuilder]
|
|
85
|
-
#[derive(Debug, Clone, derive_builder::Builder)]
|
|
86
|
-
#[non_exhaustive]
|
|
87
|
-
pub struct TelemetryOptions {
|
|
88
|
-
/// A string in the [EnvFilter] format which specifies what tracing data is included in
|
|
89
|
-
/// telemetry, log forwarded to lang, or console output. May be overridden by the
|
|
90
|
-
/// `TEMPORAL_TRACING_FILTER` env variable.
|
|
91
|
-
#[builder(default = "DEFAULT_FILTER.to_string()")]
|
|
92
|
-
pub tracing_filter: String,
|
|
93
|
-
|
|
94
|
-
/// Optional trace exporter - set as None to disable.
|
|
95
|
-
#[builder(setter(into, strip_option), default)]
|
|
96
|
-
pub tracing: Option<TraceExporter>,
|
|
97
|
-
/// Optional logger - set as None to disable.
|
|
98
|
-
#[builder(setter(into, strip_option), default)]
|
|
99
|
-
pub logging: Option<Logger>,
|
|
100
|
-
/// Optional metrics exporter - set as None to disable.
|
|
101
|
-
#[builder(setter(into, strip_option), default)]
|
|
102
|
-
pub metrics: Option<MetricsExporter>,
|
|
103
|
-
|
|
104
|
-
/// If set true, do not prefix metrics with `temporal_`. Will be removed eventually as
|
|
105
|
-
/// the prefix is consistent with other SDKs.
|
|
106
|
-
#[builder(default)]
|
|
107
|
-
pub no_temporal_prefix_for_metrics: bool,
|
|
108
|
-
|
|
109
|
-
/// Specifies the aggregation temporality for metric export. Defaults to cumulative.
|
|
110
|
-
#[builder(default = "MetricTemporality::Cumulative")]
|
|
111
|
-
pub metric_temporality: MetricTemporality,
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/// Types of aggregation temporality for metric export.
|
|
115
|
-
/// See: <https://github.com/open-telemetry/opentelemetry-specification/blob/ce50e4634efcba8da445cc23523243cb893905cb/specification/metrics/datamodel.md#temporality>
|
|
116
|
-
#[derive(Debug, Clone, Copy)]
|
|
117
|
-
pub enum MetricTemporality {
|
|
118
|
-
/// Successive data points repeat the starting timestamp
|
|
119
|
-
Cumulative,
|
|
120
|
-
/// Successive data points advance the starting timestamp
|
|
121
|
-
Delta,
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
impl MetricTemporality {
|
|
125
|
-
fn to_selector(self) -> impl TemporalitySelector + Send + Sync + Clone {
|
|
126
|
-
match self {
|
|
127
|
-
MetricTemporality::Cumulative => {
|
|
128
|
-
aggregation::constant_temporality_selector(Temporality::Cumulative)
|
|
129
|
-
}
|
|
130
|
-
MetricTemporality::Delta => {
|
|
131
|
-
aggregation::constant_temporality_selector(Temporality::Delta)
|
|
132
|
-
}
|
|
58
|
+
impl TelemetryInstance {
|
|
59
|
+
fn new(
|
|
60
|
+
trace_subscriber: Arc<dyn Subscriber + Send + Sync>,
|
|
61
|
+
logs_out: Option<Mutex<CoreLogsOut>>,
|
|
62
|
+
metric_prefix: &'static str,
|
|
63
|
+
mut meter_provider: Option<Box<dyn MeterProvider + Send + Sync + 'static>>,
|
|
64
|
+
keepalive_rx: Receiver<()>,
|
|
65
|
+
) -> Self {
|
|
66
|
+
let metrics = meter_provider.take().map(|mp| {
|
|
67
|
+
let meter = mp.meter(TELEM_SERVICE_NAME);
|
|
68
|
+
(mp, meter)
|
|
69
|
+
});
|
|
70
|
+
Self {
|
|
71
|
+
metric_prefix,
|
|
72
|
+
logs_out,
|
|
73
|
+
metrics,
|
|
74
|
+
trace_subscriber,
|
|
75
|
+
_keepalive_rx: keepalive_rx,
|
|
133
76
|
}
|
|
134
77
|
}
|
|
135
|
-
}
|
|
136
78
|
|
|
137
|
-
|
|
138
|
-
///
|
|
139
|
-
pub fn
|
|
140
|
-
|
|
141
|
-
DEFAULT_FILTER
|
|
142
|
-
} else {
|
|
143
|
-
&self.tracing_filter
|
|
144
|
-
})
|
|
79
|
+
/// Returns a trace subscriber which can be used with the tracing crate, or with our own
|
|
80
|
+
/// [set_trace_subscriber_for_current_thread] function.
|
|
81
|
+
pub fn trace_subscriber(&self) -> Arc<dyn Subscriber + Send + Sync> {
|
|
82
|
+
self.trace_subscriber.clone()
|
|
145
83
|
}
|
|
146
84
|
}
|
|
147
85
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
86
|
+
thread_local! {
|
|
87
|
+
static SUB_GUARD: RefCell<Option<tracing::subscriber::DefaultGuard>> = RefCell::new(None);
|
|
88
|
+
}
|
|
89
|
+
/// Set the trace subscriber for the current thread. This must be done in every thread which uses
|
|
90
|
+
/// core stuff, otherwise traces/logs will not be collected on that thread. For example, if using
|
|
91
|
+
/// a multithreaded Tokio runtime, you should ensure that said runtime uses
|
|
92
|
+
/// [on_thread_start](https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.on_thread_start)
|
|
93
|
+
/// or a similar mechanism to call this for each thread within the runtime.
|
|
94
|
+
pub fn set_trace_subscriber_for_current_thread(sub: impl Subscriber + Send + Sync + 'static) {
|
|
95
|
+
SUB_GUARD.with(|sg| {
|
|
96
|
+
if sg.borrow().is_none() {
|
|
97
|
+
let g = tracing::subscriber::set_default(sub);
|
|
98
|
+
*sg.borrow_mut() = Some(g);
|
|
99
|
+
}
|
|
100
|
+
})
|
|
152
101
|
}
|
|
153
102
|
|
|
154
|
-
///
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
core_export_logger: Option<CoreExportLogger>,
|
|
158
|
-
runtime: Option<tokio::runtime::Runtime>,
|
|
159
|
-
prom_srv: Option<PromServer>,
|
|
160
|
-
no_temporal_prefix_for_metrics: bool,
|
|
103
|
+
/// Undoes [set_trace_subscriber_for_current_thread]
|
|
104
|
+
pub fn remove_trace_subscriber_for_current_thread() {
|
|
105
|
+
SUB_GUARD.with(|sg| sg.take());
|
|
161
106
|
}
|
|
162
107
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if let Some(srv) = &self.prom_srv {
|
|
169
|
-
self.runtime
|
|
170
|
-
.as_ref()
|
|
171
|
-
.expect("Telemetry runtime is initted")
|
|
172
|
-
.spawn(srv.run());
|
|
173
|
-
}
|
|
108
|
+
fn metric_prefix(opts: &TelemetryOptions) -> &'static str {
|
|
109
|
+
if opts.no_temporal_prefix_for_metrics {
|
|
110
|
+
""
|
|
111
|
+
} else {
|
|
112
|
+
"temporal_"
|
|
174
113
|
}
|
|
175
114
|
}
|
|
176
115
|
|
|
177
|
-
impl CoreTelemetry for
|
|
116
|
+
impl CoreTelemetry for TelemetryInstance {
|
|
178
117
|
fn fetch_buffered_logs(&self) -> Vec<CoreLog> {
|
|
179
|
-
|
|
118
|
+
if let Some(logs_out) = self.logs_out.as_ref() {
|
|
119
|
+
logs_out.lock().pop_iter().collect()
|
|
120
|
+
} else {
|
|
121
|
+
vec![]
|
|
122
|
+
}
|
|
180
123
|
}
|
|
181
124
|
|
|
182
125
|
fn get_metric_meter(&self) -> Option<&Meter> {
|
|
183
|
-
|
|
184
|
-
return Some(&METRIC_METER);
|
|
185
|
-
}
|
|
186
|
-
None
|
|
126
|
+
self.metrics.as_ref().map(|(_, m)| m)
|
|
187
127
|
}
|
|
188
128
|
}
|
|
189
129
|
|
|
190
|
-
/// Initialize tracing subscribers/output and logging export
|
|
191
|
-
///
|
|
130
|
+
/// Initialize tracing subscribers/output and logging export, returning a [TelemetryInstance]
|
|
131
|
+
/// which can be used to register default / global tracing subscribers.
|
|
132
|
+
///
|
|
133
|
+
/// You should only call this once per unique [TelemetryOptions]
|
|
192
134
|
///
|
|
193
135
|
/// See [TelemetryOptions] docs for more on configuration.
|
|
194
|
-
pub fn telemetry_init(opts:
|
|
195
|
-
// TODO: Per-layer filtering has been implemented but does not yet support
|
|
196
|
-
// env-filter. When it does, allow filtering logs/telemetry separately.
|
|
197
|
-
|
|
198
|
-
// Ensure we don't pointlessly spawn threads that won't do anything or call telem dat's init 2x
|
|
199
|
-
let guard = TELETM_MUTEX.lock();
|
|
200
|
-
if let Some(gtd) = GLOBAL_TELEM_DAT.get() {
|
|
201
|
-
return Ok(gtd);
|
|
202
|
-
}
|
|
203
|
-
|
|
136
|
+
pub fn telemetry_init(opts: TelemetryOptions) -> Result<TelemetryInstance, anyhow::Error> {
|
|
204
137
|
// This is a bit odd, but functional. It's desirable to create a separate tokio runtime for
|
|
205
138
|
// metrics handling, since tests typically use a single-threaded runtime and initializing
|
|
206
139
|
// pipeline requires us to know if the runtime is single or multithreaded, we will crash
|
|
207
140
|
// in one case or the other. There does not seem to be a way to tell from the current runtime
|
|
208
141
|
// handle if it is single or multithreaded. Additionally, we can isolate metrics work this
|
|
209
142
|
// way which is nice.
|
|
210
|
-
let
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
Logger::Forward(filter) => {
|
|
245
|
-
log::set_max_level(*filter);
|
|
246
|
-
globaldat.core_export_logger = Some(CoreExportLogger::new(*filter));
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
if let Some(ref metrics) = opts.metrics {
|
|
252
|
-
match metrics {
|
|
253
|
-
MetricsExporter::Prometheus(addr) => {
|
|
254
|
-
let srv = PromServer::new(*addr, opts.metric_temporality.to_selector())?;
|
|
255
|
-
globaldat.prom_srv = Some(srv);
|
|
256
|
-
}
|
|
257
|
-
MetricsExporter::Otel(OtelCollectorOptions { url, headers }) => {
|
|
258
|
-
runtime.block_on(async {
|
|
259
|
-
let metrics = opentelemetry_otlp::new_pipeline()
|
|
260
|
-
.metrics(
|
|
261
|
-
SDKAggSelector,
|
|
262
|
-
opts.metric_temporality.to_selector(),
|
|
263
|
-
runtime::Tokio,
|
|
143
|
+
let (tx, rx) = crossbeam::channel::bounded(0);
|
|
144
|
+
let (keepalive_tx, keepalive_rx) = crossbeam::channel::bounded(0);
|
|
145
|
+
let jh = std::thread::spawn(move || -> Result<(), anyhow::Error> {
|
|
146
|
+
let runtime = tokio::runtime::Builder::new_multi_thread()
|
|
147
|
+
.thread_name("telemetry")
|
|
148
|
+
.worker_threads(2)
|
|
149
|
+
.enable_all()
|
|
150
|
+
.build()?;
|
|
151
|
+
// Parts of telem dat ====
|
|
152
|
+
let mut logs_out = None;
|
|
153
|
+
let metric_prefix = metric_prefix(&opts);
|
|
154
|
+
// =======================
|
|
155
|
+
|
|
156
|
+
// Tracing subscriber layers =========
|
|
157
|
+
let mut console_pretty_layer = None;
|
|
158
|
+
let mut console_compact_layer = None;
|
|
159
|
+
let mut forward_layer = None;
|
|
160
|
+
let mut export_layer = None;
|
|
161
|
+
// ===================================
|
|
162
|
+
|
|
163
|
+
if let Some(ref logger) = opts.logging {
|
|
164
|
+
match logger {
|
|
165
|
+
Logger::Console { filter } => {
|
|
166
|
+
// This is silly dupe but can't be avoided without boxing.
|
|
167
|
+
if env::var("TEMPORAL_CORE_PRETTY_LOGS").is_ok() {
|
|
168
|
+
console_pretty_layer = Some(
|
|
169
|
+
tracing_subscriber::fmt::layer()
|
|
170
|
+
.with_target(false)
|
|
171
|
+
.event_format(
|
|
172
|
+
tracing_subscriber::fmt::format()
|
|
173
|
+
.pretty()
|
|
174
|
+
.with_source_location(false),
|
|
264
175
|
)
|
|
265
|
-
.
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
.
|
|
274
|
-
|
|
275
|
-
)),
|
|
176
|
+
.with_filter(EnvFilter::new(filter)),
|
|
177
|
+
)
|
|
178
|
+
} else {
|
|
179
|
+
console_compact_layer = Some(
|
|
180
|
+
tracing_subscriber::fmt::layer()
|
|
181
|
+
.with_target(false)
|
|
182
|
+
.event_format(
|
|
183
|
+
tracing_subscriber::fmt::format()
|
|
184
|
+
.compact()
|
|
185
|
+
.with_source_location(false),
|
|
276
186
|
)
|
|
277
|
-
.
|
|
278
|
-
|
|
279
|
-
Result::<(), anyhow::Error>::Ok(())
|
|
280
|
-
})?;
|
|
187
|
+
.with_filter(EnvFilter::new(filter)),
|
|
188
|
+
)
|
|
281
189
|
}
|
|
282
|
-
}
|
|
190
|
+
}
|
|
191
|
+
Logger::Forward { filter } => {
|
|
192
|
+
let (export_layer, lo) = CoreLogExportLayer::new();
|
|
193
|
+
logs_out = Some(Mutex::new(lo));
|
|
194
|
+
forward_layer = Some(export_layer.with_filter(EnvFilter::new(filter)));
|
|
195
|
+
}
|
|
283
196
|
};
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
let meter_provider = if let Some(ref metrics) = opts.metrics {
|
|
200
|
+
let aggregator = SDKAggSelector { metric_prefix };
|
|
201
|
+
match metrics {
|
|
202
|
+
MetricsExporter::Prometheus(addr) => {
|
|
203
|
+
let srv = PromServer::new(
|
|
204
|
+
*addr,
|
|
205
|
+
aggregator,
|
|
206
|
+
metric_temporality_to_selector(opts.metric_temporality),
|
|
207
|
+
)?;
|
|
208
|
+
let mp = srv.exporter.meter_provider()?;
|
|
209
|
+
runtime.spawn(async move { srv.run().await });
|
|
210
|
+
Some(Box::new(mp) as Box<dyn MeterProvider + Send + Sync>)
|
|
211
|
+
}
|
|
212
|
+
MetricsExporter::Otel(OtelCollectorOptions {
|
|
213
|
+
url,
|
|
214
|
+
headers,
|
|
215
|
+
metric_periodicity,
|
|
216
|
+
}) => runtime.block_on(async {
|
|
217
|
+
let metrics = opentelemetry_otlp::new_pipeline()
|
|
218
|
+
.metrics(
|
|
219
|
+
aggregator,
|
|
220
|
+
metric_temporality_to_selector(opts.metric_temporality),
|
|
221
|
+
runtime::Tokio,
|
|
222
|
+
)
|
|
223
|
+
.with_period(metric_periodicity.unwrap_or_else(|| Duration::from_secs(1)))
|
|
224
|
+
.with_resource(default_resource())
|
|
225
|
+
.with_exporter(
|
|
226
|
+
opentelemetry_otlp::new_exporter()
|
|
227
|
+
.tonic()
|
|
228
|
+
.with_endpoint(url.to_string())
|
|
229
|
+
.with_metadata(MetadataMap::from_headers(headers.try_into()?)),
|
|
230
|
+
)
|
|
231
|
+
.build()?;
|
|
232
|
+
Ok::<_, anyhow::Error>(Some(
|
|
233
|
+
Box::new(metrics) as Box<dyn MeterProvider + Send + Sync>
|
|
234
|
+
))
|
|
235
|
+
})?,
|
|
236
|
+
}
|
|
237
|
+
} else {
|
|
238
|
+
None
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
if let Some(ref tracing) = opts.tracing {
|
|
242
|
+
match &tracing.exporter {
|
|
243
|
+
TraceExporter::Otel(OtelCollectorOptions { url, headers, .. }) => {
|
|
244
|
+
runtime.block_on(async {
|
|
245
|
+
let tracer_cfg = Config::default().with_resource(default_resource());
|
|
246
|
+
let tracer = opentelemetry_otlp::new_pipeline()
|
|
247
|
+
.tracing()
|
|
248
|
+
.with_exporter(
|
|
249
|
+
opentelemetry_otlp::new_exporter()
|
|
250
|
+
.tonic()
|
|
251
|
+
.with_endpoint(url.to_string())
|
|
252
|
+
.with_metadata(MetadataMap::from_headers(headers.try_into()?)),
|
|
253
|
+
)
|
|
254
|
+
.with_trace_config(tracer_cfg)
|
|
255
|
+
.install_batch(runtime::Tokio)?;
|
|
256
|
+
|
|
257
|
+
let opentelemetry = tracing_opentelemetry::layer()
|
|
258
|
+
.with_tracer(tracer)
|
|
259
|
+
.with_filter(EnvFilter::new(&tracing.filter));
|
|
260
|
+
|
|
261
|
+
export_layer = Some(opentelemetry);
|
|
262
|
+
Result::<(), anyhow::Error>::Ok(())
|
|
263
|
+
})?;
|
|
264
|
+
}
|
|
331
265
|
};
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
let reg = tracing_subscriber::registry()
|
|
269
|
+
.with(console_pretty_layer)
|
|
270
|
+
.with(console_compact_layer)
|
|
271
|
+
.with(forward_layer)
|
|
272
|
+
.with(export_layer);
|
|
273
|
+
|
|
274
|
+
tx.send(TelemetryInstance::new(
|
|
275
|
+
Arc::new(reg),
|
|
276
|
+
logs_out,
|
|
277
|
+
metric_prefix,
|
|
278
|
+
meter_provider,
|
|
279
|
+
keepalive_rx,
|
|
280
|
+
))
|
|
281
|
+
.expect("Must be able to send telem instance out of thread");
|
|
282
|
+
// Now keep the thread alive until the telemetry instance is dropped by trying to send
|
|
283
|
+
// something forever
|
|
284
|
+
let _ = keepalive_tx.send(());
|
|
285
|
+
Ok(())
|
|
286
|
+
});
|
|
287
|
+
match rx.recv() {
|
|
288
|
+
Ok(ti) => Ok(ti),
|
|
289
|
+
Err(_) => {
|
|
290
|
+
// Immediately join the thread since something went wrong in it
|
|
291
|
+
jh.join().expect("Telemetry must init cleanly")?;
|
|
292
|
+
// This can't happen. The rx channel can't be dropped unless the thread errored.
|
|
293
|
+
unreachable!("Impossible error in telemetry init thread");
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
332
297
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
298
|
+
/// Initialize telemetry/tracing globally. Useful for testing.
|
|
299
|
+
pub fn telemetry_init_global(opts: TelemetryOptions) -> Result<(), anyhow::Error> {
|
|
300
|
+
let ti = telemetry_init(opts)?;
|
|
301
|
+
tracing::subscriber::set_global_default(ti.trace_subscriber())?;
|
|
302
|
+
Ok(())
|
|
303
|
+
}
|
|
336
304
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
305
|
+
fn default_resource_kvs() -> &'static [KeyValue] {
|
|
306
|
+
static INSTANCE: OnceCell<[KeyValue; 1]> = OnceCell::new();
|
|
307
|
+
INSTANCE.get_or_init(|| [KeyValue::new("service.name", TELEM_SERVICE_NAME)])
|
|
308
|
+
}
|
|
309
|
+
fn default_resource() -> Resource {
|
|
310
|
+
Resource::new(default_resource_kvs().iter().cloned())
|
|
342
311
|
}
|
|
343
312
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
} else {
|
|
353
|
-
vec![]
|
|
313
|
+
fn metric_temporality_to_selector(
|
|
314
|
+
t: MetricTemporality,
|
|
315
|
+
) -> impl TemporalitySelector + Send + Sync + Clone {
|
|
316
|
+
match t {
|
|
317
|
+
MetricTemporality::Cumulative => {
|
|
318
|
+
aggregation::constant_temporality_selector(Temporality::Cumulative)
|
|
319
|
+
}
|
|
320
|
+
MetricTemporality::Delta => aggregation::constant_temporality_selector(Temporality::Delta),
|
|
354
321
|
}
|
|
355
322
|
}
|
|
356
323
|
|
|
357
|
-
#[allow(dead_code)] // Not always used, called to enable for debugging when needed
|
|
358
324
|
#[cfg(test)]
|
|
359
|
-
pub
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
325
|
+
pub mod test_initters {
|
|
326
|
+
use super::*;
|
|
327
|
+
use temporal_sdk_core_api::telemetry::{TelemetryOptionsBuilder, TraceExportConfig};
|
|
328
|
+
|
|
329
|
+
#[allow(dead_code)] // Not always used, called to enable for debugging when needed
|
|
330
|
+
pub fn test_telem_console() {
|
|
331
|
+
telemetry_init_global(
|
|
332
|
+
TelemetryOptionsBuilder::default()
|
|
333
|
+
.logging(Logger::Console {
|
|
334
|
+
filter: construct_filter_string(Level::DEBUG, Level::WARN),
|
|
335
|
+
})
|
|
336
|
+
.build()
|
|
337
|
+
.unwrap(),
|
|
338
|
+
)
|
|
339
|
+
.unwrap();
|
|
340
|
+
}
|
|
370
341
|
|
|
371
|
-
#[allow(dead_code)] // Not always used, called to enable for debugging when needed
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
342
|
+
#[allow(dead_code)] // Not always used, called to enable for debugging when needed
|
|
343
|
+
pub fn test_telem_collector() {
|
|
344
|
+
telemetry_init_global(
|
|
345
|
+
TelemetryOptionsBuilder::default()
|
|
346
|
+
.logging(Logger::Console {
|
|
347
|
+
filter: construct_filter_string(Level::DEBUG, Level::WARN),
|
|
348
|
+
})
|
|
349
|
+
.tracing(TraceExportConfig {
|
|
350
|
+
filter: construct_filter_string(Level::DEBUG, Level::WARN),
|
|
351
|
+
exporter: TraceExporter::Otel(OtelCollectorOptions {
|
|
352
|
+
url: "grpc://localhost:4317".parse().unwrap(),
|
|
353
|
+
headers: Default::default(),
|
|
354
|
+
metric_periodicity: None,
|
|
355
|
+
}),
|
|
356
|
+
})
|
|
357
|
+
.build()
|
|
358
|
+
.unwrap(),
|
|
359
|
+
)
|
|
360
|
+
.unwrap();
|
|
361
|
+
}
|
|
386
362
|
}
|
|
363
|
+
#[cfg(test)]
|
|
364
|
+
pub use test_initters::*;
|
|
387
365
|
|
|
388
366
|
/// A trait for using [Display] on the contents of vecs, etc, which don't implement it.
|
|
389
367
|
///
|