aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/container/handler/threadpool/ThreadPoolMetric.java
blob: 24bc7ebc2d02acb5f3485530e0113f2374b751f3 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.handler.threadpool;

import ai.vespa.metrics.ContainerMetrics;
import com.yahoo.jdisc.Metric;

import java.util.Map;

/**
 * @author bjorncs
 */
class ThreadPoolMetric {

    private static final String THREAD_POOL_NAME_DIMENSION = "threadpool";

    private final Metric metric;
    private final Metric.Context defaultContext;
    private final String threadPoolName;

    ThreadPoolMetric(Metric metric, String threadPoolName) {
        this.metric = metric;
        this.threadPoolName = threadPoolName;
        this.defaultContext = metric.createContext(Map.of(THREAD_POOL_NAME_DIMENSION, threadPoolName));
    }

    void reportRejectRequest() {
        metric.add(ContainerMetrics.SERVER_REJECTED_REQUESTS.baseName(), 1L, defaultContext);
        metric.add(ContainerMetrics.JDISC_THREAD_POOL_REJECTED_TASKS.baseName(), 1L, defaultContext);
    }

    void reportThreadPoolSize(long size) {
        metric.set(ContainerMetrics.SERVER_THREAD_POOL_SIZE.baseName(), size, defaultContext);
        metric.set(ContainerMetrics.JDISC_THREAD_POOL_SIZE.baseName(), size, defaultContext);
    }

    void reportMaxAllowedThreadPoolSize(long size) {
        metric.set(ContainerMetrics.JDISC_THREAD_POOL_MAX_ALLOWED_SIZE.baseName(), size, defaultContext);
    }

    void reportActiveThreads(long threads) {
        metric.set(ContainerMetrics.SERVER_ACTIVE_THREADS.baseName(), threads, defaultContext);
        metric.set(ContainerMetrics.JDISC_THREAD_POOL_ACTIVE_THREADS.baseName(), threads, defaultContext);
    }

    void reportWorkQueueCapacity(long capacity) {
        metric.set(ContainerMetrics.JDISC_THREAD_POOL_WORK_QUEUE_CAPACITY.baseName(), capacity, defaultContext);
    }

    void reportWorkQueueSize(long size) {
        metric.set(ContainerMetrics.JDISC_THREAD_POOL_WORK_QUEUE_SIZE.baseName(), size, defaultContext);
    }

    void reportUnhandledException(Throwable t) {
        Metric.Context ctx = metric.createContext(Map.of(
                THREAD_POOL_NAME_DIMENSION, threadPoolName,
                "exception", t.getClass().getSimpleName()));
        metric.set(ContainerMetrics.JDISC_THREAD_POOL_UNHANDLED_EXCEPTIONS.baseName(), 1L, ctx);
    }

}