下面列出了怎么用com.codahale.metrics.jmx.JmxReporter的API类实例代码及写法,或者点击链接到github查看源代码。
@Provides(type = Type.SET)
ContextConfigurator provideJMX(final MetricRegistry metrics) {
return new ContextConfigurator() {
private JmxReporter reporter;
@Override
public void init(ServletContextHandler context) {
context.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", metrics);
ServletHolder servlet = new ServletHolder(new JMXJsonServlet());
context.addServlet(servlet, "/rest/v1/system/jmx");
}
@Override
public void start() {
reporter = JmxReporter.forRegistry(metrics).build();
reporter.start();
}
@Override
public void stop() {
if(reporter != null) {
reporter.stop();
reporter.close();
}
}
};
}
public static void setupJMXReporter(Bus b, MetricRegistry reg) {
InstrumentationManager im = b.getExtension(InstrumentationManager.class);
if (im != null) {
JmxReporter reporter = JmxReporter.forRegistry(reg).registerWith(im.getMBeanServer())
.inDomain("org.apache.cxf")
.createsObjectNamesWith(new ObjectNameFactory() {
public ObjectName createName(String type, String domain, String name) {
try {
return new ObjectName(name);
} catch (MalformedObjectNameException e) {
throw new RuntimeException(e);
}
}
})
.build();
reporter.start();
}
}
public static void main(String args[]) throws Exception {
VerifiableProperties verifiableProperties = ToolUtils.getVerifiableProperties(args);
MetricRegistry registry = new MetricRegistry();
StoreToolsMetrics metrics = new StoreToolsMetrics(registry);
JmxReporter reporter = null;
try {
reporter = JmxReporter.forRegistry(registry).build();
reporter.start();
DumpDataTool dumpDataTool = new DumpDataTool(verifiableProperties, metrics);
dumpDataTool.doOperation();
} finally {
if (reporter != null) {
reporter.stop();
}
}
}
public static void main(String args[]) throws Exception {
VerifiableProperties verifiableProperties = ToolUtils.getVerifiableProperties(args);
MetricRegistry registry = new MetricRegistry();
StoreToolsMetrics metrics = new StoreToolsMetrics(registry);
JmxReporter reporter = null;
try {
reporter = JmxReporter.forRegistry(registry).build();
reporter.start();
DumpLogTool dumpLogTool = new DumpLogTool(verifiableProperties, metrics);
dumpLogTool.doOperation();
} finally {
if (reporter != null) {
reporter.stop();
}
}
}
private void configureMetricsActivation() {
if (env.containsProperty("Metrics")) {
metricsActivatedByConfiguration = Boolean.TRUE;
// Start a Metrics JMX reporter
reporter = JmxReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start();
} else {
metricsActivatedByConfiguration = Boolean.FALSE;
}
}
private static JmxReporter getJmxReporter() {
if (METRICS_JMX_OUTPUT_ENABLED) {
JmxReporter reporter = JmxReporter.forRegistry(REGISTRY).build();
reporter.start();
return reporter;
}
return null;
}
@PostConstruct
public void postConstruct() {
if (!constructed.compareAndSet(false, true)) {
return;
}
if (!InternalConfigurations.JMX_REPORTER_ENABLED) {
return;
}
jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
jmxReporter.start();
log.debug("Started JMX Metrics Reporting.");
}
@Test
public void test_post_construct_twice() {
final JmxReporterBootstrap jmxReporterBootstrap = new JmxReporterBootstrap(new MetricRegistry());
jmxReporterBootstrap.postConstruct();
final JmxReporter firstReporter = jmxReporterBootstrap.jmxReporter;
jmxReporterBootstrap.postConstruct();
final JmxReporter secondReporter = jmxReporterBootstrap.jmxReporter;
assertSame(firstReporter, secondReporter);
}
private void configureMetricsActivation() {
if (configurationService.containsKey("Metrics")) {
metricsActivatedByConfiguration = Boolean.TRUE;
// Start a Metrics JMX reporter
reporter = JmxReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start();
} else {
metricsActivatedByConfiguration = Boolean.FALSE;
}
}
public InjectionModule(ServiceProperties serviceProperties) {
this.serviceProperties = serviceProperties;
metricRegistry = new MetricRegistry();
JmxReporter reporter = JmxReporter.forRegistry(metricRegistry).build();
reporter.start();
httpClient = createHttpClient();
}
@Override
protected JmxReporter getReporter(Map<String, Object> configuration, MetricRegistry registry) {
JmxReporter reporter = JmxReporter.forRegistry(registry).build();
reporter.start();
return reporter;
}
@Override
public synchronized Reporter getReporter(MetricRegistry registry) {
if (null == reporter) {
reporter = JmxReporter.forRegistry(registry).build();
}
return reporter;
}
protected JmxReporterServer(MetricRegistry registry, String host, int port,
MBeanServer mBeanServer) {
String serviceUrl =
"service:jmx:rmi://localhost:" + port + "/jndi/rmi://" + host + ":" + port + "/jmxrmi";
try {
JMXServiceURL url = new JMXServiceURL(serviceUrl);
connector = JMXConnectorServerFactory
.newJMXConnectorServer(url, null, mBeanServer);
rmiRegistry = LocateRegistry.createRegistry(port);
reporter = JmxReporter.forRegistry(registry).registerWith(mBeanServer).build();
} catch (Exception e) {
throw new HoodieException("Jmx service url created " + serviceUrl, e);
}
}
/**
* Creates a {@link JmxReporter} that generates metrics whose names
* are decided according to the {@link BrooklinObjectNameFactory}.
*/
public static JmxReporter createJmxReporter(MetricRegistry metricRegistry) {
Validate.notNull(metricRegistry);
return JmxReporter.forRegistry(metricRegistry)
.createsObjectNamesWith(OBJECT_NAME_FACTORY)
.build();
}
public ApplicationState(ApplicationConfig config, XioTracing tracing) {
this.config = config;
this.tracing = tracing;
this.metricRegistry = new MetricRegistry();
JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
jmxReporter.start();
this.channelConfiguration = config.serverChannelConfig();
this.ipFilterConfig = new AtomicReference<>(new IpFilterConfig());
this.http1FilterConfig = new AtomicReference<>(new Http1FilterConfig());
}
@Override
public void start(MetricsContext metricsContext) throws MetricsReporterException {
MetricRegistry registry = (MetricRegistry) metricsContext.getProperty(
DefaultMetricsService.METRICS_REGISTRY);
jmxReporter = JmxReporter.forRegistry(registry).build();
jmxReporter.start();
}
/**
* The JMX Reporter is activated only if the jmxEnabled property is set. If the jmxAutoStart property is enabled,
* the JMX Reporter will start automatically.
*
* @param configurationProperties configuration properties
* @param metricRegistry metric registry
* @return {@link JmxMetricReporter}
*/
@Override
public JmxMetricReporter init(ConfigurationProperties configurationProperties, MetricRegistry metricRegistry) {
if (configurationProperties.isJmxEnabled()) {
jmxReporter = JmxReporter
.forRegistry(metricRegistry)
.inDomain(getClass().getName() + "." + configurationProperties.getUniqueName())
.build();
}
if(configurationProperties.isJmxAutoStart()) {
start();
}
return this;
}
private void start(String[] args) throws Exception {
this.initialize();
applicationContext = SpringApplication.run(SpringConfig.class, args);
ApplicationHolder.applicationContext = applicationContext;
int port = Integer.parseInt(Configuration.getProperty(Dict.PROPERTY_SERVER_PORT));
//TODO: Server custom configuration
int processors = Runtime.getRuntime().availableProcessors();
Integer corePoolSize = Configuration.getPropertyInt("serving.core.pool.size", processors);
Integer maxPoolSize = Configuration.getPropertyInt("serving.max.pool.size", processors * 2);
Integer aliveTime = Configuration.getPropertyInt("serving.pool.alive.time", 1000);
Integer queueSize = Configuration.getPropertyInt("serving.pool.queue.size", 10);
Executor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, aliveTime.longValue(), TimeUnit.MILLISECONDS,
new SynchronousQueue(), new NamedThreadFactory("ServingServer", true));
FateServerBuilder serverBuilder = (FateServerBuilder) ServerBuilder.forPort(port);
serverBuilder.keepAliveTime(100,TimeUnit.MILLISECONDS);
serverBuilder.executor(executor);
//new ServiceOverloadProtectionHandle()
serverBuilder.addService(ServerInterceptors.intercept(applicationContext.getBean(InferenceService.class), new ServiceExceptionHandler(), new ServiceOverloadProtectionHandle()), InferenceService.class);
serverBuilder.addService(ServerInterceptors.intercept(applicationContext.getBean(ModelService.class), new ServiceExceptionHandler(), new ServiceOverloadProtectionHandle()), ModelService.class);
serverBuilder.addService(ServerInterceptors.intercept(applicationContext.getBean(ProxyService.class), new ServiceExceptionHandler(), new ServiceOverloadProtectionHandle()), ProxyService.class);
server = serverBuilder.build();
logger.info("server started listening on port: {}, use configuration: {}", port, this.confPath);
server.start();
String userRegisterString = Configuration.getProperty(Dict.USE_REGISTER,"true");
useRegister = Boolean.valueOf(userRegisterString);
if(useRegister) {
logger.info("serving-server is using register center");
}
else{
logger.warn("serving-server not use register center");
}
if (useRegister) {
ZookeeperRegistry zookeeperRegistry = applicationContext.getBean(ZookeeperRegistry.class);
zookeeperRegistry.subProject(Dict.PROPERTY_PROXY_ADDRESS);
zookeeperRegistry.subProject(Dict.PROPERTY_FLOW_ADDRESS);
BaseModel.routerService = applicationContext.getBean(RouterService.class);
FateServer.serviceSets.forEach(servie -> {
try {
String serviceName = servie.serviceName();
String weightKey = serviceName + ".weight";
HashMap properties = Configuration.getProperties();
if (properties.get(weightKey) != null) {
int weight = Integer.valueOf(properties.get(weightKey).toString());
if (weight > 0) {
zookeeperRegistry.getServieWeightMap().put(weightKey, weight);
}
}
} catch (Throwable e) {
logger.error("parse interface weight error", e);
}
});
zookeeperRegistry.register(FateServer.serviceSets);
}
ModelService modelService = applicationContext.getBean(ModelService.class);
modelService.restore();
ConsoleReporter reporter = applicationContext.getBean(ConsoleReporter.class);
reporter.start(1, TimeUnit.MINUTES);
JmxReporter jmxReporter = applicationContext.getBean(JmxReporter.class);
jmxReporter.start();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
logger.info("*** shutting down gRPC server since JVM is shutting down");
ServingServer.this.stop();
logger.info("*** server shut down");
}
});
}
@Bean
public JmxReporter jmxReporter(MetricRegistry metrics) {
return JmxReporter.forRegistry(metrics).build();
}
private void initJmxReporter(String domain) {
jmxReporter = JmxReporter.forRegistry(METRIC_REGISTRY_INSTANCE).inDomain(domain).build();
}
public JmxMeterRegistry(JmxConfig config, Clock clock, HierarchicalNameMapper nameMapper, MetricRegistry metricRegistry,
JmxReporter jmxReporter) {
super(config, metricRegistry, nameMapper, clock);
this.reporter = jmxReporter;
this.reporter.start();
}
private static JmxReporter defaultJmxReporter(JmxConfig config, MetricRegistry metricRegistry) {
return JmxReporter.forRegistry(metricRegistry)
.inDomain(config.domain())
.build();
}
public JmxReporterService(String domain, MetricRegistry metricRegistry) {
this.reporter = JmxReporter.forRegistry(metricRegistry)
.inDomain(domain)
.build();
}
@Override
public void configureAndStart(String name, MetricRegistry registry, MetricFilter filter) {
reporter = JmxReporter.forRegistry(registry).convertRatesTo(rateUnit).convertDurationsTo(durationUnit).filter(filter).build();
reporter.start();
}
@Bean(destroyMethod = "stop")
public JmxReporter jmxReporter() {
JmxReporter reporter = JmxReporter.forRegistry(metricRegistry).build();
reporter.start();
return reporter;
}
public MetricsComponent() {
metricRegistry = new MetricRegistry();
reporter = JmxReporter.forRegistry(metricRegistry).build();
}
public JmxReporter getReporter() {
return reporter;
}
@Inject
public DropWizardMetricFactory(MetricRegistry metricRegistry) {
this.metricRegistry = metricRegistry;
this.jmxReporter = JmxReporter.forRegistry(metricRegistry)
.build();
}
private MetricUtil() {
JmxReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.MINUTES)
.build()
.start();
}
private void startJmxReporter() {
JmxReporter reporter = JmxReporter.forRegistry(registry).build();
reporter.start();
}