aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Processors/Formats/Impl/PrometheusTextOutputFormat.h
blob: de87237c6637d62936de4af55e58b52548040f68 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once

#include <string>
#include <unordered_map>

#include <Core/Block.h>
#include <Formats/FormatSettings.h>
#include <Processors/Formats/IRowOutputFormat.h>
#include <DataTypes/Serializations/ISerialization.h>


namespace DB
{

class WriteBuffer;

class PrometheusTextOutputFormat : public IRowOutputFormat
{
public:
    PrometheusTextOutputFormat(
        WriteBuffer & out_,
        const Block & header_,
        const FormatSettings & format_settings_);

    String getName() const override { return "PrometheusTextOutputFormat"; }

    /// https://github.com/prometheus/docs/blob/86386ed25bc8a5309492483ec7d18d0914043162/content/docs/instrumenting/exposition_formats.md
    String getContentType() const override { return "text/plain; version=0.0.4; charset=UTF-8"; }

protected:

    struct ColumnPositions
    {
        size_t name;
        size_t value;
        std::optional<size_t> help;
        std::optional<size_t> type;
        std::optional<size_t> labels;
        std::optional<size_t> timestamp;
    };

    /// One metric can be represented by multiple rows (e.g. containing different labels).
    struct CurrentMetric
    {
        struct RowValue
        {
            std::map<String, String> labels;
            String value;
            String timestamp;
        };

        CurrentMetric() = default;
        explicit CurrentMetric(const String & name_) : name(name_) {}

        String name;
        String help;
        String type;
        std::vector<RowValue> values;
    };

    /// Input rows should be grouped by the same metric.
    void write(const Columns & columns, size_t row_num) override;
    void writeField(const IColumn &, const ISerialization &, size_t) override {}
    void finalizeImpl() override;

    void flushCurrentMetric();
    String getString(const Columns & columns, size_t row_num, size_t column_pos);
    String getString(const IColumn & column, size_t row_num, SerializationPtr serialization);

    static void fixupBucketLabels(CurrentMetric & metric);

    ColumnPositions pos;
    CurrentMetric current_metric;
    SerializationPtr string_serialization;
    const FormatSettings format_settings;
};

}