Skip to content

Commit c1747ab

Browse files
committed
cleanup
1 parent 9b48241 commit c1747ab

File tree

3 files changed

+95
-86
lines changed

3 files changed

+95
-86
lines changed

ext/telemetry/lib.rs

+49-50
Original file line numberDiff line numberDiff line change
@@ -357,27 +357,33 @@ impl DenoPeriodicReader {
357357
.with_temporality(PushMetricExporter::temporality(&exporter))
358358
.build();
359359

360-
let collect_and_export = || async {
361-
let mut resource_metrics =
362-
opentelemetry_sdk::metrics::data::ResourceMetrics {
363-
resource: Default::default(),
364-
scope_metrics: Default::default(),
365-
};
366-
let callbacks = {
367-
let mut callbacks = OTEL_PRE_COLLECT_CALLBACKS.lock().unwrap();
368-
std::mem::take(&mut *callbacks)
369-
};
370-
let mut futures = JoinSet::new();
371-
for callback in callbacks {
372-
let (tx, rx) = oneshot::channel();
373-
if let Ok(()) = callback.send(tx) {
374-
futures.spawn(rx);
360+
let collect_and_export = |collect_observed: bool| {
361+
let inner = &inner;
362+
let exporter = &exporter;
363+
async move {
364+
let mut resource_metrics =
365+
opentelemetry_sdk::metrics::data::ResourceMetrics {
366+
resource: Default::default(),
367+
scope_metrics: Default::default(),
368+
};
369+
if collect_observed {
370+
let callbacks = {
371+
let mut callbacks = OTEL_PRE_COLLECT_CALLBACKS.lock().unwrap();
372+
std::mem::take(&mut *callbacks)
373+
};
374+
let mut futures = JoinSet::new();
375+
for callback in callbacks {
376+
let (tx, rx) = oneshot::channel();
377+
if let Ok(()) = callback.send(tx) {
378+
futures.spawn(rx);
379+
}
380+
}
381+
while futures.join_next().await.is_some() {}
375382
}
383+
inner.collect(&mut resource_metrics)?;
384+
exporter.export(&mut resource_metrics).await?;
385+
Ok(())
376386
}
377-
while let Some(_) = futures.join_next().await {}
378-
inner.collect(&mut resource_metrics)?;
379-
exporter.export(&mut resource_metrics).await?;
380-
Ok(())
381387
};
382388

383389
let mut ticker = tokio::time::interval(interval);
@@ -403,7 +409,7 @@ impl DenoPeriodicReader {
403409
name: "DenoPeriodicReader.ExportTriggered",
404410
message = "Export message received.",
405411
);
406-
if let Err(err) = collect_and_export().await {
412+
if let Err(err) = collect_and_export(true).await {
407413
otel_error!(
408414
name: "DenoPeriodicReader.ExportFailed",
409415
message = "Failed to export metrics",
@@ -415,7 +421,7 @@ impl DenoPeriodicReader {
415421
name: "DenoPeriodicReader.ForceFlushCalled",
416422
message = "Flush message received.",
417423
);
418-
let res = collect_and_export().await;
424+
let res = collect_and_export(false).await;
419425
if let Err(send_error) = sender.send(res) {
420426
otel_debug!(
421427
name: "DenoPeriodicReader.Flush.SendResultError",
@@ -429,7 +435,7 @@ impl DenoPeriodicReader {
429435
name: "DenoPeriodicReader.ShutdownCalled",
430436
message = "Shutdown message received",
431437
);
432-
let res = collect_and_export().await;
438+
let res = collect_and_export(false).await;
433439
let _ = exporter.shutdown();
434440
if let Err(send_error) = sender.send(res) {
435441
otel_debug!(
@@ -1409,7 +1415,7 @@ fn op_otel_metric_record0(
14091415
Instrument::UpDownCounter(counter) => counter.add(value, attributes),
14101416
Instrument::Gauge(gauge) => gauge.record(value, attributes),
14111417
Instrument::Histogram(histogram) => histogram.record(value, attributes),
1412-
_ => return,
1418+
_ => {}
14131419
}
14141420
}
14151421

@@ -1448,10 +1454,11 @@ fn op_otel_metric_record1(
14481454
Instrument::UpDownCounter(counter) => counter.add(value, attributes),
14491455
Instrument::Gauge(gauge) => gauge.record(value, attributes),
14501456
Instrument::Histogram(histogram) => histogram.record(value, attributes),
1451-
_ => return,
1457+
_ => {}
14521458
}
14531459
}
14541460

1461+
#[allow(clippy::too_many_arguments)]
14551462
#[op2(fast)]
14561463
fn op_otel_metric_record2(
14571464
state: &mut OpState,
@@ -1495,10 +1502,11 @@ fn op_otel_metric_record2(
14951502
Instrument::UpDownCounter(counter) => counter.add(value, attributes),
14961503
Instrument::Gauge(gauge) => gauge.record(value, attributes),
14971504
Instrument::Histogram(histogram) => histogram.record(value, attributes),
1498-
_ => return,
1505+
_ => {}
14991506
}
15001507
}
15011508

1509+
#[allow(clippy::too_many_arguments)]
15021510
#[op2(fast)]
15031511
fn op_otel_metric_record3(
15041512
state: &mut OpState,
@@ -1552,7 +1560,7 @@ fn op_otel_metric_record3(
15521560
Instrument::UpDownCounter(counter) => counter.add(value, attributes),
15531561
Instrument::Gauge(gauge) => gauge.record(value, attributes),
15541562
Instrument::Histogram(histogram) => histogram.record(value, attributes),
1555-
_ => return,
1563+
_ => {}
15561564
}
15571565
}
15581566

@@ -1564,12 +1572,9 @@ fn op_otel_metric_observable_record0(
15641572
) {
15651573
let values = state.try_take::<MetricAttributes>();
15661574
let attributes = values.map(|attr| attr.attributes).unwrap_or_default();
1567-
match instrument {
1568-
Instrument::Observable(data_share) => {
1569-
let mut data = data_share.lock().unwrap();
1570-
data.insert(attributes, value);
1571-
}
1572-
_ => return,
1575+
if let Instrument::Observable(data_share) = instrument {
1576+
let mut data = data_share.lock().unwrap();
1577+
data.insert(attributes, value);
15731578
}
15741579
}
15751580

@@ -1599,15 +1604,13 @@ fn op_otel_metric_observable_record1(
15991604
if let Some(kv1) = attr1 {
16001605
attributes.push(kv1);
16011606
}
1602-
match &*instrument {
1603-
Instrument::Observable(data_share) => {
1604-
let mut data = data_share.lock().unwrap();
1605-
data.insert(attributes, value);
1606-
}
1607-
_ => return,
1607+
if let Instrument::Observable(data_share) = &*instrument {
1608+
let mut data = data_share.lock().unwrap();
1609+
data.insert(attributes, value);
16081610
}
16091611
}
16101612

1613+
#[allow(clippy::too_many_arguments)]
16111614
#[op2(fast)]
16121615
fn op_otel_metric_observable_record2(
16131616
state: &mut OpState,
@@ -1640,15 +1643,13 @@ fn op_otel_metric_observable_record2(
16401643
if let Some(kv2) = attr2 {
16411644
attributes.push(kv2);
16421645
}
1643-
match &*instrument {
1644-
Instrument::Observable(data_share) => {
1645-
let mut data = data_share.lock().unwrap();
1646-
data.insert(attributes, value);
1647-
}
1648-
_ => return,
1646+
if let Instrument::Observable(data_share) = &*instrument {
1647+
let mut data = data_share.lock().unwrap();
1648+
data.insert(attributes, value);
16491649
}
16501650
}
16511651

1652+
#[allow(clippy::too_many_arguments)]
16521653
#[op2(fast)]
16531654
fn op_otel_metric_observable_record3(
16541655
state: &mut OpState,
@@ -1687,15 +1688,13 @@ fn op_otel_metric_observable_record3(
16871688
if let Some(kv3) = attr3 {
16881689
attributes.push(kv3);
16891690
}
1690-
match &*instrument {
1691-
Instrument::Observable(data_share) => {
1692-
let mut data = data_share.lock().unwrap();
1693-
data.insert(attributes, value);
1694-
}
1695-
_ => return,
1691+
if let Instrument::Observable(data_share) = &*instrument {
1692+
let mut data = data_share.lock().unwrap();
1693+
data.insert(attributes, value);
16961694
}
16971695
}
16981696

1697+
#[allow(clippy::too_many_arguments)]
16991698
#[op2(fast)]
17001699
fn op_otel_metric_attribute3<'s>(
17011700
scope: &mut v8::HandleScope<'s>,

ext/telemetry/telemetry.ts

+38-21
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,27 @@ import { Console } from "ext:deno_console/01_console.js";
3737
import { performance } from "ext:deno_web/15_performance.js";
3838

3939
const {
40-
SafeWeakSet,
41-
SafeSet,
42-
SafeWeakMap,
4340
Array,
44-
ObjectEntries,
45-
ReflectApply,
46-
SymbolFor,
41+
ArrayPrototypePush,
4742
Error,
48-
Uint8Array,
49-
TypedArrayPrototypeSubarray,
5043
ObjectAssign,
5144
ObjectDefineProperty,
52-
WeakRefPrototypeDeref,
53-
String,
54-
StringPrototypePadStart,
45+
ObjectEntries,
5546
ObjectPrototypeIsPrototypeOf,
47+
ReflectApply,
48+
SafeIterator,
49+
SafeMap,
50+
SafePromiseAll,
51+
SafeSet,
52+
SafeWeakMap,
5653
SafeWeakRef,
54+
SafeWeakSet,
55+
String,
56+
StringPrototypePadStart,
57+
SymbolFor,
58+
TypedArrayPrototypeSubarray,
59+
Uint8Array,
60+
WeakRefPrototypeDeref,
5761
} = primordials;
5862
const { AsyncVariable, setAsyncContext } = core;
5963

@@ -748,7 +752,7 @@ class BatchObservableResult {
748752

749753
static {
750754
batchResultHasObservables = (cb, observables) => {
751-
for (const observable of observables) {
755+
for (const observable of new SafeIterator(observables)) {
752756
if (!cb.#observables.has(observable)) return false;
753757
}
754758
return true;
@@ -788,6 +792,7 @@ class Meter {
788792
activateInstrumentationLibrary(this.#instrumentationLibrary);
789793
const instrument = op_otel_metric_create_counter(
790794
name,
795+
// deno-lint-ignore prefer-primordials
791796
options?.description,
792797
options?.unit,
793798
) as Instrument;
@@ -804,6 +809,7 @@ class Meter {
804809
activateInstrumentationLibrary(this.#instrumentationLibrary);
805810
const instrument = op_otel_metric_create_up_down_counter(
806811
name,
812+
// deno-lint-ignore prefer-primordials
807813
options?.description,
808814
options?.unit,
809815
) as Instrument;
@@ -820,6 +826,7 @@ class Meter {
820826
activateInstrumentationLibrary(this.#instrumentationLibrary);
821827
const instrument = op_otel_metric_create_gauge(
822828
name,
829+
// deno-lint-ignore prefer-primordials
823830
options?.description,
824831
options?.unit,
825832
) as Instrument;
@@ -836,6 +843,7 @@ class Meter {
836843
activateInstrumentationLibrary(this.#instrumentationLibrary);
837844
const instrument = op_otel_metric_create_histogram(
838845
name,
846+
// deno-lint-ignore prefer-primordials
839847
options?.description,
840848
options?.unit,
841849
options?.advice?.explicitBucketBoundaries,
@@ -853,6 +861,7 @@ class Meter {
853861
activateInstrumentationLibrary(this.#instrumentationLibrary);
854862
const instrument = op_otel_metric_create_observable_counter(
855863
name,
864+
// deno-lint-ignore prefer-primordials
856865
options?.description,
857866
options?.unit,
858867
) as Instrument;
@@ -869,6 +878,7 @@ class Meter {
869878
activateInstrumentationLibrary(this.#instrumentationLibrary);
870879
const instrument = op_otel_metric_create_observable_gauge(
871880
name,
881+
// deno-lint-ignore prefer-primordials
872882
options?.description,
873883
options?.unit,
874884
) as Instrument;
@@ -885,6 +895,7 @@ class Meter {
885895
activateInstrumentationLibrary(this.#instrumentationLibrary);
886896
const instrument = op_otel_metric_create_observable_up_down_counter(
887897
name,
898+
// deno-lint-ignore prefer-primordials
888899
options?.description,
889900
options?.unit,
890901
) as Instrument;
@@ -895,7 +906,7 @@ class Meter {
895906
callback: BatchObservableCallback,
896907
observables: Observable[],
897908
): void {
898-
const result = new BatchObservableResult(new WeakSet(observables));
909+
const result = new BatchObservableResult(new SafeWeakSet(observables));
899910
startObserving();
900911
BATCH_CALLBACKS.set(callback, result);
901912
}
@@ -1048,7 +1059,7 @@ class Counter {
10481059
this.#upDown = upDown;
10491060
}
10501061

1051-
add(value: number, attributes?: MetricAttributes, context?: Context): void {
1062+
add(value: number, attributes?: MetricAttributes, _context?: Context): void {
10521063
if (value < 0 && !this.#upDown) {
10531064
throw new Error("Counter can only be incremented");
10541065
}
@@ -1066,7 +1077,7 @@ class Gauge {
10661077
record(
10671078
value: number,
10681079
attributes?: MetricAttributes,
1069-
context?: Context,
1080+
_context?: Context,
10701081
): void {
10711082
record(this.#instrument, value, attributes);
10721083
}
@@ -1082,7 +1093,7 @@ class Histogram {
10821093
record(
10831094
value: number,
10841095
attributes?: MetricAttributes,
1085-
context?: Context,
1096+
_context?: Context,
10861097
): void {
10871098
record(this.#instrument, value, attributes);
10881099
}
@@ -1144,16 +1155,22 @@ class ObservableResult {
11441155

11451156
async function observe(): Promise<void> {
11461157
const promises: Promise<void>[] = [];
1147-
for (const [observable, callbacks] of INDIVIDUAL_CALLBACKS) {
1158+
// Primordials are not needed, because this is a SafeMap.
1159+
// deno-lint-ignore prefer-primordials
1160+
for (const { 0: observable, 1: callbacks } of INDIVIDUAL_CALLBACKS) {
11481161
const result = getObservableResult(observable);
1162+
// Primordials are not needed, because this is a SafeSet.
1163+
// deno-lint-ignore prefer-primordials
11491164
for (const callback of callbacks) {
1150-
promises.push(Promise.try(callback, result));
1165+
ArrayPrototypePush(promises, PromiseTry(callback, result));
11511166
}
11521167
}
1153-
for (const [callback, result] of BATCH_CALLBACKS) {
1154-
promises.push(Promise.try(() => callback(result)));
1168+
// Primordials are not needed, because this is a SafeMap.
1169+
// deno-lint-ignore prefer-primordials
1170+
for (const { 0: callback, 1: result } of BATCH_CALLBACKS) {
1171+
ArrayPrototypePush(promises, PromiseTry(callback, result));
11551172
}
1156-
await Promise.all(promises);
1173+
await SafePromiseAll(promises);
11571174
}
11581175

11591176
let isObserving = false;

0 commit comments

Comments
 (0)