<commit_before>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2019-2021 The Fluent Bit Authors
 *  Copyright (C) 2015-2018 Treasure Data Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_input_plugin.h>

#include "ne.h"
#include "ne_utils.h"

#include <unistd.h>

/*
 * Diskstats interface references
 * ------------------------------
 * https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats
 * https://www.kernel.org/doc/Documentation/iostats.txt
 *
 * From the documentation, Kernel versions and expected fields:
 *
 *   ==  ===================================
 *    1  major number
 *    2  minor mumber
 *    3  device name
 *    4  reads completed successfully
 *    5  reads merged
 *    6  sectors read
 *    7  time spent reading (ms)
 *    8  writes completed
 *    9  writes merged
 *   10  sectors written
 *   11  time spent writing (ms)
 *   12  I/Os currently in progress
 *   13  time spent doing I/Os (ms)
 *   14  weighted time spent doing I/Os (ms)
 *   ==  ===================================
 *
 *   Kernel 4.18+ appends four more fields for discard
 *   tracking putting the total at 18:
 *
 *   ==  ===================================
 *   15  discards completed successfully
 *   16  discards merged
 *   17  sectors discarded
 *   18  time spent discarding
 *   ==  ===================================
 *
 *   Kernel 5.5+ appends two more fields for flush requests:
 *
 *   ==  =====================================
 *   19  flush requests completed successfully
 *   20  time spent flushing
 *   ==  =====================================
 */

#define KNOWN_FIELDS     17
#define SECTOR_SIZE      512
#define IGNORED_DEVICES  "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"

struct dt_metric {
    void *metric;
    double factor;
};

static void metric_cache_set(struct flb_ne *ctx, void *metric, double factor, int *offset)
{
    int id;
    struct dt_metric *m;
    struct dt_metric **cache;

    id = *offset;

    cache = (struct dt_metric **) ctx->dt_metrics;
    m = (struct dt_metric *) &cache[id];
    m->metric = metric;
    m->factor = factor;
    (*offset)++;
}

static void metric_cache_update(struct flb_ne *ctx, int id, flb_sds_t device,
                                flb_sds_t str_val)
{
    int ret = -1;
    uint64_t ts;
    double val;
    struct dt_metric *m;
    struct dt_metric **cache;
    struct cmt_gauge *g;
    struct cmt_counter *c;

    cache = (struct dt_metric **) ctx->dt_metrics;
    m = (struct dt_metric *) &cache[id];

    ret = ne_utils_str_to_double(str_val, &val);
    if (ret == -1) {
        flb_plg_error(ctx->ins,
                      "could not represent string value '%s' for metric id '%i', "
                      "device '%s'",
                      str_val, id, device);
        return;
    }

    ts = cmt_time_now();

    if (m->factor != 0) {
        val *= m->factor;
    }

    if (id == 8) {
        g = (struct cmt_gauge *) m->metric;
        ret = cmt_gauge_set(g, ts, val, 1, (char *[]) {device});<commit_msg>in_node_exporter_metrics: Ensure zero with epsilon on double type factor<commit_after>/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2019-2021 The Fluent Bit Authors
 *  Copyright (C) 2015-2018 Treasure Data Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_sds.h>
#include <fluent-bit/flb_input_plugin.h>

#include "ne.h"
#include "ne_utils.h"

#include <unistd.h>
#include <float.h>

/*
 * Diskstats interface references
 * ------------------------------
 * https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats
 * https://www.kernel.org/doc/Documentation/iostats.txt
 *
 * From the documentation, Kernel versions and expected fields:
 *
 *   ==  ===================================
 *    1  major number
 *    2  minor mumber
 *    3  device name
 *    4  reads completed successfully
 *    5  reads merged
 *    6  sectors read
 *    7  time spent reading (ms)
 *    8  writes completed
 *    9  writes merged
 *   10  sectors written
 *   11  time spent writing (ms)
 *   12  I/Os currently in progress
 *   13  time spent doing I/Os (ms)
 *   14  weighted time spent doing I/Os (ms)
 *   ==  ===================================
 *
 *   Kernel 4.18+ appends four more fields for discard
 *   tracking putting the total at 18:
 *
 *   ==  ===================================
 *   15  discards completed successfully
 *   16  discards merged
 *   17  sectors discarded
 *   18  time spent discarding
 *   ==  ===================================
 *
 *   Kernel 5.5+ appends two more fields for flush requests:
 *
 *   ==  =====================================
 *   19  flush requests completed successfully
 *   20  time spent flushing
 *   ==  =====================================
 */

#define KNOWN_FIELDS     17
#define SECTOR_SIZE      512
#define IGNORED_DEVICES  "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"

struct dt_metric {
    void *metric;
    double factor;
};

static void metric_cache_set(struct flb_ne *ctx, void *metric, double factor, int *offset)
{
    int id;
    struct dt_metric *m;
    struct dt_metric **cache;

    id = *offset;

    cache = (struct dt_metric **) ctx->dt_metrics;
    m = (struct dt_metric *) &cache[id];
    m->metric = metric;
    m->factor = factor;
    (*offset)++;
}

static void metric_cache_update(struct flb_ne *ctx, int id, flb_sds_t device,
                                flb_sds_t str_val)
{
    int ret = -1;
    uint64_t ts;
    double val;
    struct dt_metric *m;
    struct dt_metric **cache;
    struct cmt_gauge *g;
    struct cmt_counter *c;

    cache = (struct dt_metric **) ctx->dt_metrics;
    m = (struct dt_metric *) &cache[id];

    ret = ne_utils_str_to_double(str_val, &val);
    if (ret == -1) {
        flb_plg_error(ctx->ins,
                      "could not represent string value '%s' for metric id '%i', "
                      "device '%s'",
                      str_val, id, device);
        return;
    }

    ts = cmt_time_now();

    if (m->factor > DBL_EPSILON) {
        val *= m->factor;
    }

    if (id == 8) {
        g = (struct cmt_gauge *) m->metric;
        ret = cmt_gauge_set(g, ts, val, 1, (char *[]) {device});
{'fraction_non_alphanumeric': 0.17538256819693945, 'fraction_numerical': 0.019028609447771124, 'mean_word_length': 2.6663414634146343, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 29, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1558656', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2376, 'n_words': 729}