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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
#include "opentelemetry/sdk/resource/resource.h"
#include "opentelemetry/version.h"
namespace metric_sdk = opentelemetry::sdk::metrics;
namespace nostd = opentelemetry::nostd;
namespace exportermetrics = opentelemetry::exporter::metrics;
namespace
{
using opentelemetry::sdk::instrumentationscope::InstrumentationScope;
using opentelemetry::sdk::resource::Resource;
using opentelemetry::sdk::resource::ResourceAttributes;
struct TestDataPoints
{
Resource resource = Resource::Create(ResourceAttributes{});
nostd::unique_ptr<InstrumentationScope> instrumentation_scope =
InstrumentationScope::Create("library_name", "1.2.0");
/**
* Helper function to create ResourceMetrics
*/
inline metric_sdk::ResourceMetrics CreateSumPointData()
{
metric_sdk::SumPointData sum_point_data{};
sum_point_data.value_ = 10.0;
metric_sdk::SumPointData sum_point_data2{};
sum_point_data2.value_ = 20.0;
metric_sdk::ResourceMetrics data;
data.resource_ = &resource;
metric_sdk::MetricData metric_data{
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, sum_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, sum_point_data2}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
return data;
}
inline metric_sdk::ResourceMetrics CreateHistogramPointData()
{
metric_sdk::HistogramPointData histogram_point_data{};
histogram_point_data.boundaries_ = {10.1, 20.2, 30.2};
histogram_point_data.count_ = 3;
histogram_point_data.counts_ = {200, 300, 400, 500};
histogram_point_data.sum_ = 900.5;
metric_sdk::HistogramPointData histogram_point_data2{};
histogram_point_data2.boundaries_ = {10.0, 20.0, 30.0};
histogram_point_data2.count_ = 3;
histogram_point_data2.counts_ = {200, 300, 400, 500};
histogram_point_data2.sum_ = static_cast<int64_t>(900);
metric_sdk::ResourceMetrics data;
data.resource_ = &resource;
metric_sdk::MetricData metric_data{
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kHistogram,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, histogram_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, histogram_point_data2}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
return data;
}
inline metric_sdk::ResourceMetrics CreateLastValuePointData()
{
metric_sdk::ResourceMetrics data;
data.resource_ = &resource;
metric_sdk::LastValuePointData last_value_point_data{};
last_value_point_data.value_ = 10.0;
last_value_point_data.is_lastvalue_valid_ = true;
last_value_point_data.sample_ts_ = opentelemetry::common::SystemTimestamp{};
metric_sdk::LastValuePointData last_value_point_data2{};
last_value_point_data2.value_ = static_cast<int64_t>(20);
last_value_point_data2.is_lastvalue_valid_ = true;
last_value_point_data2.sample_ts_ = opentelemetry::common::SystemTimestamp{};
metric_sdk::MetricData metric_data{
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, last_value_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, last_value_point_data2}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
return data;
}
inline metric_sdk::ResourceMetrics CreateDropPointData()
{
metric_sdk::ResourceMetrics data;
data.resource_ = &resource;
metric_sdk::DropPointData drop_point_data{};
metric_sdk::DropPointData drop_point_data2{};
metric_sdk::MetricData metric_data{
metric_sdk::InstrumentDescriptor{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble},
metric_sdk::AggregationTemporality::kDelta, opentelemetry::common::SystemTimestamp{},
opentelemetry::common::SystemTimestamp{},
std::vector<metric_sdk::PointDataAttributes>{
{metric_sdk::PointAttributes{{"a1", "b1"}}, drop_point_data},
{metric_sdk::PointAttributes{{"a2", "b2"}}, drop_point_data2}}};
data.scope_metric_data_ = std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope.get(), std::vector<metric_sdk::MetricData>{metric_data}}};
return data;
}
};
} // namespace
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk
{
namespace metrics
{
inline bool operator==(const metric_sdk::MetricData &lhs, const metric_sdk::MetricData &rhs)
{
if (lhs.start_ts != rhs.start_ts)
{
return false;
}
if (lhs.end_ts != rhs.end_ts)
{
return false;
}
if (lhs.instrument_descriptor.description_ != rhs.instrument_descriptor.description_)
{
return false;
}
if (lhs.instrument_descriptor.name_ != rhs.instrument_descriptor.name_)
{
return false;
}
if (lhs.instrument_descriptor.type_ != rhs.instrument_descriptor.type_)
{
return false;
}
if (lhs.instrument_descriptor.unit_ != rhs.instrument_descriptor.unit_)
{
return false;
}
if (lhs.instrument_descriptor.value_type_ != rhs.instrument_descriptor.value_type_)
{
return false;
}
if (lhs.point_data_attr_.size() != rhs.point_data_attr_.size())
{
return false;
}
for (uint32_t idx = 0; idx < lhs.point_data_attr_.size(); ++idx)
{
if (lhs.point_data_attr_.at(idx).attributes != rhs.point_data_attr_.at(idx).attributes)
{
return false;
}
auto &lhs_point_data = lhs.point_data_attr_.at(idx).point_data;
auto &rhs_point_data = rhs.point_data_attr_.at(idx).point_data;
if (nostd::holds_alternative<metric_sdk::SumPointData>(lhs_point_data))
{
if (nostd::get<metric_sdk::SumPointData>(lhs_point_data).value_ !=
nostd::get<metric_sdk::SumPointData>(rhs_point_data).value_)
{
return false;
}
}
else if (nostd::holds_alternative<metric_sdk::DropPointData>(lhs_point_data))
{
if (!nostd::holds_alternative<metric_sdk::DropPointData>(rhs_point_data))
{
return false;
}
}
else if (nostd::holds_alternative<metric_sdk::HistogramPointData>(lhs_point_data))
{
auto &lhs_histogram_d = nostd::get<metric_sdk::HistogramPointData>(lhs_point_data);
auto &rhs_histogram_d = nostd::get<metric_sdk::HistogramPointData>(rhs_point_data);
if (lhs_histogram_d.count_ != rhs_histogram_d.count_)
{
return false;
}
if (lhs_histogram_d.counts_ != rhs_histogram_d.counts_)
{
return false;
}
if (lhs_histogram_d.boundaries_ != rhs_histogram_d.boundaries_)
{
return false;
}
if (lhs_histogram_d.sum_ != rhs_histogram_d.sum_)
{
return false;
}
}
else if (nostd::holds_alternative<metric_sdk::LastValuePointData>(lhs_point_data))
{
auto &lhs_last_value_d = nostd::get<metric_sdk::LastValuePointData>(lhs_point_data);
auto &rhs_last_value_d = nostd::get<metric_sdk::LastValuePointData>(rhs_point_data);
if (lhs_last_value_d.is_lastvalue_valid_ != rhs_last_value_d.is_lastvalue_valid_)
{
return false;
}
if (lhs_last_value_d.sample_ts_ != rhs_last_value_d.sample_ts_)
{
return false;
}
if (lhs_last_value_d.value_ != rhs_last_value_d.value_)
{
return false;
}
}
}
return true;
}
} // namespace metrics
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
|