Skip to content

Commit

Permalink
[APM] Annotations API
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Apr 30, 2020
1 parent aa8cb62 commit 15301b2
Show file tree
Hide file tree
Showing 31 changed files with 1,535 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const ChartsSyncContextProvider: React.FC = ({ children }) => {
callApmApi => {
if (start && end && serviceName) {
return callApmApi({
pathname: '/api/apm/services/{serviceName}/annotations',
pathname: '/api/apm/services/{serviceName}/annotation/search',
params: {
path: {
serviceName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"include": [
"./plugins/apm/**/*",
"./plugins/observability/**/*",
"./legacy/plugins/apm/**/*",
"./typings/**/*"
],
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
],
"ui": false,
"requiredPlugins": ["apm_oss", "data", "home", "licensing"],
"optionalPlugins": ["cloud", "usageCollection", "taskManager","actions", "alerting"]
"optionalPlugins": ["cloud", "usageCollection", "taskManager","actions", "alerting", "observability"]
}
105 changes: 0 additions & 105 deletions x-pack/plugins/apm/server/lib/helpers/create_or_update_index.ts

This file was deleted.

8 changes: 5 additions & 3 deletions x-pack/plugins/apm/server/lib/helpers/es_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
/* eslint-disable no-console */
import {
IndexDocumentParams,
IndicesDeleteParams,
SearchParams,
IndicesCreateParams,
DeleteDocumentResponse
DeleteDocumentResponse,
DeleteDocumentParams
} from 'elasticsearch';
import { cloneDeep, isString, merge } from 'lodash';
import { KibanaRequest } from 'src/core/server';
Expand Down Expand Up @@ -205,7 +205,9 @@ export function getESClient(
index: <Body>(params: APMIndexDocumentParams<Body>) => {
return callEs('index', params);
},
delete: (params: IndicesDeleteParams): Promise<DeleteDocumentResponse> => {
delete: (
params: Omit<DeleteDocumentParams, 'type'>
): Promise<DeleteDocumentResponse> => {
return callEs('delete', params);
},
indicesCreate: (params: IndicesCreateParams) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { isNumber } from 'lodash';
import { Annotation, AnnotationType } from '../../../../common/annotations';
import { SetupTimeRange, Setup } from '../../helpers/setup_request';
import { ESFilter } from '../../../../typings/elasticsearch';
import { rangeFilter } from '../../helpers/range_filter';
import {
PROCESSOR_EVENT,
SERVICE_NAME,
SERVICE_ENVIRONMENT,
SERVICE_VERSION
} from '../../../../common/elasticsearch_fieldnames';

export async function getDerivedServiceAnnotations({
setup,
serviceName,
environment
}: {
serviceName: string;
environment?: string;
setup: Setup & SetupTimeRange;
}) {
const { start, end, client, indices } = setup;

const filter: ESFilter[] = [
{ term: { [PROCESSOR_EVENT]: 'transaction' } },
{ range: rangeFilter(start, end) },
{ term: { [SERVICE_NAME]: serviceName } }
];

if (environment) {
filter.push({ term: { [SERVICE_ENVIRONMENT]: environment } });
}

const versions =
(
await client.search({
index: indices['apm_oss.transactionIndices'],
body: {
size: 0,
track_total_hits: false,
query: {
bool: {
filter
}
},
aggs: {
versions: {
terms: {
field: SERVICE_VERSION
}
}
}
}
})
).aggregations?.versions.buckets.map(bucket => bucket.key) ?? [];

if (versions.length > 1) {
const annotations = await Promise.all(
versions.map(async version => {
const response = await client.search({
index: indices['apm_oss.transactionIndices'],
body: {
size: 0,
query: {
bool: {
filter: filter
.filter(esFilter => !Object.keys(esFilter).includes('range'))
.concat({
term: {
[SERVICE_VERSION]: version
}
})
}
},
aggs: {
first_seen: {
min: {
field: '@timestamp'
}
}
},
track_total_hits: false
}
});

const firstSeen = response.aggregations?.first_seen.value;

if (!isNumber(firstSeen)) {
throw new Error(
'First seen for version was unexpectedly undefined or null.'
);
}

if (firstSeen < start || firstSeen > end) {
return null;
}

return {
type: AnnotationType.VERSION,
id: version,
time: firstSeen,
text: version
};
})
);
return annotations.filter(Boolean) as Annotation[];
}
return [];
}
42 changes: 20 additions & 22 deletions x-pack/plugins/apm/server/lib/services/annotations/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { getServiceAnnotations } from '.';
import { getDerivedServiceAnnotations } from './get_derived_service_annotations';
import {
SearchParamsMock,
inspectSearchParams
Expand All @@ -24,7 +24,7 @@ describe('getServiceAnnotations', () => {
it('returns no annotations', async () => {
mock = await inspectSearchParams(
setup =>
getServiceAnnotations({
getDerivedServiceAnnotations({
setup,
serviceName: 'foo',
environment: 'bar'
Expand All @@ -34,15 +34,15 @@ describe('getServiceAnnotations', () => {
}
);

expect(mock.response).toEqual({ annotations: [] });
expect(mock.response).toEqual([]);
});
});

describe('with 1 version', () => {
it('returns no annotations', async () => {
mock = await inspectSearchParams(
setup =>
getServiceAnnotations({
getDerivedServiceAnnotations({
setup,
serviceName: 'foo',
environment: 'bar'
Expand All @@ -52,7 +52,7 @@ describe('getServiceAnnotations', () => {
}
);

expect(mock.response).toEqual({ annotations: [] });
expect(mock.response).toEqual([]);
});
});

Expand All @@ -65,7 +65,7 @@ describe('getServiceAnnotations', () => {
];
mock = await inspectSearchParams(
setup =>
getServiceAnnotations({
getDerivedServiceAnnotations({
setup,
serviceName: 'foo',
environment: 'bar'
Expand All @@ -77,22 +77,20 @@ describe('getServiceAnnotations', () => {

expect(mock.spy.mock.calls.length).toBe(3);

expect(mock.response).toEqual({
annotations: [
{
id: '8.0.0',
text: '8.0.0',
time: 1.5281138e12,
type: 'version'
},
{
id: '7.5.0',
text: '7.5.0',
time: 1.5281138e12,
type: 'version'
}
]
});
expect(mock.response).toEqual([
{
id: '8.0.0',
text: '8.0.0',
time: 1.5281138e12,
type: 'version'
},
{
id: '7.5.0',
text: '7.5.0',
time: 1.5281138e12,
type: 'version'
}
]);
});
});
});
Loading

0 comments on commit 15301b2

Please sign in to comment.