Skip to content

Commit

Permalink
'transaction' mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszmarcinkowski committed Sep 20, 2024
1 parent 81596df commit a85fbf0
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 17 deletions.
5 changes: 5 additions & 0 deletions packages/kbn-apm-types/src/es_fields/apm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export const TRANSACTION_OVERFLOW_COUNT = 'transaction.aggregation.overflow_coun
// for transaction metrics
export const TRANSACTION_ROOT = 'transaction.root';
export const TRANSACTION_PROFILER_STACK_TRACE_IDS = 'transaction.profiler_stack_trace_ids';
export const TRANSACTION_NAME_TEXT = 'transaction.name.text';
export const TRANSACTION_REPRESENTATIVE_COUNT = 'transaction.representative_count';

export const EVENT_OUTCOME = 'event.outcome';

Expand Down Expand Up @@ -143,6 +145,7 @@ export const LABEL_GC = 'labels.gc';
export const LABEL_TYPE = 'labels.type';
export const LABEL_TELEMETRY_AUTO_VERSION = 'labels.telemetry_auto_version';
export const LABEL_LIFECYCLE_STATE = 'labels.lifecycle_state';
export const LABEL_SOME_RESOURCE_ATTRIBUTE = 'labels.some_resource_attribute';

export const HOST = 'host';
export const HOST_HOSTNAME = 'host.hostname'; // Do not use. Please use `HOST_NAME` instead.
Expand Down Expand Up @@ -185,6 +188,8 @@ export const VALUE_OTEL_JVM_PROCESS_MEMORY_NON_HEAP = 'non_heap';
export const TIER = '_tier';
export const INDEX = '_index';
export const DATA_STEAM_TYPE = 'data_stream.type';
export const DATA_STREAM_NAMESPACE = 'data_stream.namespace';
export const DATA_STREAM_DATASET = 'data_stream.dataset';

// Mobile
export const NETWORK_CONNECTION_TYPE = 'network.connection.type';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

import { rangeQuery, termQuery } from '@kbn/observability-plugin/server';
import type { Transaction } from '@kbn/apm-types/es_schemas_ui';
import { normalizeFields } from '../../../utils/normalize_fields';
import { transactionMapping } from '../../../utils/es_fields_mappings';
import { TRACE_ID, TRANSACTION_ID } from '../../../../common/es_fields/apm';
import { asMutableArray } from '../../../../common/utils/as_mutable_array';
import { APMEventClient } from '../../../lib/helpers/create_es_client/create_apm_event_client';
Expand Down Expand Up @@ -53,8 +52,5 @@ export async function getTransaction({
},
});

const fields = resp.hits.hits[0]?.fields;
const fieldsNorm = normalizeFields(fields) as unknown as Transaction;

return fieldsNorm;
return transactionMapping(resp.hits.hits[0]?.fields);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
*/

import { rangeQuery } from '@kbn/observability-plugin/server';
import type { Transaction } from '../../../../typings/es_schemas/ui/transaction';
import { normalizeFields } from '../../../utils/normalize_fields';
import { ApmDocumentType } from '../../../../common/document_type';
import { SERVICE_NAME, TRANSACTION_NAME } from '../../../../common/es_fields/apm';
import { RollupInterval } from '../../../../common/rollup';
import { asMutableArray } from '../../../../common/utils/as_mutable_array';
import { APMEventClient } from '../../../lib/helpers/create_es_client/create_apm_event_client';
import { transactionMapping } from '../../../utils/es_fields_mappings';

export async function getTransactionByName({
transactionName,
Expand Down Expand Up @@ -53,8 +52,5 @@ export async function getTransactionByName({
},
});

const fields = resp.hits.hits[0]?.fields;
const fieldsNorm = normalizeFields(fields) as unknown as Transaction;

return fieldsNorm;
return transactionMapping(resp.hits.hits[0]?.fields);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import { ProcessorEvent } from '@kbn/observability-plugin/common';
import { rangeQuery } from '@kbn/observability-plugin/server';
import type { Transaction } from '@kbn/apm-types/es_schemas_ui';
import { normalizeFields } from '../../../utils/normalize_fields';
import { TRACE_ID, PARENT_ID } from '../../../../common/es_fields/apm';
import { APMEventClient } from '../../../lib/helpers/create_es_client/create_apm_event_client';
import { transactionMapping } from '../../../utils/es_fields_mappings';

export async function getRootTransactionByTraceId({
traceId,
Expand Down Expand Up @@ -52,8 +51,6 @@ export async function getRootTransactionByTraceId({
};

const resp = await apmEventClient.search('get_root_transaction_by_trace_id', params);
const fields = resp.hits.hits[0]?.fields;
const transaction = normalizeFields(fields) as unknown as Transaction;

return { transaction };
return transactionMapping(resp.hits.hits[0]?.fields);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ import {
AGENT_NAME,
AGENT_VERSION,
AT_TIMESTAMP,
DATA_STEAM_TYPE,
DATA_STREAM_DATASET,
DATA_STREAM_NAMESPACE,
EVENT_OUTCOME,
EVENT_SUCCESS_COUNT,
LABEL_SOME_RESOURCE_ATTRIBUTE,
OBSERVER_HOSTNAME,
OBSERVER_TYPE,
OBSERVER_VERSION,
OBSERVER_VERSION_MAJOR,
PARENT_ID,
PROCESOR_NAME,
PROCESSOR_EVENT,
SERVICE_ENVIRONMENT,
SERVICE_FRAMEWORK_NAME,
SERVICE_NAME,
SERVICE_NODE_NAME,
SPAN_DESTINATION_SERVICE_RESOURCE,
SPAN_DURATION,
SPAN_ID,
Expand All @@ -33,6 +41,9 @@ import {
TRANSACTION_DURATION,
TRANSACTION_ID,
TRANSACTION_NAME,
TRANSACTION_REPRESENTATIVE_COUNT,
TRANSACTION_RESULT,
TRANSACTION_SAMPLED,
TRANSACTION_TYPE,
} from '@kbn/apm-types/src/es_fields/apm';
import { EventOutcome } from '../../common/event_outcome';
Expand Down Expand Up @@ -193,6 +204,68 @@ export const linkedParentsOfSpanMapping = (fields: Partial<Record<string, unknow
};
};

export const transactionMapping = (fields: Partial<Record<string, unknown[]>>) => {
return {
transaction: {
representative_count: normalizeValue<number>(fields[TRANSACTION_REPRESENTATIVE_COUNT]),
result: normalizeValue<string>(fields[TRANSACTION_RESULT]),
sampled: normalizeValue<boolean>(fields[TRANSACTION_SAMPLED]),
id: normalizeValue<string>(fields[TRANSACTION_ID]),
duration: {
us: normalizeValue<number>(fields[TRANSACTION_DURATION]),
},
type: normalizeValue<string>(fields[TRANSACTION_TYPE]),
name: normalizeValue<string>(fields[TRANSACTION_NAME]),
},
service: {
node: {
name: normalizeValue<string>(fields[SERVICE_NODE_NAME]),
},
language: {
name: normalizeValue<string>(fields[SERVICE_NODE_NAME]),
},
name: normalizeValue<string>(fields[SERVICE_NAME]),
framework: {
name: normalizeValue<string>(fields[SERVICE_FRAMEWORK_NAME]),
},
},
trace: {
id: normalizeValue<string>(fields[TRACE_ID]),
},
agent: {
name: normalizeValue<AgentName>(fields[AGENT_NAME]),
version: normalizeValue<string>(fields[AGENT_VERSION]),
},
event: {
success_count: normalizeValue<boolean>(fields[EVENT_SUCCESS_COUNT]),
outcome: normalizeValue<EventOutcome>(fields[EVENT_OUTCOME]),
},
processor: {
event: normalizeValue<'transaction'>(fields[PROCESSOR_EVENT]),
},
data_stream: {
namespace: normalizeValue<string>(fields[DATA_STREAM_NAMESPACE]),
type: normalizeValue<string>(fields[DATA_STEAM_TYPE]),
dataset: normalizeValue<string>(fields[DATA_STREAM_DATASET]),
},
span: {
id: normalizeValue<string>(fields[SPAN_ID]),
},
observer: {
hostname: normalizeValue<string>(fields[OBSERVER_HOSTNAME]),
type: normalizeValue<string>(fields[OBSERVER_TYPE]),
version: normalizeValue<string>(fields[OBSERVER_VERSION]),
},
timestamp: {
us: normalizeValue<number>(fields[TIMESTAMP]),
},
'@timestamp': normalizeValue<string>(fields[AT_TIMESTAMP]),
labels: {
some_resource_attribute: normalizeValue<string>(fields[LABEL_SOME_RESOURCE_ATTRIBUTE]),
},
};
};

const normalizeValue = <T>(field: unknown[] | unknown): T => {
return (Array.isArray(field) && field.length > 0 ? field[0] : field) as T;
};

0 comments on commit a85fbf0

Please sign in to comment.