use of org.apache.flink.metrics.Counter in project flink by apache.
the class PrometheusReporterTaskScopeTest method countersCanBeAddedSeveralTimesIfTheyDifferInLabels.
@Test
void countersCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestException {
Counter counter1 = new SimpleCounter();
counter1.inc(1);
Counter counter2 = new SimpleCounter();
counter2.inc(2);
taskMetricGroup1.counter("my_counter", counter1);
taskMetricGroup2.counter("my_counter", counter2);
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1)).isEqualTo(1.);
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues2)).isEqualTo(2.);
}
use of org.apache.flink.metrics.Counter in project flink by apache.
the class PrometheusReporterTaskScopeTest method removingSingleInstanceOfMetricDoesNotBreakOtherInstances.
@Test
void removingSingleInstanceOfMetricDoesNotBreakOtherInstances() throws UnirestException {
Counter counter1 = new SimpleCounter();
counter1.inc(1);
Counter counter2 = new SimpleCounter();
counter2.inc(2);
taskMetricGroup1.counter("my_counter", counter1);
taskMetricGroup2.counter("my_counter", counter2);
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1)).isEqualTo(1.);
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues2)).isEqualTo(2.);
taskMetricGroup2.close();
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1)).isEqualTo(1.);
taskMetricGroup1.close();
assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_counter", LABEL_NAMES, labelValues1)).isNull();
}
use of org.apache.flink.metrics.Counter in project flink by apache.
the class StatsDReporterTest method testAddingMetrics.
/**
* Tests that the registered metrics' names don't contain invalid characters.
*/
@Test
void testAddingMetrics() {
String counterName = "testCounter";
final String scope = "scope";
final char delimiter = '_';
MetricGroup metricGroup = TestMetricGroup.newBuilder().setMetricIdentifierFunction((metricName, characterFilter) -> scope + delimiter + metricName).build();
TestingStatsDReporter reporter = new TestingStatsDReporter();
reporter.open(new MetricConfig());
SimpleCounter myCounter = new SimpleCounter();
reporter.notifyOfAddedMetric(myCounter, counterName, metricGroup);
Map<Counter, String> counters = reporter.getCounters();
assertThat(counters).containsKey(myCounter);
String expectedCounterName = reporter.filterCharacters(scope) + delimiter + reporter.filterCharacters(counterName);
assertThat(counters.get(myCounter)).isEqualTo(expectedCounterName);
}
use of org.apache.flink.metrics.Counter in project flink by apache.
the class JMXReporter method notifyOfAddedMetric.
// ------------------------------------------------------------------------
// adding / removing metrics
// ------------------------------------------------------------------------
@Override
public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) {
final String domain = generateJmxDomain(metricName, group);
final Hashtable<String, String> table = generateJmxTable(group.getAllVariables());
AbstractBean jmxMetric;
ObjectName jmxName;
try {
jmxName = new ObjectName(domain, table);
} catch (MalformedObjectNameException e) {
/*
* There is an implementation error on our side if this occurs. Either the domain was
* modified and no longer conforms to the JMX domain rules or the table wasn't properly
* generated.
*/
LOG.debug("Implementation error. The domain or table does not conform to JMX rules.", e);
return;
}
if (metric instanceof Gauge) {
jmxMetric = new JmxGauge((Gauge<?>) metric);
} else if (metric instanceof Counter) {
jmxMetric = new JmxCounter((Counter) metric);
} else if (metric instanceof Histogram) {
jmxMetric = new JmxHistogram((Histogram) metric);
} else if (metric instanceof Meter) {
jmxMetric = new JmxMeter((Meter) metric);
} else {
LOG.error("Cannot add unknown metric type: {}. This indicates that the metric type " + "is not supported by this reporter.", metric.getClass().getName());
return;
}
try {
synchronized (this) {
mBeanServer.registerMBean(jmxMetric, jmxName);
registeredMetrics.put(metric, jmxName);
}
} catch (NotCompliantMBeanException e) {
// implementation error on our side
LOG.debug("Metric did not comply with JMX MBean rules.", e);
} catch (InstanceAlreadyExistsException e) {
LOG.warn("A metric with the name " + jmxName + " was already registered.", e);
} catch (Throwable t) {
LOG.warn("Failed to register metric", t);
}
}
use of org.apache.flink.metrics.Counter in project flink by apache.
the class InfluxdbReporter method buildReport.
@Nullable
private BatchPoints buildReport() {
Instant timestamp = Instant.now();
BatchPoints.Builder report = BatchPoints.database(database);
report.retentionPolicy(retentionPolicy);
report.consistency(consistency);
try {
for (Map.Entry<Gauge<?>, MeasurementInfo> entry : gauges.entrySet()) {
report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
}
for (Map.Entry<Counter, MeasurementInfo> entry : counters.entrySet()) {
report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
}
for (Map.Entry<Histogram, MeasurementInfo> entry : histograms.entrySet()) {
report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
}
for (Map.Entry<Meter, MeasurementInfo> entry : meters.entrySet()) {
report.point(MetricMapper.map(entry.getValue(), timestamp, entry.getKey()));
}
} catch (ConcurrentModificationException | NoSuchElementException e) {
// report next time
return null;
}
return report.build();
}
Aggregations