Skip to content

Commit

Permalink
[APM] Add types for setup_request (#23762)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv committed Oct 18, 2018
1 parent db00699 commit 066f8d2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
px,
units
} from '../../../style/variables';
import { KeySorter } from './types';

export type KeySorter = (data: StringMap<any>, parentKey?: string) => string[];

const Table = styled.table`
font-family: ${fontFamilyCode};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import { colors, fontSize, px, unit, units } from '../../../style/variables';
import { getFeatureDocs } from '../../../utils/documentation';
// @ts-ignore
import { ExternalLink } from '../../../utils/url';
import { NestedKeyValueTable } from './NestedKeyValueTable';
import { KeySorter, NestedKeyValueTable } from './NestedKeyValueTable';
import PROPERTY_CONFIG from './propertyConfig.json';
import { KeySorter } from './types';

const indexedPropertyConfig = _.indexBy(PROPERTY_CONFIG, 'key');

Expand Down
38 changes: 0 additions & 38 deletions x-pack/plugins/apm/server/lib/helpers/setup_request.js

This file was deleted.

65 changes: 65 additions & 0 deletions x-pack/plugins/apm/server/lib/helpers/setup_request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.
*/

/* tslint:disable no-console */
import { SearchParams, SearchResponse } from 'elasticsearch';
import moment from 'moment';
import { Url } from 'url';
import { StringMap } from '../../../typings/common';

function decodeEsQuery(esQuery?: string) {
return esQuery ? JSON.parse(decodeURIComponent(esQuery)) : null;
}

// TODO: get these from hapi
interface Request {
query: StringMap;
server: any;
method: string;
url: Url;
}

interface ServerConfig {
get: (key: string) => string;
}

type Client<T> = (type: string, params: SearchParams) => SearchResponse<T>;

export interface Setup<T = any> {
start: number;
end: number;
esFilterQuery: string;
client: Client<T>;
config: ServerConfig;
}

export function setupRequest(req: Request, reply: (setup: Setup) => void) {
const cluster = req.server.plugins.elasticsearch.getCluster('data');

function client<T>(type: string, params: SearchParams): SearchResponse<T> {
if (req.query._debug) {
console.log(`DEBUG ES QUERY:`);
console.log(
`${req.method.toUpperCase()} ${req.url.pathname} ${JSON.stringify(
req.query
)}`
);
console.log(`GET ${params.index}/_search`);
console.log(JSON.stringify(params.body, null, 4));
}
return cluster.callWithRequest(req, type, params);
}

const setup = {
start: moment.utc(req.query.start).valueOf(),
end: moment.utc(req.query.end).valueOf(),
esFilterQuery: decodeEsQuery(req.query.esFilterQuery),
client,
config: req.server.config()
};

reply(setup);
}
28 changes: 11 additions & 17 deletions x-pack/plugins/apm/server/lib/traces/get_trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@

import { SearchParams, SearchResponse } from 'elasticsearch';
import { SERVICE_NAME, TRACE_ID } from '../../../common/constants';
import { TermsAggsBucket } from '../../../typings/elasticsearch';
import { Span } from '../../../typings/Span';
import { Transaction } from '../../../typings/Transaction';
import { Setup } from '../helpers/setup_request';

interface ServerConfig {
get: (key: string) => string;
export interface TraceResponse {
services: string[];
hits: Array<Transaction | Span>;
}

// TODO: move to shared
interface Setup<T = any> {
start: string;
end: string;
client: (type: string, params: object) => Promise<SearchResponse<T>>;
config: ServerConfig;
}

export async function getTrace(traceId: string, setup: Setup) {
export async function getTrace(
traceId: string,
setup: Setup
): Promise<TraceResponse> {
const { start, end, client, config } = setup;

const params: SearchParams = {
Expand Down Expand Up @@ -55,18 +53,14 @@ export async function getTrace(traceId: string, setup: Setup) {
}
};

interface ServiceBucket {
key: string;
}

const resp: SearchResponse<Span | Transaction> = await client(
'search',
params
);

return {
services: resp.aggregations.services.buckets.map(
(bucket: ServiceBucket) => bucket.key
services: (resp.aggregations.services.buckets as TermsAggsBucket[]).map(
bucket => bucket.key
),
hits: resp.hits.hits.map(hit => hit._source)
};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/apm/typings/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
* you may not use this file except in compliance with the Elastic License.
*/

export interface StringMap<T> {
export interface StringMap<T = any> {
[key: string]: T;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { StringMap } from '../../../../typings/common';

export type KeySorter = (data: StringMap<any>, parentKey?: string) => string[];
export interface TermsAggsBucket {
key: string;
doc_count: number;
}

0 comments on commit 066f8d2

Please sign in to comment.