Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): memory allocation in filtered spans #104

Merged
merged 4 commits into from
Aug 21, 2023
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
2 changes: 1 addition & 1 deletion go/trace_ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ func (t *TraceCtx) init(ctx context.Context, r wazero.Runtime) error {
f.within = append(f.within, e)
}
}
t.pushFunction(f)
}
t.pushFunction(f)
return
}

Expand Down
13 changes: 12 additions & 1 deletion js/src/lib/adapters/datadog/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,19 @@ export class Trace {
}
}

const allocationKey = 'allocation';

export const addAllocation = (span: Span, amount: MemoryGrowAmount) => {
span.meta.set("allocation", amount.toString());
let sumAmount = amount;
let existingAllocation = span.meta[allocationKey];
if (existingAllocation) {
try {
sumAmount = parseInt(existingAllocation) + amount;
} catch (e) {
console.error(e);
}
}
span.meta.set(allocationKey, sumAmount.toString());
};

export class DatadogFormatter {
Expand Down
10 changes: 10 additions & 0 deletions js/src/lib/collectors/span/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ export class SpanCollector implements Collector {
const funcDuration = fn.duration() * 1e-3;
const minSpanDuration = this.opts.spanFilter.minDurationMicroseconds;
if (funcDuration < minSpanDuration) {
// check for memory allocations and attribute them to the parent span before filtering
const f = this.stack.pop();
if (f) {
fn.within.forEach((ev) => {
if (ev instanceof MemoryGrow) {
f.within.push(ev);
}
});
this.stack.push(f);
}
return;
}

Expand Down
32 changes: 23 additions & 9 deletions js/src/lib/formatters/opentelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function traceFromEvents(serviceName: string, events: ObserveEvent[]): Tr
return trace;
}

const allocationKey = 'allocation';
/**
*
* @param trace - all spans created will be tied to this trace
Expand All @@ -53,17 +54,30 @@ function eventToSpans(trace: Trace, spans: Span[], ev: ObserveEvent, parentId?:

ev.within.forEach((e) => {
eventToSpans(trace, spans, e, span.spanId);
})
});
}
else if (ev instanceof MemoryGrow) {
const span = newSpan(trace, 'allocation', ev.start, ev.start, parentId);
span.attributes.push({
key: 'amount',
value: {
intValue: ev.amount,
}
})
spans.push(span);
const span = spans[spans.length - 1];
let existingIndex = -1;
const existing = span.attributes.find((a, i) => {
existingIndex = i;
return a.key === allocationKey
});
if (existing) {
span.attributes[existingIndex] = {
key: allocationKey,
value: {
intValue: ev.amount + existing.value.intValue
}
};
} else {
span.attributes.push({
key: allocationKey,
value: {
intValue: ev.amount,
}
});
}
}
}

Expand Down