0

我正在使用的 DropwizardMetricServices#submit() 没有第二次提交仪表指标。

即我的用例是在阅读后从 JMX 中删除计量指标。我的应用程序可以发送相同的指标(具有不同的值)。

第一次成功提交仪表指标(然后我的应用程序在读取指标后将其删除)。但是,第二次没有提交相同的指标。

所以,我有点困惑 DropwizardMetricServices#submit() 第二次不工作的原因是什么?

下面是代码:

提交指标:

private void submitNonSparseMetric(final String metricName, final long value) {
    validateMetricName(metricName);
    metricService.submit(metricName, value); // metricService is the DropwizardMetricServices
    log(metricName, value);
    LOGGER.debug("Submitted the metric {} to JMX", metricName);
}

读取和删除指标的代码:

protected void collectMetrics() {
    // Create the connection
    Long currTime = System.currentTimeMillis()/1000; // Graphite needs  
    Socket connection = createConnection();
    if (connection == null){
        return;
    }
    // Get the output stream
    DataOutputStream outputStream = getDataOutputStream(connection);
    if (outputStream == null){
        closeConnection();
        return;
    }
    // Get metrics from JMX
    Map<String, Gauge> g = metricRegistry.getGauges(); // metricRegistry is com.codahale.metrics.MetricRegistry
    for(Entry<String, Gauge> e : g.entrySet()){
        String key = e.getKey();
        if(p2cMetric(key)){
            String metricName = convertToMetricStandard(key);
            String metricValue = String.valueOf(e.getValue().getValue());

            String metricToSend = String.format("%s %s %s\n", metricName, metricValue, currTime);
            try {
                writeToStream(outputStream, metricToSend);
                // Remove the metric from JMX after successfully sending metric to graphite
                removeMetricFromJMX(key);
            } catch (IOException e1) {
                LOGGER.error("Unable to send metric to Graphite - {}", e1.getMessage());
            }
        }

    }

    closeOutputStream();
    closeConnection();
}
4

1 回答 1

0

我想我找到了问题所在。

根据 DropwizardMetricServices 文档 - https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/metrics/dropwizard/DropwizardMetricServices.html#submit-java.lang.String- double- , submit() 方法Set the specified gauge value。因此,我认为建议使用 DropwizardMetricServices#submit() 方法仅设置 JMX 中任何现有量规指标的值,而不是向 JMX 添加任何新指标。

因此,一旦我用 MetricRegistry#register() (com.codahale.metrics.MetricRegistry) 方法替换了 DropwizardMetricServices#submit() 以提交我的所有指标,它按预期工作,并且我的指标被读取到 JMX(一旦它们被我的应用程序删除) )。

但是,我只是想知道是什么让 DropwizardMetricServices#submit() 只向 JMX 添加新指标,而不是任何已经删除的指标(从 JMX)。DropwizardMetricServices 是否缓存(在内存中)提交给 JMX 的所有指标?这使得 DropwizardMetricServices#submit() 方法不重新提交指标?

于 2017-12-05T06:51:20.790 回答