下面列出了怎么用org.springframework.boot.actuate.metrics.MetricsEndpoint的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Metrics method
*
* @param name metrics name to display
* @param tags tags to filter with
* @return metrics
*/
@ShellMethod(key = "metrics", value = "Display metrics endpoint.")
@ShellMethodAvailability("metricsAvailability")
public Object metrics(
@ShellOption(value = {"-n", "--name"}, help = "Metric name to get", defaultValue = ShellOption.NULL) String name,
@ShellOption(value = {"-t", "--tags"}, help = "Tags (key=value, separated by coma)", defaultValue =
ShellOption.NULL) String tags
) {
if (name != null) {
MetricsEndpoint.MetricResponse result = metrics.metric(name, tags != null ? Arrays.asList(tags.split(",")
) : null);
if (result == null) {
String tagsStr = tags != null ? " and tags: " + tags : "";
throw new IllegalArgumentException("No result for metrics name: " + name + tagsStr);
}
return result;
}
return metrics.listNames();
}
/**
* Read thread size from metric response
*
* @param response metric response
* @return thread size
*/
private int getThreadSize(MetricResponse response) {
List<MetricsEndpoint.Sample> measurements = response.getMeasurements();
if (measurements != null && !measurements.isEmpty()) {
MetricsEndpoint.Sample defaultSample = measurements.get(0);
return defaultSample.getValue().intValue();
}
return 0;
}
/**
* Read metrics sample size as MB
*
* @param response metric response from actuator
* @return size as MB
*/
@NonNull
private int readSampleSizeAsMB(MetricResponse response) {
List<MetricsEndpoint.Sample> measurements = response.getMeasurements();
if (measurements != null && !measurements.isEmpty()) {
MetricsEndpoint.Sample defaultSample = measurements.get(0);
return convertBitToMB(defaultSample.getValue());
}
return 0;
}
public ActuatorCommand(ApplicationContext applicationContext, Environment environment,
SshShellProperties properties, SshShellHelper helper,
@Lazy AuditEventsEndpoint audit, @Lazy BeansEndpoint beans,
@Lazy ConditionsReportEndpoint conditions,
@Lazy ConfigurationPropertiesReportEndpoint configprops, @Lazy EnvironmentEndpoint env,
@Lazy HealthEndpoint health,
@Lazy HttpTraceEndpoint httptrace, @Lazy InfoEndpoint info, @Lazy LoggersEndpoint loggers,
@Lazy MetricsEndpoint metrics,
@Lazy MappingsEndpoint mappings, @Lazy ScheduledTasksEndpoint scheduledtasks,
@Lazy ShutdownEndpoint shutdown,
@Lazy ThreadDumpEndpoint threaddump) {
this.applicationContext = applicationContext;
this.environment = environment;
this.properties = properties;
this.helper = helper;
this.audit = audit;
this.beans = beans;
this.conditions = conditions;
this.configprops = configprops;
this.env = env;
this.health = health;
this.httptrace = httptrace;
this.info = info;
this.loggers = loggers;
this.metrics = metrics;
this.mappings = mappings;
this.scheduledtasks = scheduledtasks;
this.shutdown = shutdown;
this.threaddump = threaddump;
}
@Bean
@ConditionalOnMissingBean
public ActuatorMemoryDimension createMemoryDimension(MetricsEndpoint endpoint) {
return new ActuatorMemoryDimension(endpoint);
}
@Bean
@ConditionalOnMissingBean
public ActuatorThreadSummaryDimension createThreadSummaryDimension(MetricsEndpoint endpoint) {
return new ActuatorThreadSummaryDimension(endpoint);
}
public ActuatorThreadSummaryDimension(MetricsEndpoint endpoint) {
this.endpoint = endpoint;
}
public ActuatorMemoryDimension(MetricsEndpoint endpoint) {
this.endpoint = endpoint;
Collections.addAll(heapTags, TAG_HEAP);
Collections.addAll(noneHeapTags, TAG_NON_HEAP);
Collections.addAll(noneHeapMetaSpaceTags, TAG_NON_HEAP, TAG_METASPACE);
}
@Bean
public ActuatorMemoryDimension createMemoryDimension(MetricsEndpoint endpoint) {
return new ActuatorMemoryDimension(endpoint);
}
@Bean
public ActuatorThreadSummaryDimension createThreadSummaryDimension(MetricsEndpoint endpoint) {
return new ActuatorThreadSummaryDimension(endpoint);
}
/**
* @return whether `metrics` command is available
*/
public Availability metricsAvailability() {
return availability("metrics", MetricsEndpoint.class);
}
@Test
void testMetrics() {
assertEquals(metrics.listNames().getNames().size(),
((MetricsEndpoint.ListNamesResponse) cmd.metrics(null, null)).getNames().size());
}
@Test
void testMetricsName() {
assertEquals(metrics.metric("jvm.memory.max", null).getName(),
((MetricsEndpoint.MetricResponse) cmd.metrics("jvm.memory.max", null)).getName());
}
@Test
void testMetricsTags() {
assertEquals(metrics.metric("jvm.memory.max", Collections.singletonList("area:heap")).getName(),
((MetricsEndpoint.MetricResponse) cmd.metrics("jvm.memory.max",
"area:heap")).getName());
}
MetricsCommand(@Value("${sshd.system.command.roles.metrics}") String[] systemRoles,
MetricsEndpoint metricsEndpoint) {
super(systemRoles);
this.metricsEndpoint = metricsEndpoint;
}