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

[APM] Fix query to mobile transactions main statistics #155895

Merged
merged 4 commits into from
Apr 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ type MobileDetailedStatisticsByField =
APIReturnType<'GET /internal/apm/mobile-services/{serviceName}/detailed_statistics'>;

export function getColumns({
agentName,
detailedStatisticsLoading,
detailedStatistics,
comparisonEnabled,
offset,
}: {
agentName?: string;
detailedStatisticsLoading: boolean;
detailedStatistics: MobileDetailedStatisticsByField;
comparisonEnabled?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ export interface MobileMainStatisticsResponse {
name: string | number;
latency: number | null;
throughput: number;
crashRate?: number;
crashRate: number;
kpatticha marked this conversation as resolved.
Show resolved Hide resolved
}>;
}

type MergedQueriesResponse = Array<{
name: string | number;
latency: number | null;
throughput: number;
sessions: number;
crashes?: number;
}>;

export async function getMobileMainStatisticsByField({
kuery,
apmEventClient,
Expand All @@ -55,7 +63,7 @@ export async function getMobileMainStatisticsByField({
}: Props) {
async function getMobileTransactionEventStatistics() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getMobileMainStatisticsByField(){
   async function getMobileTransactionEventStatistics() {}
   async function getMobileErrorEventStatistics(){}
}

Nesting functions can be useful for encapsulating functionality and scoping variables but in this case, it seems it doesn't provide any value.

On the other hand, if there is a possibility of reusing ex getMobileErrorEventStatistics in other parts of the codebase, defining it outside of getMobileMainStatisticsByField could be a more practical approach.

What do you think about defining them outside of getMobileMainStatisticsByField?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but getMobileErrorEventStatistics is getting crash errors by field, it's not generic enough to be reusable. But I will keep it in mind for when we start implementing more use cases for crashRate

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but getMobileErrorEventStatistics is getting crash errors by field, it's not generic enough to be reusable.

tbh, I'm not sure what you meant with it 🤔 but I think something like the following will work

const commonPros = {
    kuery,
    apmEventClient,
    serviceName,
    environment,
    start,
    end,
    field,
  };
  const [transactioEventStatistics, errorEventStatistics] = await Promise.all([
    getMobileTransactionEventStatistics(commonPros),
    getMobileErrorEventStatistics(commonPros),
  ]);

const response = await apmEventClient.search(
`get_mobile_main_statistics_by_field`,
`get_mobile_transaction_events_main_statistics_by_field`,
{
apm: {
sources: [
Expand Down Expand Up @@ -90,6 +98,11 @@ export async function getMobileMainStatisticsByField({
field: TRANSACTION_DURATION,
},
},
sessions: {
cardinality: {
field: SESSION_ID,
},
},
},
},
},
Expand All @@ -110,66 +123,59 @@ export async function getMobileMainStatisticsByField({
end,
value: bucket.doc_count,
}),
sessions: bucket.sessions.value,
};
}) ?? []
);
}

async function getMobileErrorEventStatistics() {
const response = await apmEventClient.search(
`get_mobile_transaction_events_main_statistics_by_field`,
{
apm: {
sources: [
{
documentType: ApmDocumentType.ErrorEvent,
rollupInterval: RollupInterval.None,
},
],
const response = await apmEventClient.search(`get_mobile_crashes`, {
apm: {
sources: [
{
documentType: ApmDocumentType.ErrorEvent,
rollupInterval: RollupInterval.None,
},
],
},
body: {
track_total_hits: false,
size: 0,
query: {
bool: {
filter: [
...termQuery(SERVICE_NAME, serviceName),
...rangeQuery(start, end),
...environmentQuery(environment),
...kqlQuery(kuery),
],
},
},
body: {
track_total_hits: false,
size: 0,
query: {
bool: {
filter: [
...termQuery(SERVICE_NAME, serviceName),
...rangeQuery(start, end),
...environmentQuery(environment),
...kqlQuery(kuery),
],
aggs: {
main_statistics: {
terms: {
field,
size: 1000,
},
},
aggs: {
main_statistics: {
terms: {
field,
size: 1000,
},
aggs: {
sessions: {
cardinality: {
field: SESSION_ID,
},
},
crashes: {
filter: {
term: {
[ERROR_TYPE]: 'crash',
},
aggs: {
crashes: {
filter: {
term: {
[ERROR_TYPE]: 'crash',
},
},
},
},
},
},
}
);
},
});
return (
response.aggregations?.main_statistics.buckets.map((bucket) => {
return {
name: bucket.key,
crashRate: bucket.crashes.doc_count / bucket.sessions.value ?? 0,
crashes: bucket.crashes.doc_count ?? 0,
};
}) ?? []
);
Expand All @@ -180,7 +186,19 @@ export async function getMobileMainStatisticsByField({
getMobileErrorEventStatistics(),
]);

const mainStatistics = merge(transactioEventStatistics, errorEventStatistics);
const mainStatisticsMerged: MergedQueriesResponse = merge(
transactioEventStatistics,
errorEventStatistics
);

const mainStatistics = mainStatisticsMerged.map((item) => {
return {
name: item.name,
latency: item.latency,
throughput: item.throughput,
crashRate: item.crashes ? item.crashes / item.sessions : 0,
};
});

return { mainStatistics };
}