Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions ext/telemetry/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,12 +1388,7 @@ impl OtelSpan {
}

#[fast]
fn add_event(
&self,
#[string] name: String,
start_time: f64,
#[smi] dropped_attributes_count: u32,
) {
fn add_event(&self, #[string] name: String, start_time: f64) {
let start_time = if start_time.is_nan() {
SystemTime::now()
} else {
Expand All @@ -1405,12 +1400,10 @@ impl OtelSpan {
let OtelSpanState::Recording(span) = &mut **state else {
return;
};
span.events.events.push(Event::new(
name,
start_time,
vec![],
dropped_attributes_count,
));
span
.events
.events
.push(Event::new(name, start_time, vec![], 0));
}

#[fast]
Expand Down Expand Up @@ -1452,10 +1445,34 @@ impl OtelSpan {
}
}

fn span_attributes(
span: &mut SpanData,
location: u32,
) -> Option<(&mut Vec<KeyValue>, &mut u32)> {
match location {
// SELF
0 => Some((&mut span.attributes, &mut span.dropped_attributes_count)),
// LAST_EVENT
1 => span
.events
.events
.last_mut()
.map(|e| (&mut e.attributes, &mut e.dropped_attributes_count)),
// LAST_LINK
2 => span
.links
.links
.last_mut()
.map(|e| (&mut e.attributes, &mut e.dropped_attributes_count)),
_ => None,
}
}

#[op2(fast)]
fn op_otel_span_attribute1<'s>(
scope: &mut v8::HandleScope<'s>,
span: v8::Local<'_, v8::Value>,
#[smi] location: u32,
key: v8::Local<'s, v8::Value>,
value: v8::Local<'s, v8::Value>,
) {
Expand All @@ -1466,14 +1483,20 @@ fn op_otel_span_attribute1<'s>(
};
let mut state = span.0.borrow_mut();
if let OtelSpanState::Recording(span) = &mut **state {
attr!(scope, span.attributes => span.dropped_attributes_count, key, value);
let Some((attributes, dropped_attributes_count)) =
span_attributes(span, location)
else {
return;
};
attr!(scope, attributes => *dropped_attributes_count, key, value);
}
}

#[op2(fast)]
fn op_otel_span_attribute2<'s>(
scope: &mut v8::HandleScope<'s>,
span: v8::Local<'_, v8::Value>,
#[smi] location: u32,
key1: v8::Local<'s, v8::Value>,
value1: v8::Local<'s, v8::Value>,
key2: v8::Local<'s, v8::Value>,
Expand All @@ -1486,8 +1509,13 @@ fn op_otel_span_attribute2<'s>(
};
let mut state = span.0.borrow_mut();
if let OtelSpanState::Recording(span) = &mut **state {
attr!(scope, span.attributes => span.dropped_attributes_count, key1, value1);
attr!(scope, span.attributes => span.dropped_attributes_count, key2, value2);
let Some((attributes, dropped_attributes_count)) =
span_attributes(span, location)
else {
return;
};
attr!(scope, attributes => *dropped_attributes_count, key1, value1);
attr!(scope, attributes => *dropped_attributes_count, key2, value2);
}
}

Expand All @@ -1496,6 +1524,7 @@ fn op_otel_span_attribute2<'s>(
fn op_otel_span_attribute3<'s>(
scope: &mut v8::HandleScope<'s>,
span: v8::Local<'_, v8::Value>,
#[smi] location: u32,
key1: v8::Local<'s, v8::Value>,
value1: v8::Local<'s, v8::Value>,
key2: v8::Local<'s, v8::Value>,
Expand All @@ -1510,9 +1539,14 @@ fn op_otel_span_attribute3<'s>(
};
let mut state = span.0.borrow_mut();
if let OtelSpanState::Recording(span) = &mut **state {
attr!(scope, span.attributes => span.dropped_attributes_count, key1, value1);
attr!(scope, span.attributes => span.dropped_attributes_count, key2, value2);
attr!(scope, span.attributes => span.dropped_attributes_count, key3, value3);
let Some((attributes, dropped_attributes_count)) =
span_attributes(span, location)
else {
return;
};
attr!(scope, attributes => *dropped_attributes_count, key1, value1);
attr!(scope, attributes => *dropped_attributes_count, key2, value2);
attr!(scope, attributes => *dropped_attributes_count, key3, value3);
}
}

Expand Down
153 changes: 110 additions & 43 deletions ext/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ interface Attributes {

type SpanAttributes = Attributes;

interface Exception {
code?: string | number;
message?: string;
name?: string;
stack?: string;
}

type TimeInput = [number, number] | number | Date;

interface SpanOptions {
Expand Down Expand Up @@ -240,12 +247,59 @@ interface OtelSpan {
addEvent(
name: string,
startTime: number,
droppedAttributeCount: number,
): void;
dropEvent(): void;
end(endTime: number): void;
}

enum SpanAttributesLocation {
SELF = 0,
LAST_EVENT = 1,
LAST_LINK = 2,
}

function spanAddAttributes(
span: OtelSpan,
attributesLocation: SpanAttributesLocation,
attributes: Attributes,
) {
const attributeKvs = ObjectEntries(attributes);
let i = 0;
while (i < attributeKvs.length) {
if (i + 2 < attributeKvs.length) {
op_otel_span_attribute3(
span,
attributesLocation,
attributeKvs[i][0],
attributeKvs[i][1],
attributeKvs[i + 1][0],
attributeKvs[i + 1][1],
attributeKvs[i + 2][0],
attributeKvs[i + 2][1],
);
i += 3;
} else if (i + 1 < attributeKvs.length) {
op_otel_span_attribute2(
span,
attributesLocation,
attributeKvs[i][0],
attributeKvs[i][1],
attributeKvs[i + 1][0],
attributeKvs[i + 1][1],
);
i += 2;
} else {
op_otel_span_attribute1(
span,
attributesLocation,
attributeKvs[i][0],
attributeKvs[i][1],
);
i += 1;
}
}
}

interface TracerOptions {
schemaUrl?: string;
}
Expand Down Expand Up @@ -404,31 +458,46 @@ class Span {
attributesOrStartTime?: Attributes | TimeInput,
startTime?: TimeInput,
): this {
if (!this.#otelSpan) return this;
let attributes: Attributes | undefined;
if (isTimeInput(attributesOrStartTime)) {
startTime = attributesOrStartTime;
attributesOrStartTime = undefined;
} else {
attributes = attributesOrStartTime;
}
const startTimeMs = timeInputToMs(startTime);

this.#otelSpan?.addEvent(
this.#otelSpan.addEvent(
name,
startTimeMs ?? NaN,
countAttributes(attributesOrStartTime),
);
if (attributes) {
spanAddAttributes(
this.#otelSpan,
SpanAttributesLocation.LAST_EVENT,
attributes,
);
}
return this;
}

addLink(link: Link): this {
const droppedAttributeCount = (link.droppedAttributesCount ?? 0) +
countAttributes(link.attributes);
if (!this.#otelSpan) return this;
const valid = op_otel_span_add_link(
this.#otelSpan,
link.context.traceId,
link.context.spanId,
link.context.traceFlags,
link.context.isRemote ?? false,
droppedAttributeCount,
link.droppedAttributesCount ?? 0,
);
if (link.attributes) {
spanAddAttributes(
this.#otelSpan,
SpanAttributesLocation.LAST_LINK,
link.attributes,
);
}
if (!valid) return this;
return this;
}
Expand All @@ -448,51 +517,49 @@ class Span {
return this.#otelSpan !== undefined;
}

// deno-lint-ignore no-explicit-any
recordException(_exception: any, _time?: TimeInput): void {
this.#otelSpan?.dropEvent();
recordException(exception: string | Exception, time?: TimeInput): void {
if (typeof exception === "string") {
this.addEvent("exception", {
"exception.message": exception,
}, time);
return;
}
const attributes: Attributes = {};

if (exception.code) {
if (typeof exception.code === "number") {
attributes["exception.type"] = NumberPrototypeToString(exception.code);
} else {
attributes["exception.type"] = exception.code;
}
} else if (exception.name) {
attributes["exception.type"] = exception.name;
}

if (exception.message) {
attributes["exception.message"] = exception.message;
}
if (exception.stack) {
attributes["exception.stacktrace"] = exception.stack;
}

this.addEvent("exception", attributes, time);
}

setAttribute(key: string, value: AttributeValue): this {
if (!this.#otelSpan) return this;
op_otel_span_attribute1(this.#otelSpan, key, value);
op_otel_span_attribute1(
this.#otelSpan,
SpanAttributesLocation.SELF,
key,
value,
);
return this;
}

setAttributes(attributes: Attributes): this {
if (!this.#otelSpan) return this;
const attributeKvs = ObjectEntries(attributes);
let i = 0;
while (i < attributeKvs.length) {
if (i + 2 < attributeKvs.length) {
op_otel_span_attribute3(
this.#otelSpan,
attributeKvs[i][0],
attributeKvs[i][1],
attributeKvs[i + 1][0],
attributeKvs[i + 1][1],
attributeKvs[i + 2][0],
attributeKvs[i + 2][1],
);
i += 3;
} else if (i + 1 < attributeKvs.length) {
op_otel_span_attribute2(
this.#otelSpan,
attributeKvs[i][0],
attributeKvs[i][1],
attributeKvs[i + 1][0],
attributeKvs[i + 1][1],
);
i += 2;
} else {
op_otel_span_attribute1(
this.#otelSpan,
attributeKvs[i][0],
attributeKvs[i][1],
);
i += 1;
}
}
spanAddAttributes(this.#otelSpan, SpanAttributesLocation.SELF, attributes);
return this;
}

Expand Down
Loading