Skip to content

Commit

Permalink
ecs k8s service fix
Browse files Browse the repository at this point in the history
  • Loading branch information
crespocarlos committed Nov 22, 2024
1 parent 4c628dd commit 7900589
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/kbn-apm-synthtrace-client/src/lib/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { k8sNodeEntity } from './kubernetes/node_entity';
import { k8sPodEntity } from './kubernetes/pod_entity';
import { k8sReplicaSetEntity } from './kubernetes/replica_set';
import { k8sStatefulSetEntity } from './kubernetes/stateful_set';
import { k8sServiceEntity } from './kubernetes/service';
import { k8sContainerEntity } from './kubernetes/container_entity';

export type EntityDataStreamType = 'metrics' | 'logs' | 'traces';
Expand Down Expand Up @@ -57,6 +58,7 @@ export const entities = {
k8sPodEntity,
k8sReplicaSetEntity,
k8sStatefulSetEntity,
k8sServiceEntity,
k8sContainerEntity,
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const identityFieldsMap: Record<Schema, Record<string, string[]>> = {
node: ['kubernetes.node.name'],
replicaset: ['kubernetes.replicaset.name'],
statefulset: ['kubernetes.statefulset.name'],
service: ['kubernetes.service.name'],
container: ['kubernetes.container.id'],
},
semconv: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { Schema } from '..';
import { K8sEntity } from '.';

export function k8sServiceEntity({
schema,
name,
uid,
clusterName,
entityId,
...others
}: {
schema: Schema;
name: string;
uid?: string;
clusterName?: string;
entityId: string;
[key: string]: any;
}) {
if (schema !== 'ecs') {
throw new Error('Schema not supported for service entity: ' + schema);
}
return new K8sEntity(schema, {
'entity.definition_id': 'service',
'entity.type': 'service',
'kubernetes.service.name': name,
'kubernetes.namespace': clusterName,
'entity.id': entityId,
...others,
});
}
19 changes: 18 additions & 1 deletion packages/kbn-apm-synthtrace/src/scenarios/k8s_entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const CRON_JOB_ENTITY_ID = generateShortId();
const CRON_JOB_UID = generateShortId();
const NODE_ENTITY_ID = generateShortId();
const NODE_UID = generateShortId();
const SERVICE_UID = generateShortId();

const scenario: Scenario<Partial<EntityFields>> = async (runOptions) => {
const { logger } = runOptions;
Expand All @@ -45,7 +46,7 @@ const scenario: Scenario<Partial<EntityFields>> = async (runOptions) => {
.interval('1m')
.rate(1)
.generator((timestamp) => {
return [
const commonEntities = [
entities.k8s
.k8sClusterJobEntity({
schema,
Expand Down Expand Up @@ -133,6 +134,22 @@ const scenario: Scenario<Partial<EntityFields>> = async (runOptions) => {
})
.timestamp(timestamp),
];

if (schema === 'ecs') {
return [
...commonEntities,
entities.k8s
.k8sServiceEntity({
schema,
clusterName: CLUSTER_NAME,
name: 'my_service',
entityId: SERVICE_UID,
})
.timestamp(timestamp),
];
}

return commonEntities;
});

const ecsEntities = getK8sEntitiesEvents('ecs');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* 2.0.
*/

export const commonEcsIndexPatterns = ['metrics-kubernetes*', 'metrics-apm*', 'logs-*'];
export const commonEcsIndexPatterns = ['metrics-kubernetes*', 'logs-*'];
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const KUBERNETES_DASHBOARDS_IDS: Record<string, string> = {
[BUILT_IN_ENTITY_TYPES.KUBERNETES.JOB.ecs]: 'kubernetes-9bf990a0-bcb1-11ec-b64f-7dd6e8e82013',
[BUILT_IN_ENTITY_TYPES.KUBERNETES.NODE.ecs]: 'kubernetes-b945b7b0-bcb1-11ec-b64f-7dd6e8e82013',
[BUILT_IN_ENTITY_TYPES.KUBERNETES.POD.ecs]: 'kubernetes-3d4d9290-bcb1-11ec-b64f-7dd6e8e82013',
[BUILT_IN_ENTITY_TYPES.KUBERNETES.SERVICE.ecs]: 'kubernetes-ff1b3850-bcb1-11ec-b64f-7dd6e8e82013',
[BUILT_IN_ENTITY_TYPES.KUBERNETES.STATEFULSET.ecs]:
'kubernetes-21694370-bcb2-11ec-b64f-7dd6e8e82013',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const BUILT_IN_ENTITY_TYPES = {
NAMESPACE: createKubernetesEntity('namespace'),
NODE: createKubernetesEntity('node'),
POD: createKubernetesEntity('pod'),
SERVICE: createKubernetesEntity('service'),
STATEFULSET: createKubernetesEntity('statefulset'),
},
} as const;

0 comments on commit 7900589

Please sign in to comment.