Search in sources :

Example 6 with Histogram

use of org.apache.flink.metrics.Histogram in project flink by apache.

the class MetricQueryServiceTest method testHandleOversizedMetricMessage.

@Test
public void testHandleOversizedMetricMessage() throws Exception {
    final long sizeLimit = 200L;
    MetricQueryService queryService = MetricQueryService.createMetricQueryService(rpcService, ResourceID.generate(), sizeLimit);
    queryService.start();
    final TaskManagerMetricGroup tm = UnregisteredMetricGroups.createUnregisteredTaskManagerMetricGroup();
    final String gaugeValue = "Hello";
    final long requiredGaugesToExceedLimit = sizeLimit / gaugeValue.length() + 1;
    List<Tuple2<String, Gauge<String>>> gauges = LongStream.range(0, requiredGaugesToExceedLimit).mapToObj(x -> Tuple2.of("gauge" + x, (Gauge<String>) () -> "Hello" + x)).collect(Collectors.toList());
    gauges.forEach(gauge -> queryService.addMetric(gauge.f0, gauge.f1, tm));
    queryService.addMetric("counter", new SimpleCounter(), tm);
    queryService.addMetric("histogram", new TestHistogram(), tm);
    queryService.addMetric("meter", new TestMeter(), tm);
    MetricDumpSerialization.MetricSerializationResult dump = queryService.queryMetrics(TIMEOUT).get();
    assertTrue(dump.serializedCounters.length > 0);
    assertEquals(1, dump.numCounters);
    assertTrue(dump.serializedMeters.length > 0);
    assertEquals(1, dump.numMeters);
    // gauges exceeded the size limit and will be excluded
    assertEquals(0, dump.serializedGauges.length);
    assertEquals(0, dump.numGauges);
    assertTrue(dump.serializedHistograms.length > 0);
    assertEquals(1, dump.numHistograms);
    // unregister all but one gauge to ensure gauges are reported again if the remaining fit
    for (int x = 1; x < gauges.size(); x++) {
        queryService.removeMetric(gauges.get(x).f1);
    }
    MetricDumpSerialization.MetricSerializationResult recoveredDump = queryService.queryMetrics(TIMEOUT).get();
    assertTrue(recoveredDump.serializedCounters.length > 0);
    assertEquals(1, recoveredDump.numCounters);
    assertTrue(recoveredDump.serializedMeters.length > 0);
    assertEquals(1, recoveredDump.numMeters);
    assertTrue(recoveredDump.serializedGauges.length > 0);
    assertEquals(1, recoveredDump.numGauges);
    assertTrue(recoveredDump.serializedHistograms.length > 0);
    assertEquals(1, recoveredDump.numHistograms);
}
Also used : AfterClass(org.junit.AfterClass) LongStream(java.util.stream.LongStream) BeforeClass(org.junit.BeforeClass) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TestingRpcService(org.apache.flink.runtime.rpc.TestingRpcService) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestMeter(org.apache.flink.metrics.util.TestMeter) Collectors(java.util.stream.Collectors) Histogram(org.apache.flink.metrics.Histogram) Meter(org.apache.flink.metrics.Meter) List(java.util.List) UnregisteredMetricGroups(org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups) TestHistogram(org.apache.flink.metrics.util.TestHistogram) After(org.junit.After) Gauge(org.apache.flink.metrics.Gauge) TestLogger(org.apache.flink.util.TestLogger) SimpleCounter(org.apache.flink.metrics.SimpleCounter) ResourceID(org.apache.flink.runtime.clusterframework.types.ResourceID) Time(org.apache.flink.api.common.time.Time) Counter(org.apache.flink.metrics.Counter) Assert.assertEquals(org.junit.Assert.assertEquals) TestMeter(org.apache.flink.metrics.util.TestMeter) TaskManagerMetricGroup(org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup) TestHistogram(org.apache.flink.metrics.util.TestHistogram) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Test(org.junit.Test)

Example 7 with Histogram

use of org.apache.flink.metrics.Histogram in project flink by apache.

the class LatencyStatsTest method testLatencyStats.

private static void testLatencyStats(final LatencyStats.Granularity granularity, final Consumer<List<Tuple2<String, Histogram>>> verifier) {
    final AbstractMetricGroup<?> dummyGroup = UnregisteredMetricGroups.createUnregisteredOperatorMetricGroup();
    final List<Tuple2<String, Histogram>> latencyHistograms = new ArrayList<>(4);
    final TestingMetricRegistry registry = TestingMetricRegistry.builder().setRegisterConsumer((metric, metricName, group) -> {
        if (metric instanceof Histogram) {
            latencyHistograms.add(Tuple2.of(group.getMetricIdentifier(metricName), (Histogram) metric));
        }
    }).build();
    final MetricGroup parentGroup = new GenericMetricGroup(registry, dummyGroup, PARENT_GROUP_NAME);
    final LatencyStats latencyStats = new LatencyStats(parentGroup, MetricOptions.LATENCY_HISTORY_SIZE.defaultValue(), OPERATOR_SUBTASK_INDEX, OPERATOR_ID, granularity);
    latencyStats.reportLatency(new LatencyMarker(0L, SOURCE_ID_1, 0));
    latencyStats.reportLatency(new LatencyMarker(0L, SOURCE_ID_1, 0));
    latencyStats.reportLatency(new LatencyMarker(0L, SOURCE_ID_1, 1));
    latencyStats.reportLatency(new LatencyMarker(0L, SOURCE_ID_2, 2));
    latencyStats.reportLatency(new LatencyMarker(0L, SOURCE_ID_2, 3));
    verifier.accept(latencyHistograms);
}
Also used : Tuple2(org.apache.flink.api.java.tuple.Tuple2) Test(org.junit.Test) MetricOptions(org.apache.flink.configuration.MetricOptions) Histogram(org.apache.flink.metrics.Histogram) ArrayList(java.util.ArrayList) AbstractMetricGroup(org.apache.flink.runtime.metrics.groups.AbstractMetricGroup) Consumer(java.util.function.Consumer) MetricGroup(org.apache.flink.metrics.MetricGroup) List(java.util.List) UnregisteredMetricGroups(org.apache.flink.runtime.metrics.groups.UnregisteredMetricGroups) TestingMetricRegistry(org.apache.flink.runtime.metrics.util.TestingMetricRegistry) TestLogger(org.apache.flink.util.TestLogger) OperatorID(org.apache.flink.runtime.jobgraph.OperatorID) GenericMetricGroup(org.apache.flink.runtime.metrics.groups.GenericMetricGroup) LatencyMarker(org.apache.flink.streaming.runtime.streamrecord.LatencyMarker) Assert(org.junit.Assert) Histogram(org.apache.flink.metrics.Histogram) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TestingMetricRegistry(org.apache.flink.runtime.metrics.util.TestingMetricRegistry) LatencyMarker(org.apache.flink.streaming.runtime.streamrecord.LatencyMarker) ArrayList(java.util.ArrayList) AbstractMetricGroup(org.apache.flink.runtime.metrics.groups.AbstractMetricGroup) MetricGroup(org.apache.flink.metrics.MetricGroup) GenericMetricGroup(org.apache.flink.runtime.metrics.groups.GenericMetricGroup) GenericMetricGroup(org.apache.flink.runtime.metrics.groups.GenericMetricGroup)

Example 8 with Histogram

use of org.apache.flink.metrics.Histogram in project flink by apache.

the class MetricFetcherTest method createRequestDumpAnswer.

private static MetricDumpSerialization.MetricSerializationResult createRequestDumpAnswer(InstanceID tmID, JobID jobID) throws IOException {
    Map<Counter, Tuple2<QueryScopeInfo, String>> counters = new HashMap<>();
    Map<Gauge<?>, Tuple2<QueryScopeInfo, String>> gauges = new HashMap<>();
    Map<Histogram, Tuple2<QueryScopeInfo, String>> histograms = new HashMap<>();
    Map<Meter, Tuple2<QueryScopeInfo, String>> meters = new HashMap<>();
    SimpleCounter c1 = new SimpleCounter();
    SimpleCounter c2 = new SimpleCounter();
    c1.inc(1);
    c2.inc(2);
    counters.put(c1, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.OperatorQueryScopeInfo(jobID.toString(), "taskid", 2, "opname", "abc"), "oc"));
    counters.put(c2, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskQueryScopeInfo(jobID.toString(), "taskid", 2, "abc"), "tc"));
    meters.put(new Meter() {

        @Override
        public void markEvent() {
        }

        @Override
        public void markEvent(long n) {
        }

        @Override
        public double getRate() {
            return 5;
        }

        @Override
        public long getCount() {
            return 10;
        }
    }, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobQueryScopeInfo(jobID.toString(), "abc"), "jc"));
    gauges.put(new Gauge<String>() {

        @Override
        public String getValue() {
            return "x";
        }
    }, new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.TaskManagerQueryScopeInfo(tmID.toString(), "abc"), "gauge"));
    histograms.put(new TestingHistogram(), new Tuple2<QueryScopeInfo, String>(new QueryScopeInfo.JobManagerQueryScopeInfo("abc"), "hist"));
    MetricDumpSerialization.MetricDumpSerializer serializer = new MetricDumpSerialization.MetricDumpSerializer();
    MetricDumpSerialization.MetricSerializationResult dump = serializer.serialize(counters, gauges, histograms, meters);
    serializer.close();
    return dump;
}
Also used : TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram) Histogram(org.apache.flink.metrics.Histogram) HashMap(java.util.HashMap) Meter(org.apache.flink.metrics.Meter) QueryScopeInfo(org.apache.flink.runtime.metrics.dump.QueryScopeInfo) Gauge(org.apache.flink.metrics.Gauge) SimpleCounter(org.apache.flink.metrics.SimpleCounter) Counter(org.apache.flink.metrics.Counter) SimpleCounter(org.apache.flink.metrics.SimpleCounter) MetricDumpSerialization(org.apache.flink.runtime.metrics.dump.MetricDumpSerialization) Tuple2(org.apache.flink.api.java.tuple.Tuple2) TestingHistogram(org.apache.flink.runtime.metrics.util.TestingHistogram)

Example 9 with Histogram

use of org.apache.flink.metrics.Histogram in project flink by apache.

the class MetricQueryService method onReceive.

@Override
public void onReceive(Object message) {
    try {
        if (message instanceof AddMetric) {
            AddMetric added = (AddMetric) message;
            String metricName = added.metricName;
            Metric metric = added.metric;
            AbstractMetricGroup group = added.group;
            QueryScopeInfo info = group.getQueryServiceMetricInfo(FILTER);
            if (metric instanceof Counter) {
                counters.put((Counter) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
            } else if (metric instanceof Gauge) {
                gauges.put((Gauge<?>) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
            } else if (metric instanceof Histogram) {
                histograms.put((Histogram) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
            } else if (metric instanceof Meter) {
                meters.put((Meter) metric, new Tuple2<>(info, FILTER.filterCharacters(metricName)));
            }
        } else if (message instanceof RemoveMetric) {
            Metric metric = (((RemoveMetric) message).metric);
            if (metric instanceof Counter) {
                this.counters.remove(metric);
            } else if (metric instanceof Gauge) {
                this.gauges.remove(metric);
            } else if (metric instanceof Histogram) {
                this.histograms.remove(metric);
            } else if (metric instanceof Meter) {
                this.meters.remove(metric);
            }
        } else if (message instanceof CreateDump) {
            MetricDumpSerialization.MetricSerializationResult dump = serializer.serialize(counters, gauges, histograms, meters);
            getSender().tell(dump, getSelf());
        } else {
            LOG.warn("MetricQueryServiceActor received an invalid message. " + message.toString());
            getSender().tell(new Status.Failure(new IOException("MetricQueryServiceActor received an invalid message. " + message.toString())), getSelf());
        }
    } catch (Exception e) {
        LOG.warn("An exception occurred while processing a message.", e);
    }
}
Also used : Status(akka.actor.Status) Histogram(org.apache.flink.metrics.Histogram) Meter(org.apache.flink.metrics.Meter) IOException(java.io.IOException) IOException(java.io.IOException) Gauge(org.apache.flink.metrics.Gauge) Counter(org.apache.flink.metrics.Counter) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Metric(org.apache.flink.metrics.Metric) AbstractMetricGroup(org.apache.flink.runtime.metrics.groups.AbstractMetricGroup)

Example 10 with Histogram

use of org.apache.flink.metrics.Histogram in project flink by apache.

the class PrometheusReporterTaskScopeTest method histogramsCanBeAddedSeveralTimesIfTheyDifferInLabels.

@Test
void histogramsCanBeAddedSeveralTimesIfTheyDifferInLabels() throws UnirestException {
    Histogram histogram = new TestHistogram();
    taskMetricGroup1.histogram("my_histogram", histogram);
    taskMetricGroup2.histogram("my_histogram", histogram);
    final String exportedMetrics = pollMetrics(reporter.getPort()).getBody();
    assertThat(exportedMetrics).contains(// histogram
    "subtask_index=\"0\",quantile=\"0.5\",} 0.5");
    assertThat(exportedMetrics).contains(// histogram
    "subtask_index=\"1\",quantile=\"0.5\",} 0.5");
    final String[] labelNamesWithQuantile = addToArray(LABEL_NAMES, "quantile");
    for (Double quantile : PrometheusReporter.HistogramSummaryProxy.QUANTILES) {
        assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_histogram", labelNamesWithQuantile, addToArray(labelValues1, "" + quantile))).isEqualTo(quantile);
        assertThat(CollectorRegistry.defaultRegistry.getSampleValue("flink_taskmanager_job_task_my_histogram", labelNamesWithQuantile, addToArray(labelValues2, "" + quantile))).isEqualTo(quantile);
    }
}
Also used : Histogram(org.apache.flink.metrics.Histogram) TestHistogram(org.apache.flink.metrics.util.TestHistogram) TestHistogram(org.apache.flink.metrics.util.TestHistogram) Test(org.junit.jupiter.api.Test)

Aggregations

Histogram (org.apache.flink.metrics.Histogram)19 Counter (org.apache.flink.metrics.Counter)14 Meter (org.apache.flink.metrics.Meter)13 Gauge (org.apache.flink.metrics.Gauge)11 TestHistogram (org.apache.flink.metrics.util.TestHistogram)9 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)6 SimpleCounter (org.apache.flink.metrics.SimpleCounter)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)5 MetricGroup (org.apache.flink.metrics.MetricGroup)4 HashMap (java.util.HashMap)3 HistogramStatistics (org.apache.flink.metrics.HistogramStatistics)3 TestMeter (org.apache.flink.metrics.util.TestMeter)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 ResourceID (org.apache.flink.runtime.clusterframework.types.ResourceID)2 MetricDumpSerialization (org.apache.flink.runtime.metrics.dump.MetricDumpSerialization)2 AbstractMetricGroup (org.apache.flink.runtime.metrics.groups.AbstractMetricGroup)2 TaskManagerMetricGroup (org.apache.flink.runtime.metrics.groups.TaskManagerMetricGroup)2