Skip to content

Commit

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

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ObsUX] [APM-OTEL] Filter by trace.id instead of transaction.if for
dependency spans
(elastic#198781)](elastic#198781)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Miriam","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-05T08:20:31Z","message":"[ObsUX]
[APM-OTEL] Filter by trace.id instead of transaction.if for dependency
spans (elastic#198781)\n\nCloses
https://github.com/elastic/kibana/issues/193672\r\n\r\n### Summary of
the issue\r\n\r\nThe trace waterfall for dependencies operations is
filled querying to\r\nspan documents using term query for
`span.destination.service.resource`\r\nand `span.name` fields, and where
`transaction.id` exist. The results\r\nfor this query were empty, in
otel, span documents don't have the\r\n`transaction.id` field. After
this query another one is made to retrieve\r\nthe transactions for those
spans, querying the transaction ids\r\n\r\n### Fix\r\n\r\nThe query has
been changed, so we will check for the `trace.id` instead\r\nof the
`transaction.id`\r\nOn the second query, we will get from those trace
ids the ones with\r\n`transaction.id` and retrieve the transactions
data\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/fba25f61-0646-4071-b49f-422eab7ff18e","sha":"c83e6db44af0eb7e00090c542263dba1ba5c5d60","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","ci:project-deploy-observability","Team:obs-ux-infra_services","v8.17.0"],"title":"[ObsUX]
[APM-OTEL] Filter by trace.id instead of transaction.if for dependency
spans","number":198781,"url":"https://github.com/elastic/kibana/pull/198781","mergeCommit":{"message":"[ObsUX]
[APM-OTEL] Filter by trace.id instead of transaction.if for dependency
spans (elastic#198781)\n\nCloses
https://github.com/elastic/kibana/issues/193672\r\n\r\n### Summary of
the issue\r\n\r\nThe trace waterfall for dependencies operations is
filled querying to\r\nspan documents using term query for
`span.destination.service.resource`\r\nand `span.name` fields, and where
`transaction.id` exist. The results\r\nfor this query were empty, in
otel, span documents don't have the\r\n`transaction.id` field. After
this query another one is made to retrieve\r\nthe transactions for those
spans, querying the transaction ids\r\n\r\n### Fix\r\n\r\nThe query has
been changed, so we will check for the `trace.id` instead\r\nof the
`transaction.id`\r\nOn the second query, we will get from those trace
ids the ones with\r\n`transaction.id` and retrieve the transactions
data\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/fba25f61-0646-4071-b49f-422eab7ff18e","sha":"c83e6db44af0eb7e00090c542263dba1ba5c5d60"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/198781","number":198781,"mergeCommit":{"message":"[ObsUX]
[APM-OTEL] Filter by trace.id instead of transaction.if for dependency
spans (elastic#198781)\n\nCloses
https://github.com/elastic/kibana/issues/193672\r\n\r\n### Summary of
the issue\r\n\r\nThe trace waterfall for dependencies operations is
filled querying to\r\nspan documents using term query for
`span.destination.service.resource`\r\nand `span.name` fields, and where
`transaction.id` exist. The results\r\nfor this query were empty, in
otel, span documents don't have the\r\n`transaction.id` field. After
this query another one is made to retrieve\r\nthe transactions for those
spans, querying the transaction ids\r\n\r\n### Fix\r\n\r\nThe query has
been changed, so we will check for the `trace.id` instead\r\nof the
`transaction.id`\r\nOn the second query, we will get from those trace
ids the ones with\r\n`transaction.id` and retrieve the transactions
data\r\n\r\n\r\n\r\nhttps://github.com/user-attachments/assets/fba25f61-0646-4071-b49f-422eab7ff18e","sha":"c83e6db44af0eb7e00090c542263dba1ba5c5d60"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Miriam <[email protected]>
  • Loading branch information
kibanamachine and MiriamAparicio authored Nov 5, 2024
1 parent 97f60e3 commit 336ad62
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 336ad62

Please sign in to comment.