summaryrefslogtreecommitdiffstats
path: root/exporting/send_internal_metrics.c
blob: 515cda3b29e3863adbadf54235672a46da17e15f (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-License-Identifier: GPL-3.0-or-later

#include "exporting_engine.h"

/**
 * Create a chart for the main exporting thread CPU usage
 *
 * @param st_rusage the thead CPU usage chart
 * @param rd_user a dimension for user CPU usage
 * @param rd_system a dimension for system CPU usage
 */
void create_main_rusage_chart(RRDSET **st_rusage, RRDDIM **rd_user, RRDDIM **rd_system)
{
    if (!global_statistics_enabled)
        return;
        
    if (*st_rusage && *rd_user && *rd_system)
        return;

    *st_rusage = rrdset_create_localhost(
        "netdata", "exporting_main_thread_cpu", NULL, "exporting", "exporting_cpu_usage", "Netdata Main Exporting Thread CPU Usage",
        "milliseconds/s", "exporting", NULL, 130600, localhost->rrd_update_every, RRDSET_TYPE_STACKED);

    *rd_user = rrddim_add(*st_rusage, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
    *rd_system = rrddim_add(*st_rusage, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
}

/**
 * Send the main exporting thread CPU usage
 *
 * @param st_rusage a thead CPU usage chart
 * @param rd_user a dimension for user CPU usage
 * @param rd_system a dimension for system CPU usage
 */
void send_main_rusage(RRDSET *st_rusage, RRDDIM *rd_user, RRDDIM *rd_system)
{
    if (!global_statistics_enabled)
        return;

    struct rusage thread;
    getrusage(RUSAGE_THREAD, &thread);

    rrddim_set_by_pointer(st_rusage, rd_user,   thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
    rrddim_set_by_pointer(st_rusage, rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);

    rrdset_done(st_rusage);
}

/**
 * Send internal metrics for an instance
 *
 * Send performance metrics for the operation of exporting engine itself to the Netdata database.
 *
 * @param instance an instance data structure.
 */
void send_internal_metrics(struct instance *instance)
{
    if (!global_statistics_enabled)
        return;

    struct stats *stats = &instance->stats;

    // ------------------------------------------------------------------------
    // create charts for monitoring the exporting operations

    if (!stats->initialized) {
        char id[RRD_ID_LENGTH_MAX + 1];
        BUFFER *family = buffer_create(0);

        buffer_sprintf(family, "exporting_%s", instance->config.name);

        snprintf(id, RRD_ID_LENGTH_MAX, "exporting_%s_metrics", instance->config.name);
        netdata_fix_chart_id(id);

        stats->st_metrics = rrdset_create_localhost(
            "netdata", id, NULL, buffer_tostring(family), "exporting_buffer", "Netdata Buffered Metrics", "metrics", "exporting", NULL,
            130610, instance->config.update_every, RRDSET_TYPE_LINE);

        stats->rd_buffered_metrics = rrddim_add(stats->st_metrics, "buffered", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_lost_metrics     = rrddim_add(stats->st_metrics, "lost", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_sent_metrics     = rrddim_add(stats->st_metrics, "sent", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);

        // ------------------------------------------------------------------------

        snprintf(id, RRD_ID_LENGTH_MAX, "exporting_%s_bytes", instance->config.name);
        netdata_fix_chart_id(id);

        stats->st_bytes = rrdset_create_localhost(
            "netdata", id, NULL, buffer_tostring(family), "exporting_data_size", "Netdata Exporting Data Size", "KiB", "exporting", NULL,
            130620, instance->config.update_every, RRDSET_TYPE_AREA);

        stats->rd_buffered_bytes = rrddim_add(stats->st_bytes, "buffered", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_lost_bytes     = rrddim_add(stats->st_bytes, "lost", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_sent_bytes     = rrddim_add(stats->st_bytes, "sent", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_received_bytes = rrddim_add(stats->st_bytes, "received", NULL, 1, 1024, RRD_ALGORITHM_ABSOLUTE);

        // ------------------------------------------------------------------------

        snprintf(id, RRD_ID_LENGTH_MAX, "exporting_%s_ops", instance->config.name);
        netdata_fix_chart_id(id);

        stats->st_ops = rrdset_create_localhost(
            "netdata", id, NULL, buffer_tostring(family), "exporting_operations", "Netdata Exporting Operations", "operations", "exporting",
            NULL, 130630, instance->config.update_every, RRDSET_TYPE_LINE);

        stats->rd_transmission_successes = rrddim_add(stats->st_ops, "write", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_data_lost_events       = rrddim_add(stats->st_ops, "discard", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_reconnects             = rrddim_add(stats->st_ops, "reconnect", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_transmission_failures  = rrddim_add(stats->st_ops, "failure", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
        stats->rd_receptions             = rrddim_add(stats->st_ops, "read", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);

        // ------------------------------------------------------------------------

        snprintf(id, RRD_ID_LENGTH_MAX, "exporting_%s_thread_cpu", instance->config.name);
        netdata_fix_chart_id(id);

        stats->st_rusage = rrdset_create_localhost(
            "netdata", id, NULL, buffer_tostring(family), "exporting_instance", "Netdata Exporting Instance Thread CPU Usage",
            "milliseconds/s", "exporting", NULL, 130640, instance->config.update_every, RRDSET_TYPE_STACKED);

        stats->rd_user   = rrddim_add(stats->st_rusage, "user", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);
        stats->rd_system = rrddim_add(stats->st_rusage, "system", NULL, 1, 1000, RRD_ALGORITHM_INCREMENTAL);

        buffer_free(family);

        stats->initialized = 1;
    }

    // ------------------------------------------------------------------------
    // update the monitoring charts

    rrddim_set_by_pointer(stats->st_metrics, stats->rd_buffered_metrics, stats->buffered_metrics);
    rrddim_set_by_pointer(stats->st_metrics, stats->rd_lost_metrics,     stats->lost_metrics);
    rrddim_set_by_pointer(stats->st_metrics, stats->rd_sent_metrics,     stats->sent_metrics);
    rrdset_done(stats->st_metrics);

    rrddim_set_by_pointer(stats->st_bytes, stats->rd_buffered_bytes, stats->buffered_bytes);
    rrddim_set_by_pointer(stats->st_bytes, stats->rd_lost_bytes,     stats->lost_bytes);
    rrddim_set_by_pointer(stats->st_bytes, stats->rd_sent_bytes,     stats->sent_bytes);
    rrddim_set_by_pointer(stats->st_bytes, stats->rd_received_bytes, stats->received_bytes);
    rrdset_done(stats->st_bytes);

    rrddim_set_by_pointer(stats->st_ops, stats->rd_transmission_successes, stats->transmission_successes);
    rrddim_set_by_pointer(stats->st_ops, stats->rd_data_lost_events,       stats->data_lost_events);
    rrddim_set_by_pointer(stats->st_ops, stats->rd_reconnects,             stats->reconnects);
    rrddim_set_by_pointer(stats->st_ops, stats->rd_transmission_failures,  stats->transmission_failures);
    rrddim_set_by_pointer(stats->st_ops, stats->rd_receptions,             stats->receptions);
    rrdset_done(stats->st_ops);

    struct rusage thread;
    getrusage(RUSAGE_THREAD, &thread);

    rrddim_set_by_pointer(stats->st_rusage, stats->rd_user,   thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
    rrddim_set_by_pointer(stats->st_rusage, stats->rd_system, thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
    rrdset_done(stats->st_rusage);
}