import { produce } from 'immer' ; import { apmSchema } from '../server/lib/apm_telemetry/schema' ; function schemaToMapping ( schemaLeaf ) { if ( schemaLeaf . type === 'array' ) { return schemaToMapping ( schemaLeaf . items ) ; } if ( typeof schemaLeaf . type === 'string' ) { return schemaLeaf ; } return Object . entries < any > ( schemaLeaf ) . reduce ( ( acc , [ key , value ] ) => { const propMapping = schemaToMapping ( value ) ; return { ... acc , [ key ] : typeof propMapping . type === 'string' ? propMapping : { properties : propMapping } , } ; } , { } ) ; } /**
 * Generate an object containing the mapping used for APM telemetry based on the schema specified
 * in the usage collector. Can be used with the `upload-telemetry-data` script or to update the
 * mapping in the telemetry repository.
 */ export function getApmTelemetryMapping ( ) { return { properties : schemaToMapping ( apmSchema ) } ; } /**
 * Merge a telemetry mapping object (from https://github.com/elastic/telemetry/blob/master/config/templates/xpack-phone-home.json)
 * with the output from `getApmTelemetryMapping`.
 */ export function mergeApmTelemetryMapping ( xpackPhoneHomeMapping ) { return produce ( xpackPhoneHomeMapping , ( draft ) => { draft . mappings . properties . stack_stats . properties . kibana . properties . plugins . properties . apm = getApmTelemetryMapping ( ) ; return draft ; } ) ; }