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
|
#include "prometheus/text_serializer.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <cmath>
#include <limits>
#include <string>
#include "prometheus/client_metric.h"
#include "prometheus/histogram.h"
#include "prometheus/metric_family.h"
#include "prometheus/metric_type.h"
#include "prometheus/summary.h"
namespace prometheus {
namespace {
class TextSerializerTest : public testing::Test {
public:
std::string Serialize(MetricType type) const {
MetricFamily metricFamily;
metricFamily.name = name;
metricFamily.help = "my metric help text";
metricFamily.type = type;
metricFamily.metric = std::vector<ClientMetric>{metric};
std::vector<MetricFamily> families{metricFamily};
return textSerializer.Serialize(families);
}
const std::string name = "my_metric";
ClientMetric metric;
TextSerializer textSerializer;
};
TEST_F(TextSerializerTest, shouldSerializeNotANumber) {
metric.gauge.value = std::nan("");
EXPECT_THAT(Serialize(MetricType::Gauge), testing::HasSubstr(name + " Nan"));
}
TEST_F(TextSerializerTest, shouldSerializeNegativeInfinity) {
metric.gauge.value = -std::numeric_limits<double>::infinity();
EXPECT_THAT(Serialize(MetricType::Gauge), testing::HasSubstr(name + " -Inf"));
}
TEST_F(TextSerializerTest, shouldSerializePositiveInfinity) {
metric.gauge.value = std::numeric_limits<double>::infinity();
EXPECT_THAT(Serialize(MetricType::Gauge), testing::HasSubstr(name + " +Inf"));
}
TEST_F(TextSerializerTest, shouldEscapeBackslash) {
metric.label.resize(1, ClientMetric::Label{"k", "v\\v"});
EXPECT_THAT(Serialize(MetricType::Gauge),
testing::HasSubstr(name + "{k=\"v\\\\v\"}"));
}
TEST_F(TextSerializerTest, shouldEscapeNewline) {
metric.label.resize(1, ClientMetric::Label{"k", "v\nv"});
EXPECT_THAT(Serialize(MetricType::Gauge),
testing::HasSubstr(name + "{k=\"v\\nv\"}"));
}
TEST_F(TextSerializerTest, shouldEscapeDoubleQuote) {
metric.label.resize(1, ClientMetric::Label{"k", "v\"v"});
EXPECT_THAT(Serialize(MetricType::Gauge),
testing::HasSubstr(name + "{k=\"v\\\"v\"}"));
}
TEST_F(TextSerializerTest, shouldSerializeUntyped) {
metric.untyped.value = 64.0;
const auto serialized = Serialize(MetricType::Untyped);
EXPECT_THAT(serialized, testing::HasSubstr(name + " 64\n"));
}
TEST_F(TextSerializerTest, shouldSerializeTimestamp) {
metric.counter.value = 64.0;
metric.timestamp_ms = 1234;
const auto serialized = Serialize(MetricType::Counter);
EXPECT_THAT(serialized, testing::HasSubstr(name + " 64 1234\n"));
}
TEST_F(TextSerializerTest, shouldSerializeHistogramWithNoBuckets) {
metric.histogram.sample_count = 2;
metric.histogram.sample_sum = 32.0;
const auto serialized = Serialize(MetricType::Histogram);
EXPECT_THAT(serialized, testing::HasSubstr(name + "_count 2"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_sum 32\n"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_bucket{le=\"+Inf\"} 2"));
}
TEST_F(TextSerializerTest, shouldSerializeHistogram) {
Histogram histogram{{1}};
histogram.Observe(0);
histogram.Observe(200);
metric = histogram.Collect();
const auto serialized = Serialize(MetricType::Histogram);
EXPECT_THAT(serialized, testing::HasSubstr(name + "_count 2\n"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_sum 200\n"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_bucket{le=\"1\"} 1\n"));
EXPECT_THAT(serialized,
testing::HasSubstr(name + "_bucket{le=\"+Inf\"} 2\n"));
}
TEST_F(TextSerializerTest, shouldSerializeSummary) {
Summary summary{Summary::Quantiles{{0.5, 0.05}}};
summary.Observe(0);
summary.Observe(200);
metric = summary.Collect();
const auto serialized = Serialize(MetricType::Summary);
EXPECT_THAT(serialized, testing::HasSubstr(name + "_count 2"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "_sum 200\n"));
EXPECT_THAT(serialized, testing::HasSubstr(name + "{quantile=\"0.5\"} 0\n"));
}
} // namespace
} // namespace prometheus
|