protected static void registerGraphiteMetricObserver ( List < MetricObserver > observers , String prefix , String host , String port ) { // verify at least hostname is set, else cannot configure this observer if ( null == host || host . trim ( ) . isEmpty ( ) ) { LOGGER . info ( "GraphiteMetricObserver not configured, missing environment variable: {}" , ENV_GRAPHITE_HOSTNAME ) ; return ; } LOGGER . debug ( "{} environment variable is: {}" , ENV_GRAPHITE_PREFIX , prefix ) ; LOGGER . debug ( "{} environment variable is: {}" , ENV_GRAPHITE_HOSTNAME , host ) ; LOGGER . debug ( "{} environment variable is: {}" , ENV_GRAPHITE_PORT , port ) ; if ( prefix == null ) { if ( System . getenv ( "OPENSHIFT_APP_NAME" ) != null ) { // try to get name from openshift. assume it's scaleable app. // format: <app name>. <namespace>.<gear dns> prefix = String . format ( "%s.%s.%s" , System . getenv ( "OPENSHIFT_APP_NAME" ) , System . getenv ( "OPENSHIFT_NAMESPACE" ) , System . getenv ( "OPENSHIFT_GEAR_DNS" ) ) ; } else { //default prefix = System . getenv ( "HOSTNAME" ) ; LOGGER . debug ( "using HOSTNAME as default prefix" + prefix ) ; } } int iport = - 1 ; if ( port != null && ! port . isEmpty ( ) ) { try { iport = Integer . valueOf ( port ) ; } catch ( NumberFormatException e ) { iport = - 1 ; LOGGER . warn ( "Configured port is not an integer. Falling back to default" ) ; } } if ( iport < 0 ) { iport = 2004 ; //default graphite port LOGGER . debug ( "Using default port: " + iport ) ; } String addr = host + ":" + iport ; LOGGER . debug ( "GraphiteMetricObserver prefix: " + prefix ) ; LOGGER . debug ( "GraphiteMetricObserver address: " + addr ) ; observers . add ( new GraphiteMetricObserver ( prefix , addr ) ) ; }
If there is sufficient configuration register a Graphite observer to publish metrics . Requires at a minimum a host . Optionally can set prefix as well as port . The prefix defaults to the host and port defaults to 2004 .