diff --git a/x-pack/plugins/apm/server/plugin.ts b/x-pack/plugins/apm/server/plugin.ts index 2d3638272508e..638880c9f3e4a 100644 --- a/x-pack/plugins/apm/server/plugin.ts +++ b/x-pack/plugins/apm/server/plugin.ts @@ -202,6 +202,10 @@ export class APMPlugin }; }) as APMRouteHandlerResources['plugins']; + const telemetryUsageCounter = resourcePlugins.usageCollection?.setup.createUsageCounter( + 'apm' + ); + registerRoutes({ core: { setup: core, @@ -212,6 +216,7 @@ export class APMPlugin repository: getGlobalApmServerRouteRepository(), ruleDataClient, plugins: resourcePlugins, + telemetryUsageCounter, }); const boundGetApmIndices = async () => diff --git a/x-pack/plugins/apm/server/routes/register_routes/index.ts b/x-pack/plugins/apm/server/routes/register_routes/index.ts index c9df12fd58208..136f3c73d8046 100644 --- a/x-pack/plugins/apm/server/routes/register_routes/index.ts +++ b/x-pack/plugins/apm/server/routes/register_routes/index.ts @@ -18,6 +18,7 @@ import { routeValidationObject, } from '@kbn/server-route-repository'; import { mergeRt, jsonRt } from '@kbn/io-ts-utils'; +import { UsageCollectionSetup } from '../../../../../../src/plugins/usage_collection/server'; import { pickKeys } from '../../../common/utils/pick_keys'; import { APMRouteHandlerResources, InspectResponse } from '../typings'; import type { ApmPluginRequestHandlerContext } from '../typings'; @@ -40,6 +41,7 @@ export function registerRoutes({ logger, config, ruleDataClient, + telemetryUsageCounter, }: { core: APMRouteHandlerResources['core']; plugins: APMRouteHandlerResources['plugins']; @@ -47,6 +49,9 @@ export function registerRoutes({ repository: ServerRouteRepository; config: APMRouteHandlerResources['config']; ruleDataClient: APMRouteHandlerResources['ruleDataClient']; + telemetryUsageCounter?: ReturnType< + UsageCollectionSetup['createUsageCounter'] + >; }) { const routes = repository.getRoutes(); @@ -116,9 +121,22 @@ export function registerRoutes({ // cleanup inspectableEsQueriesMap.delete(request); + if (!options.disableTelemetry && telemetryUsageCounter) { + telemetryUsageCounter.incrementCounter({ + counterName: `${method.toUpperCase()} ${pathname}`, + counterType: 'success', + }); + } + return response.ok({ body }); } catch (error) { logger.error(error); + if (!options.disableTelemetry && telemetryUsageCounter) { + telemetryUsageCounter.incrementCounter({ + counterName: `${method.toUpperCase()} ${pathname}`, + counterType: 'error', + }); + } const opts = { statusCode: 500, body: { diff --git a/x-pack/plugins/apm/server/routes/typings.ts b/x-pack/plugins/apm/server/routes/typings.ts index c9f425473ada6..98c2ee47b5633 100644 --- a/x-pack/plugins/apm/server/routes/typings.ts +++ b/x-pack/plugins/apm/server/routes/typings.ts @@ -41,6 +41,7 @@ export interface APMRouteCreateOptions { | 'access:ml:canCreateJob' >; body?: { accepts: Array<'application/json' | 'multipart/form-data'> }; + disableTelemetry?: boolean; }; }