Skip to content

Commit

Permalink
[ObsUX] [APM-OTEL] Filter by trace.id instead of transaction.if for d…
Browse files Browse the repository at this point in the history
…ependency spans (elastic#198781)

Closes elastic#193672

### Summary of the issue

The trace waterfall for dependencies operations is filled querying to
span documents using term query for `span.destination.service.resource`
and `span.name` fields, and where `transaction.id` exist. The results
for this query were empty, in otel, span documents don't have the
`transaction.id` field. After this query another one is made to retrieve
the transactions for those spans, querying the transaction ids

### Fix

The query has been changed, so we will check for the `trace.id` instead
of the `transaction.id`
On the second query, we will get from those trace ids the ones with
`transaction.id` and retrieve the transactions data



https://github.com/user-attachments/assets/fba25f61-0646-4071-b49f-422eab7ff18e
  • Loading branch information
MiriamAparicio authored Nov 5, 2024
1 parent 69c1e5a commit c83e6db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function maybeRedirectToAvailableSpanSample({
page: number;
replace: typeof urlHelpersReplace;
history: History;
samples: Array<{ spanId: string; traceId: string; transactionId: string }>;
samples: Array<{ spanId: string; traceId: string; transactionId?: string }>;
}) {
if (spanFetchStatus !== FETCH_STATUS.SUCCESS) {
// we're still loading, don't do anything
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface DependencySpan {
serviceName: string;
agentName: AgentName;
traceId: string;
transactionId: string;
transactionId?: string;
transactionType?: string;
transactionName?: string;
duration: number;
Expand Down Expand Up @@ -72,7 +72,6 @@ export async function getTopDependencySpans({
const topDedsRequiredFields = asMutableArray([
SPAN_ID,
TRACE_ID,
TRANSACTION_ID,
SPAN_NAME,
SERVICE_NAME,
SERVICE_ENVIRONMENT,
Expand All @@ -98,7 +97,6 @@ export async function getTopDependencySpans({
...kqlQuery(kuery),
...termQuery(SPAN_DESTINATION_SERVICE_RESOURCE, dependencyName),
...termQuery(SPAN_NAME, spanName),
{ exists: { field: TRANSACTION_ID } },
...((sampleRangeFrom ?? 0) >= 0 && (sampleRangeTo ?? 0) > 0
? [
{
Expand All @@ -119,9 +117,10 @@ export async function getTopDependencySpans({
})
).hits.hits.map((hit) => unflattenKnownApmEventFields(hit.fields, topDedsRequiredFields));

const transactionIds = spans.map((span) => span.transaction.id);
const traceIds = spans.map((span) => span.trace.id);

const txRequiredFields = asMutableArray([
TRACE_ID,
TRANSACTION_ID,
TRANSACTION_TYPE,
TRANSACTION_NAME,
Expand All @@ -134,10 +133,10 @@ export async function getTopDependencySpans({
},
body: {
track_total_hits: false,
size: transactionIds.length,
size: traceIds.length,
query: {
bool: {
filter: [...termsQuery(TRANSACTION_ID, ...transactionIds)],
filter: [...termsQuery(TRACE_ID, ...traceIds), { exists: { field: TRANSACTION_ID } }],
},
},
fields: txRequiredFields,
Expand All @@ -148,10 +147,10 @@ export async function getTopDependencySpans({
})
).hits.hits.map((hit) => unflattenKnownApmEventFields(hit.fields, txRequiredFields));

const transactionsById = keyBy(transactions, (transaction) => transaction.transaction.id);
const transactionsByTraceId = keyBy(transactions, (transaction) => transaction.trace.id);

return spans.map((span): DependencySpan => {
const transaction = maybe(transactionsById[span.transaction!.id]);
const transaction = maybe(transactionsByTraceId[span.trace!.id]);

return {
'@timestamp': new Date(span['@timestamp']).getTime(),
Expand All @@ -162,7 +161,7 @@ export async function getTopDependencySpans({
duration: span.span.duration.us,
traceId: span.trace.id,
outcome: (span.event?.outcome || EventOutcome.unknown) as EventOutcome,
transactionId: span.transaction!.id,
transactionId: transaction?.transaction.id,
transactionType: transaction?.transaction.type,
transactionName: transaction?.transaction.name,
};
Expand Down

0 comments on commit c83e6db

Please sign in to comment.