summaryrefslogtreecommitdiffstats
path: root/container-disc/src/main/java/com/yahoo/container/jdisc/metric/JrtMetrics.java
blob: 01709844f6abd2dfb141a2bf34a13841b53dcf39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2019 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.metric;

import com.yahoo.jdisc.Metric;
import com.yahoo.jrt.TransportMetrics;

import java.util.function.ToLongFunction;

import static com.yahoo.jrt.TransportMetrics.*;

/**
 * Emits jrt metrics
 *
 * @author bjorncs
 */
class JrtMetrics {

    private final TransportMetrics transportMetrics = TransportMetrics.getInstance();
    private final Metric metric;
    private volatile Snapshot previousSnapshot;

    JrtMetrics(Metric metric) {
        this.metric = metric;
    }

    void emitMetrics() {
        Snapshot snapshot = transportMetrics.snapshot();
        increment("jrt.transport.tls-certificate-verification-failures", Snapshot::tlsCertificateVerificationFailures, snapshot);
        increment("jrt.transport.peer-authorization-failures", Snapshot::peerAuthorizationFailures, snapshot);
        increment("jrt.transport.server.tls-connections-established", Snapshot::serverTlsConnectionsEstablished, snapshot);
        increment("jrt.transport.client.tls-connections-established", Snapshot::clientTlsConnectionsEstablished, snapshot);
        increment("jrt.transport.server.unencrypted-connections-established", Snapshot::serverUnencryptedConnectionsEstablished, snapshot);
        increment("jrt.transport.client.unencrypted-connections-established", Snapshot::clientUnencryptedConnectionsEstablished, snapshot);
        previousSnapshot = snapshot;
    }

    private void increment(String metricName, ToLongFunction<Snapshot> metricGetter, Snapshot snapshot) {
        long currentValue = metricGetter.applyAsLong(snapshot);
        long increment = previousSnapshot != null
                ? currentValue - metricGetter.applyAsLong(previousSnapshot)
                : currentValue;
        metric.add(metricName, increment, null);
    }
}