import { join } from 'path' ; import { merge , get } from 'lodash' ; import { execSync } from 'child_process' ; import { getDataPath } from '@osd/utils/target/path' ; import { readFileSync } from 'fs' ; import { ApmAgentConfig } from './types' ; const getDefaultConfig = ( isDistributable ) : ApmAgentConfig => { if ( isDistributable ) { return { active : false , globalLabels : { } , centralConfig : false , logUncaughtExceptions : true , breakdownMetrics : false , } ; } return { active : false , serverUrl : 'https://f1542b814f674090afd914960583265f.apm.us-central1.gcp.cloud.es.io:443' , secretToken : 'R0Gjg46pE9K9wGestd' , globalLabels : { } , breakdownMetrics : true , centralConfig : false , logUncaughtExceptions : true , } ; } ; export class ApmConfiguration { private baseConfig ? : any ; private opensearchDashboardsVersion : string ; private pkgBuild : Record < string , any > ; constructor ( private readonly rootDir , private readonly rawOpenSearchDashboardsConfig , private readonly isDistributable ) { const { version , build } = require ( join ( this . rootDir , 'package.json' ) ) ; this . opensearchDashboardsVersion = version ; this . pkgBuild = build ; } public getConfig ( serviceName ) { return { ... this . getBaseConfig ( ) , serviceName , } ; } private getBaseConfig ( ) { if ( ! this . baseConfig ) { const apmConfig = merge ( getDefaultConfig ( this . isDistributable ) , this . getConfigFromOpenSearchDashboardsConfig ( ) , this . getDevConfig ( ) , this . getDistConfig ( ) ) ; const rev = this . getGitRev ( ) ; if ( rev !== null ) { apmConfig . globalLabels . git_rev = rev ; } const uuid = this . getOpenSearchDashboardsUuid ( ) ; if ( uuid ) { apmConfig . globalLabels . opensearch_dashboards_uuid = uuid ; } apmConfig . serviceVersion = this . opensearchDashboardsVersion ; this . baseConfig = apmConfig ; } return this . baseConfig ; } private getConfigFromOpenSearchDashboardsConfig ( ) { return get ( this . rawOpenSearchDashboardsConfig , 'opensearch.apm' , { } ) ; } private getOpenSearchDashboardsUuid ( ) { if ( get ( this . rawOpenSearchDashboardsConfig , 'server.uuid' ) ) { return this . rawOpenSearchDashboardsConfig . server . uuid ; } const dataPath = get ( this . rawOpenSearchDashboardsConfig , 'path.data' ) || getDataPath ( ) ; try { const filename = join ( dataPath , 'uuid' ) ; return readFileSync ( filename , 'utf-8' ) ; } catch ( e ) { } } private getDevConfig ( ) { try { const apmDevConfigPath = join ( this . rootDir , 'config' , 'apm.dev.js' ) ; return require ( apmDevConfigPath ) ; } catch ( e ) { return { } ; } } /** Config keys that cannot be overridden in production builds */ private getDistConfig ( ) { if ( ! this . isDistributable ) { return { } ; } return { captureHeaders : false , captureBody : 'off' , } ; } private getGitRev ( ) { if ( this . isDistributable ) { return this . pkgBuild . sha ; } try { return execSync ( 'git rev-parse --short HEAD' , { encoding : 'utf-8' as BufferEncoding , stdio : [ 'ignore' , 'pipe' , 'ignore' ] , } ) . trim ( ) ; } catch ( e ) { return null ; } } }