Java源码示例:io.vertx.ext.dropwizard.DropwizardMetricsOptions
示例1
/**
* Configure Dropwizard helper.
* @param graphiteHost graphite server host
* @param port graphits server port
* @param tu time unit
* @param period reporting period
* @param vopt Vert.x options
* @param hostName logical hostname for this node (reporting)
*/
public static void config(String graphiteHost, int port, TimeUnit tu,
int period, VertxOptions vopt, String hostName) {
final String registryName = "okapi";
MetricRegistry registry = SharedMetricRegistries.getOrCreate(registryName);
DropwizardMetricsOptions metricsOpt = new DropwizardMetricsOptions();
metricsOpt.setEnabled(true).setRegistryName(registryName);
vopt.setMetricsOptions(metricsOpt);
Graphite graphite = new Graphite(new InetSocketAddress(graphiteHost, port));
final String prefix = "folio.okapi." + hostName;
GraphiteReporter reporter = GraphiteReporter.forRegistry(registry)
.prefixedWith(prefix)
.build(graphite);
reporter.start(period, tu);
logger.info("Metrics remote {}:{} this {}", graphiteHost, port, prefix);
}
示例2
@Test
public void testLoadingFromFile() throws Exception {
String filePath = ClassLoader.getSystemResource("test_metrics_config.json").getFile();
DropwizardMetricsOptions dmo = new DropwizardMetricsOptions().setConfigPath(filePath);
VertxOptions vertxOptions = new VertxOptions().setMetricsOptions(dmo);
// Verify our jmx domain isn't there already, just in case.
assertFalse(Arrays.asList(ManagementFactory.getPlatformMBeanServer().getDomains()).contains("test-jmx-domain"));
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
metrics = vmfi.metrics(vertxOptions);
List<String> jmxDomains = Arrays.asList(ManagementFactory.getPlatformMBeanServer().getDomains());
// If our file was loaded correctly, then our jmx domain will exist.
assertTrue(jmxDomains.contains("test-jmx-domain"));
}
示例3
@Test
public void testloadingFileFromClasspath() throws Exception {
String path = "test_metrics_config.json";
DropwizardMetricsOptions dmo = new DropwizardMetricsOptions().setConfigPath(path);
VertxOptions vertxOptions = new VertxOptions().setMetricsOptions(dmo);
// Verify our jmx domain isn't there already, just in case.
assertFalse(Arrays.asList(ManagementFactory.getPlatformMBeanServer().getDomains()).contains("test-jmx-domain"));
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
metrics = vmfi.metrics(vertxOptions);
List<String> jmxDomains = Arrays.asList(ManagementFactory.getPlatformMBeanServer().getDomains());
// If our file was loaded correctly, then our jmx domain will exist.
assertTrue(jmxDomains.contains("test-jmx-domain"));
}
示例4
@Test
public void testLoadingWithMissingFile() throws Exception {
String filePath = "/i/am/not/here/missingfile.json";
DropwizardMetricsOptions dmo = new DropwizardMetricsOptions()
.setJmxEnabled(true)
.setJmxDomain("non-file-jmx")
.setConfigPath(filePath);
VertxOptions vertxOptions = new VertxOptions().setMetricsOptions(dmo);
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
metrics = vmfi.metrics(vertxOptions);
List<String> jmxDomains = Arrays.asList(ManagementFactory.getPlatformMBeanServer().getDomains());
assertFalse(jmxDomains.contains("test-jmx-domain"));
assertTrue(jmxDomains.contains("non-file-jmx"));
}
示例5
@Provides
@Singleton
public Vertx getVertx()
{
return Vertx.vertx(new VertxOptions().setMetricsOptions(new DropwizardMetricsOptions()
.setEnabled(true)
.setJmxEnabled(true)
.setJmxDomain("cassandra-sidecar-metrics")));
}
示例6
@Bean
Vertx vertx(@Value("${vertx.worker-pool-size}") int workerPoolSize) {
return Vertx.vertx(new VertxOptions()
.setWorkerPoolSize(workerPoolSize)
.setMetricsOptions(new DropwizardMetricsOptions()
.setEnabled(true)
.setRegistryName(MetricsConfiguration.METRIC_REGISTRY_NAME)));
}
示例7
@Before
public void setUp(TestContext context) {
String graphiteHost = System.getProperty("graphiteHost");
final String registryName = "okapi";
MetricRegistry registry = SharedMetricRegistries.getOrCreate(registryName);
// Note the setEnabled (true or false)
DropwizardMetricsOptions metricsOpt = new DropwizardMetricsOptions().
setEnabled(false).setRegistryName(registryName);
vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(metricsOpt));
reporter1 = ConsoleReporter.forRegistry(registry).build();
reporter1.start(1, TimeUnit.SECONDS);
if (graphiteHost != null) {
Graphite graphite = new Graphite(new InetSocketAddress(graphiteHost, 2003));
reporter2 = GraphiteReporter.forRegistry(registry)
.prefixedWith("okapiserver")
.build(graphite);
reporter2.start(1, TimeUnit.MILLISECONDS);
}
DeploymentOptions opt = new DeploymentOptions()
.setConfig(new JsonObject().put("port", Integer.toString(port)));
vertx.deployVerticle(MainVerticle.class.getName(),
opt, context.asyncAssertSuccess());
httpClient = vertx.createHttpClient();
}
示例8
@Override
public void beforeStartingVertx(VertxOptions options) {
// TODO Auto-generated method stub
super.beforeStartingVertx(options);
System.out.println("starting rest verticle service..........");
options.setBlockedThreadCheckInterval(1500000);
options.setWarningExceptionTime(1500000);
boolean enabled = options.getMetricsOptions().isEnabled();
options.setMetricsOptions(new DropwizardMetricsOptions().setEnabled(enabled).addMonitoredHttpServerUri(
new Match().setValue("/.*").setType(MatchType.REGEX)));
}
示例9
public static void main(String[] args) {
VertxOptions vOpts = new VertxOptions();
DeploymentOptions options = new DeploymentOptions().setInstances(4);
vOpts.setClustered(true);
vOpts.setMetricsOptions(new DropwizardMetricsOptions().setEnabled(true));
Vertx.clusteredVertx(vOpts, cluster-> {
if(cluster.succeeded()){
final Vertx result = cluster.result();
result.deployVerticle("org.jacpfx.vertx.entrypoint.ServiceEntryPoint",options, handle -> {
});
}
});
}
示例10
public void setupJMXWithDomain() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().
setJmxEnabled(true).
setJmxDomain("mydomain")
));
}
示例11
public void setupMonitoredHandlers() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().
setEnabled(true).
addMonitoredEventBusHandler(
new Match().setValue("some-address")).
addMonitoredEventBusHandler(
new Match().setValue("business-.*").setType(MatchType.REGEX))
));
}
示例12
public void setupMonitoredUris() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().
setEnabled(true).
addMonitoredHttpServerUri(
new Match().setValue("/")).
addMonitoredHttpServerUri(
new Match().setValue("/foo/.*").setType(MatchType.REGEX))
));
}
示例13
public void setupMonitoredUrisWithAliases() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().
setEnabled(true).
addMonitoredHttpServerUri(new Match().setValue("/users/.*").setAlias("users").setType(MatchType.REGEX))
));
}
示例14
public void setupMonitoredEndpoints() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().
setEnabled(true).
addMonitoredHttpClientEndpoint(
new Match().setValue("some-host:80")).
addMonitoredHttpClientEndpoint(
new Match().setValue("another-host:.*").setType(MatchType.REGEX))
));
}
示例15
public void getRegistry() {
VertxOptions options = new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().setEnabled(true).setRegistryName("my-registry")
);
Vertx vertx = Vertx.vertx(options);
// Get the registry
MetricRegistry registry = SharedMetricRegistries.getOrCreate("my-registry");
// Do whatever you need with the registry
}
示例16
public void example4() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions()
.setEnabled(true)
.setJmxEnabled(true)
.setJmxDomain("vertx-metrics")));
}
示例17
VertxMetricsImpl(MetricRegistry registry, boolean shutdown, VertxOptions options, DropwizardMetricsOptions metricsOptions, String baseName) {
super(registry, baseName);
this.options = metricsOptions;
this.shutdown = shutdown;
gauge(options::getEventLoopPoolSize, "event-loop-size");
gauge(options::getWorkerPoolSize, "worker-pool-size");
}
示例18
EventBusMetricsImpl(AbstractMetrics metrics, String baseName, DropwizardMetricsOptions options) {
super(metrics.registry(), baseName);
handlerCount = counter("handlers");
pending = counter("messages", "pending");
pendingLocal = counter("messages", "pending-local");
pendingRemote = counter("messages", "pending-remote");
discarded = counter("messages", "discarded");
discardedLocal = counter("messages", "discarded-local");
discardedRemote = counter("messages", "discarded-remote");
receivedMessages = throughputMeter("messages", "received");
receivedLocalMessages = throughputMeter("messages", "received-local");
receivedRemoteMessages = throughputMeter("messages", "received-remote");
deliveredMessages = throughputMeter("messages", "delivered");
deliveredLocalMessages = throughputMeter("messages", "delivered-local");
deliveredRemoteMessages = throughputMeter("messages", "delivered-remote");
sentMessages = throughputMeter("messages", "sent");
sentLocalMessages = throughputMeter("messages", "sent-local");
sentRemoteMessages = throughputMeter("messages", "sent-remote");
publishedMessages = throughputMeter("messages", "published");
publishedLocalMessages = throughputMeter("messages", "published-local");
publishedRemoteMessages = throughputMeter("messages", "published-remote");
replyFailures = meter("messages", "reply-failures");
bytesRead = meter("messages", "bytes-read");
bytesWritten = meter("messages", "bytes-written");
handlerMatcher = new Matcher(options.getMonitoredEventBusHandlers());
}
示例19
@Test
public void testLoadingWithJmxDisabled() throws Exception {
String filePath = ClassLoader.getSystemResource("test_metrics_config_jmx_disabled.json").getFile();
DropwizardMetricsOptions dmo = new DropwizardMetricsOptions().setConfigPath(filePath);
VertxOptions vertxOptions = new VertxOptions().setMetricsOptions(dmo);
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
metrics = vmfi.metrics(vertxOptions);
List<String> jmxDomains = Arrays.asList(ManagementFactory.getPlatformMBeanServer().getDomains());
// If our file was loaded correctly, then our jmx domain will exist.
assertFalse(jmxDomains.contains("test-jmx-domain"));
}
示例20
@Test
public void testWithNoConfigFile() throws Exception {
DropwizardMetricsOptions dmo = new DropwizardMetricsOptions()
.setJmxEnabled(true)
.setJmxDomain("non-file-jmx");
VertxOptions vertxOptions = new VertxOptions().setMetricsOptions(dmo);
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
metrics = vmfi.metrics(vertxOptions);
List<String> jmxDomains = Arrays.asList(ManagementFactory.getPlatformMBeanServer().getDomains());
assertFalse(jmxDomains.contains("test-jmx-domain"));
assertTrue(jmxDomains.contains("non-file-jmx"));
}
示例21
@Test
public void testFromJson() throws Exception {
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
VertxMetricsImpl metrics = (VertxMetricsImpl) vmfi.metrics(new VertxOptions(
new JsonObject().put("metricsOptions", new JsonObject()
.put("enabled", true)
.put("monitoredEventBusHandlers", new JsonArray()
.add(new JsonObject().put("value", "foo")))
.put("monitoredHttpServerUris", new JsonArray()
.add(new JsonObject().put("value", "http://www.bar.com")))
.put("monitoredHttpClientUris", new JsonArray()
.add(new JsonObject().put("value", "http://www.baz.com")))
.put("monitoredHttpClientEndpoints", new JsonArray()
.add(new JsonObject().put("value", "http://www.foobar.com"))))
));
DropwizardMetricsOptions options = metrics.getOptions();
assertEquals(1, options.getMonitoredEventBusHandlers().size());
assertEquals("foo", options.getMonitoredEventBusHandlers().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredEventBusHandlers().get(0).getType());
assertEquals(1, options.getMonitoredHttpServerUris().size());
assertEquals("http://www.bar.com", options.getMonitoredHttpServerUris().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredHttpServerUris().get(0).getType());
assertEquals(1, options.getMonitoredHttpClientUris().size());
assertEquals("http://www.baz.com", options.getMonitoredHttpClientUris().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredHttpClientUris().get(0).getType());
assertEquals(1, options.getMonitoredHttpClientEndpoint().size());
assertEquals("http://www.foobar.com", options.getMonitoredHttpClientEndpoint().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredHttpClientEndpoint().get(0).getType());
}
示例22
public static void main(String[] args) {
System.setProperty("vertx.logger-delegate-factory-class-name", "io.vertx.core.logging.SLF4JLogDelegateFactory");
// Initialize metric registry
String registryName = "registry";
MetricRegistry registry = SharedMetricRegistries.getOrCreate(registryName);
SharedMetricRegistries.setDefault(registryName);
Slf4jReporter reporter = Slf4jReporter.forRegistry(registry)
.outputTo(LoggerFactory.getLogger(Application.class))
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.MINUTES);
// Initialize vertx with the metric registry
DropwizardMetricsOptions metricsOptions = new DropwizardMetricsOptions()
.setEnabled(true)
.setMetricRegistry(registry);
VertxOptions vertxOptions = new VertxOptions().setMetricsOptions(metricsOptions);
Vertx vertx = Vertx.vertx(vertxOptions);
ConfigRetrieverOptions configRetrieverOptions = getConfigRetrieverOptions();
ConfigRetriever configRetriever = ConfigRetriever.create(vertx, configRetrieverOptions);
// getConfig is called for initial loading
configRetriever.getConfig(
ar -> {
int instances = Runtime.getRuntime().availableProcessors();
DeploymentOptions deploymentOptions =
new DeploymentOptions().setInstances(instances).setConfig(ar.result());
vertx.deployVerticle(MainVerticle.class, deploymentOptions);
});
// listen is called each time configuration changes
configRetriever.listen(
change -> {
JsonObject updatedConfiguration = change.getNewConfiguration();
vertx.eventBus().publish(EventBusChannels.CONFIGURATION_CHANGED.name(), updatedConfiguration);
});
}
示例23
public static void main(String...args) {
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
ConfigArgs configArgs = ConfigArgs.parse(args);
// load assets or die
try {
weakOrBreachedPwBf = BloomFilters.deserialize(AssetRegistry.getCompromisedPasswords());
disposableMxBf = BloomFilters.deserialize(AssetRegistry.getDisposableEmails());
awsIpMembership = new CidrMembership<>(AssetRegistry.getAwsIps(), "aws");
gcpIpMembership = new CidrMembership<>(AssetRegistry.getGcpIps(), "gcp");
} catch (AssetException ex) {
log.error("error while reading asset", ex);
System.exit(-1);
}
final GeoIp4j geoIp4j = new GeoIp4jImpl();
log.info("{} <= quarantyne => {}", configArgs.getIngress().toHuman(), configArgs.getEgress().toHuman());
configArgs.getAdminIpPort().ifPresent(ipPort -> {
log.info("==> admin @ http://{}:{}", ipPort.getIp(), ipPort.getPort());
});
log.info("see available options with --help");
int numCpus = CpuCoreSensor.availableProcessors();
VertxOptions vertxOptions = new VertxOptions();
vertxOptions.setPreferNativeTransport(true);
vertxOptions.setMetricsOptions(
new DropwizardMetricsOptions().setEnabled(true)
);
log.debug("==> event loop size is {}", vertxOptions.getEventLoopPoolSize());
log.debug("==> detected {} cpus core", numCpus);
Vertx vertx = Vertx.vertx(vertxOptions);
ConfigSupplier configSupplier;
if (configArgs.getConfigFile().isPresent()) {
configSupplier = new ConfigSupplier(vertx,
new ConfigRetrieverOptionsSupplier(configArgs.getConfigFile().get()));
} else {
log.info("No configuration file was specified, using default settings");
configSupplier = new ConfigSupplier();
}
// quarantyne classifiers
List<HttpRequestClassifier> httpRequestClassifierList = Lists.newArrayList(
new FastAgentClassifier(),
new IpRotationClassifier(),
new SuspiciousRequestHeadersClassifier(),
new SuspiciousUserAgentClassifier(),
new LargeBodySizeClassifier(),
new CompromisedPasswordClassifier(weakOrBreachedPwBf, configSupplier),
new DisposableEmailClassifier(disposableMxBf, configSupplier),
new GeoDiscrepancyClassifier(geoIp4j, configSupplier),
new PublicCloudExecutionClassifier(awsIpMembership, gcpIpMembership)
// new SuspiciousLoginActivityClassifier(geoIp4j)
);
MainClassifier mainClassifier = new MainClassifier(httpRequestClassifierList);
if (configArgs.getAdminIpPort().isPresent()) {
vertx.deployVerticle(new AdminVerticle(configArgs.getAdminIpPort().get()));
}
vertx.deployVerticle(() -> new ProxyVerticle(configArgs, mainClassifier,
configSupplier),
new DeploymentOptions().setInstances(numCpus * 2 + 1));
vertx.deployVerticle(() -> new WarmupVerticle(configArgs),
new DeploymentOptions(),
warmupVerticle -> {
vertx.undeploy(warmupVerticle.result());
});
vertx.exceptionHandler(ex -> {
log.error("uncaught exception", ex);
});
}
示例24
public void setup() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().setEnabled(true)
));
}
示例25
public void setupJMX() {
Vertx vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(
new DropwizardMetricsOptions().setJmxEnabled(true)
));
}
示例26
public void baseName() {
DropwizardMetricsOptions metricsOptions =
new DropwizardMetricsOptions().setBaseName("foo");
}
示例27
@Override
public MetricsOptions newOptions(JsonObject jsonObject) {
return jsonObject == null ? new DropwizardMetricsOptions() : new DropwizardMetricsOptions(jsonObject);
}
示例28
DropwizardMetricsOptions getOptions() {
return options;
}
示例29
@Test
public void testFromJsonMixed() throws Exception {
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
VertxMetricsImpl metrics = (VertxMetricsImpl) vmfi.metrics(new VertxOptions(
new JsonObject().put("metricsOptions", new JsonObject()
.put("enabled", true)
.put("monitoredEventBusHandlers", new JsonArray()
.add(new JsonObject().put("value", "foo")))
.put("monitoredHttpServerUris", new JsonArray()
.add(new JsonObject().put("value", "http://www.bar.com")))
.put("monitoredHttpClientUris", new JsonArray()
.add(new JsonObject().put("value", "http://www.baz.com")))
.put("monitoredHttpClientEndpoints", new JsonArray()
.add(new JsonObject().put("value", "http://www.foobar.com")))
// Deprecated fields
.put("monitoredHandlers", new JsonArray()
.add(new JsonObject().put("value", "will be ignored")))
.put("monitoredServerUris", new JsonArray()
.add(new JsonObject().put("value", "will be ignored")))
.put("monitoredClientUris", new JsonArray()
.add(new JsonObject().put("value", "will be ignored")))
.put("monitoredClientEndpoints", new JsonArray()
.add(new JsonObject().put("value", "will be ignored"))))
));
DropwizardMetricsOptions options = metrics.getOptions();
assertEquals(1, options.getMonitoredEventBusHandlers().size());
assertEquals("foo", options.getMonitoredEventBusHandlers().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredEventBusHandlers().get(0).getType());
assertEquals(1, options.getMonitoredHttpServerUris().size());
assertEquals("http://www.bar.com", options.getMonitoredHttpServerUris().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredHttpServerUris().get(0).getType());
assertEquals(1, options.getMonitoredHttpClientUris().size());
assertEquals("http://www.baz.com", options.getMonitoredHttpClientUris().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredHttpClientUris().get(0).getType());
assertEquals(1, options.getMonitoredHttpClientEndpoint().size());
assertEquals("http://www.foobar.com", options.getMonitoredHttpClientEndpoint().get(0).getValue());
assertEquals(MatchType.EQUALS, options.getMonitoredHttpClientEndpoint().get(0).getType());
}
示例30
@Test
public void testDefaultBaseName() {
VertxMetricsFactoryImpl vmfi = new VertxMetricsFactoryImpl();
VertxMetricsImpl metrics = (VertxMetricsImpl) vmfi.metrics(new VertxOptions().setMetricsOptions(new DropwizardMetricsOptions()));
assertEquals("vertx", metrics.baseName());
}