|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "opencensus/exporters/stats/prometheus/internal/prometheus_utils.h" |
| 16 | + |
| 17 | +#include <cctype> |
| 18 | +#include <cstdint> |
| 19 | +#include <string> |
| 20 | +#include <utility> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#include "absl/strings/str_cat.h" |
| 24 | +#include "absl/strings/string_view.h" |
| 25 | +#include "absl/time/time.h" |
| 26 | +#include "metrics.pb.h" |
| 27 | +#include "opencensus/stats/stats.h" |
| 28 | + |
| 29 | +namespace opencensus { |
| 30 | +namespace exporters { |
| 31 | +namespace stats { |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +// Replaces non-alphanumeric characters with underscores to satisfy |
| 36 | +// Prometheus's name requirements. |
| 37 | +std::string SanitizeName(absl::string_view name) { |
| 38 | + std::string sanitized(name); |
| 39 | + std::replace_if(sanitized.begin(), sanitized.end(), |
| 40 | + [](char c) { return !::isalnum(c); }, '_'); |
| 41 | + return sanitized; |
| 42 | +} |
| 43 | + |
| 44 | +io::prometheus::client::MetricType MetricType( |
| 45 | + opencensus::stats::Aggregation::Type type) { |
| 46 | + switch (type) { |
| 47 | + case opencensus::stats::Aggregation::Type::kCount: |
| 48 | + return io::prometheus::client::MetricType::COUNTER; |
| 49 | + case opencensus::stats::Aggregation::Type::kSum: |
| 50 | + return io::prometheus::client::MetricType::UNTYPED; |
| 51 | + case opencensus::stats::Aggregation::Type::kDistribution: |
| 52 | + return io::prometheus::client::MetricType::HISTOGRAM; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void SetValue(double value, io::prometheus::client::Metric* metric) { |
| 57 | + metric->mutable_untyped()->set_value(value); |
| 58 | +} |
| 59 | +void SetValue(int64_t value, io::prometheus::client::Metric* metric) { |
| 60 | + metric->mutable_counter()->set_value(value); |
| 61 | +} |
| 62 | +void SetValue(const opencensus::stats::Distribution& value, |
| 63 | + io::prometheus::client::Metric* metric) { |
| 64 | + auto* histogram = metric->mutable_histogram(); |
| 65 | + histogram->set_sample_count(value.count()); |
| 66 | + histogram->set_sample_sum(value.count() * value.mean()); |
| 67 | + |
| 68 | + int64_t cumulative_count = 0; |
| 69 | + for (int i = 0; i < value.bucket_boundaries().num_buckets(); ++i) { |
| 70 | + cumulative_count += value.bucket_counts()[i]; |
| 71 | + auto* bucket = histogram->add_bucket(); |
| 72 | + bucket->set_cumulative_count(cumulative_count); |
| 73 | + // We use lower boundaries plus an underflow bucket; Prometheus uses upper |
| 74 | + // boundaries, including a +Inf boundary. |
| 75 | + bucket->set_upper_bound( |
| 76 | + i < value.bucket_boundaries().lower_boundaries().size() |
| 77 | + ? value.bucket_boundaries().lower_boundaries()[i] |
| 78 | + : std::numeric_limits<double>::infinity()); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +template <typename T> |
| 83 | +void SetData(const opencensus::stats::ViewDescriptor& descriptor, |
| 84 | + const opencensus::stats::ViewData::DataMap<T>& data, int64_t time, |
| 85 | + io::prometheus::client::MetricFamily* metric_family) { |
| 86 | + for (const auto& row : data) { |
| 87 | + auto* metric = metric_family->add_metric(); |
| 88 | + metric->set_timestamp_ms(time); |
| 89 | + for (int i = 0; i < descriptor.num_columns(); ++i) { |
| 90 | + auto* label = metric->add_label(); |
| 91 | + label->set_name(SanitizeName(descriptor.columns()[i])); |
| 92 | + label->set_value(row.first[i]); |
| 93 | + } |
| 94 | + SetValue(row.second, metric); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace |
| 99 | + |
| 100 | +void SetMetricFamily(const opencensus::stats::ViewDescriptor& descriptor, |
| 101 | + const opencensus::stats::ViewData& data, |
| 102 | + io::prometheus::client::MetricFamily* metric_family) { |
| 103 | + // TODO(sturdy): convert common units into base units (e.g. ms->s). |
| 104 | + metric_family->set_name(SanitizeName(absl::StrCat( |
| 105 | + descriptor.name(), "_", descriptor.measure_descriptor().units()))); |
| 106 | + metric_family->set_help(descriptor.description()); |
| 107 | + metric_family->set_type(MetricType(descriptor.aggregation().type())); |
| 108 | + |
| 109 | + const int64_t time = absl::ToUnixMillis(data.end_time()); |
| 110 | + switch (data.type()) { |
| 111 | + case opencensus::stats::ViewData::Type::kDouble: { |
| 112 | + SetData(descriptor, data.double_data(), time, metric_family); |
| 113 | + break; |
| 114 | + } |
| 115 | + case opencensus::stats::ViewData::Type::kInt64: { |
| 116 | + SetData(descriptor, data.int_data(), time, metric_family); |
| 117 | + break; |
| 118 | + } |
| 119 | + case opencensus::stats::ViewData::Type::kDistribution: { |
| 120 | + SetData(descriptor, data.distribution_data(), time, metric_family); |
| 121 | + break; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +} // namespace stats |
| 127 | +} // namespace exporters |
| 128 | +} // namespace opencensus |
0 commit comments