import { ApolloServerPlugin , BaseContext } from 'apollo-server-plugin-base' ; import { hostname } from 'os' ; import { collectDefaultMetrics , Registry } from 'prom-client' ; import { AppContext , Args , Context , generateContext , SkipMetricsMap as ContextSkipMetricsMap , Source } from './context' ; import { registerEndpoint } from './endpoint' ; import { filterLabels } from './helpers' ; import { generateHooks } from './hooks' ; import { generateMetrics } from './metrics' ; export type SkipMetricsMap < C extends BaseContext = BaseContext , S = any , A = { [ p ] : any ; } > = Partial < ContextSkipMetricsMap < C , S , A > > ; export type PluginOptions < C extends BaseContext = BaseContext , S = any , A = { [ p ] : any ; } > = Partial < Omit < Context , 'skipMetrics' > & { skipMetrics : SkipMetricsMap < C , S , A > ; } > ; export function toggleDefaultMetrics < C = AppContext , S = Source , A = Args > ( register , { defaultMetrics , defaultMetricsOptions } ) { if ( defaultMetrics ) { collectDefaultMetrics ( { register , ... defaultMetricsOptions } ) ; } } export function setDefaultLabels < C = AppContext , S = Source , A = Args > ( register , { defaultLabels , hostnameLabel , hostnameLabelName } ) { const labels = filterLabels ( { ... defaultLabels , [ hostnameLabelName ] : hostnameLabel ? hostname ( ) : undefined } ) ; register . setDefaultLabels ( labels ) ; } export function toggleEndpoint < C = AppContext , S = Source , A = Args > ( register , { metricsEndpoint , app , metricsEndpointPath } ) { if ( metricsEndpoint ) { registerEndpoint ( { app : app , register , path : metricsEndpointPath } ) ; } } export function createPlugin < C = AppContext , S = Source , A = Args > ( options ) { const context = generateContext < C , S , A > ( options ) ; const register = context . register ; toggleDefaultMetrics ( register , context ) ; setDefaultLabels ( register , context ) ; toggleEndpoint ( register , context ) ; const metrics = generateMetrics ( register , context ) ; return generateHooks ( metrics ) ; }