Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IcySteam committed Nov 25, 2024
1 parent 1775a3e commit b17d474
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 23 deletions.
5 changes: 0 additions & 5 deletions google/cloud/opentelemetry/internal/monitored_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ struct AsStringVisitor {
std::string operator()(bool const& v) const { return v ? "true" : "false"; }
};

struct OTelKeyMatch {
std::vector<std::string> otel_keys;
absl::optional<std::string> fallback = absl::nullopt;
};

class MonitoredResourceProvider {
public:
MonitoredResourceProvider(
Expand Down
7 changes: 7 additions & 0 deletions google/cloud/opentelemetry/internal/monitored_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,23 @@
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_OPENTELEMETRY_INTERNAL_MONITORED_RESOURCE_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_OPENTELEMETRY_INTERNAL_MONITORED_RESOURCE_H

#include "absl/types/optional.h"
#include "google/cloud/version.h"
#include <opentelemetry/sdk/resource/resource.h>
#include <string>
#include <unordered_map>
#include <vector>

namespace google {
namespace cloud {
namespace otel_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN

struct OTelKeyMatch {
std::vector<std::string> otel_keys;
absl::optional<std::string> fallback = absl::nullopt;
};

std::string AsString(
opentelemetry::sdk::common::OwnedAttributeValue const& attribute);

Expand Down
20 changes: 3 additions & 17 deletions google/cloud/opentelemetry/internal/time_series.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "absl/types/optional.h"
#include "google/cloud/opentelemetry/internal/time_series.h"
#include "google/cloud/opentelemetry/internal/monitored_resource.h"
#include "google/cloud/internal/absl_str_replace_quiet.h"
Expand All @@ -21,10 +20,8 @@
#include <opentelemetry/common/attribute_value.h>
#include <opentelemetry/sdk/metrics/data/metric_data.h>
#include <opentelemetry/sdk/metrics/export/metric_producer.h>
#include <opentelemetry/sdk/resource/semantic_conventions.h>
#include <cctype>
#include <string>
#include <unordered_map>
#include <vector>

namespace google {
Expand All @@ -33,18 +30,6 @@ namespace otel_internal {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

namespace sc = opentelemetry::sdk::resource::SemanticConventions;

struct OTelKeyMatch {
std::vector<std::string> otel_keys;
absl::optional<std::string> fallback = absl::nullopt;
};

std::unordered_map<std::string, OTelKeyMatch> const kExtraLabelsLookup = {
{"service_name", {{sc::kServiceName}}},
{"service_namespace", {{sc::kServiceNamespace}}},
{"service_instance_id", {{sc::kServiceInstanceId}}}};

google::protobuf::Timestamp ToProtoTimestamp(
opentelemetry::common::SystemTimestamp ts) {
return internal::ToProtoTimestamp(
Expand Down Expand Up @@ -255,14 +240,15 @@ std::vector<google::monitoring::v3::CreateTimeSeriesRequest> ToRequests(

std::vector<google::monitoring::v3::TimeSeries> WithExtraLabels(
opentelemetry::sdk::metrics::ResourceMetrics const& data,
std::vector<google::monitoring::v3::TimeSeries>& tss) {
std::vector<google::monitoring::v3::TimeSeries>& tss,
std::unordered_map<std::string, OTelKeyMatch> const& extra_labels) {
if (!data.resource_) {
return tss;
}

opentelemetry::sdk::resource::ResourceAttributes const& attributes =
data.resource_->GetAttributes();
for (auto const& kv : kExtraLabelsLookup) {
for (auto const& kv : extra_labels) {
auto const& oks = kv.second.otel_keys;
auto found = std::find_first_of(
oks.begin(), oks.end(), attributes.begin(), attributes.end(),
Expand Down
16 changes: 15 additions & 1 deletion google/cloud/opentelemetry/internal/time_series.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_OPENTELEMETRY_INTERNAL_TIME_SERIES_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_OPENTELEMETRY_INTERNAL_TIME_SERIES_H

#include "google/cloud/opentelemetry/internal/monitored_resource.h"
#include "google/cloud/version.h"
#include "absl/types/optional.h"
#include <google/api/metric.pb.h>
#include <google/api/monitored_resource.pb.h>
#include <google/monitoring/v3/metric_service.pb.h>
#include <opentelemetry/sdk/metrics/metric_reader.h>
#include <opentelemetry/sdk/resource/resource.h>
#include <opentelemetry/sdk/resource/semantic_conventions.h>
#include <functional>
#include <string>
#include <unordered_map>

namespace google {
namespace cloud {
Expand All @@ -35,6 +38,15 @@ GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
// See: https://cloud.google.com/monitoring/quotas
auto constexpr kMaxTimeSeriesPerRequest = 200;

std::unordered_map<std::string, OTelKeyMatch> const kExtraLabelsLookup = {
{"service_name",
{{opentelemetry::sdk::resource::SemanticConventions::kServiceName}}},
{"service_namespace",
{{opentelemetry::sdk::resource::SemanticConventions::kServiceNamespace}}},
{"service_instance_id",
{{opentelemetry::sdk::resource::SemanticConventions::
kServiceInstanceId}}}};

google::api::Metric ToMetric(
opentelemetry::sdk::metrics::MetricData const& metric_data,
opentelemetry::sdk::metrics::PointAttributes const& attributes,
Expand Down Expand Up @@ -102,7 +114,9 @@ std::vector<google::monitoring::v3::CreateTimeSeriesRequest> ToRequests(
*/
std::vector<google::monitoring::v3::TimeSeries> WithExtraLabels(
opentelemetry::sdk::metrics::ResourceMetrics const& data,
std::vector<google::monitoring::v3::TimeSeries>& tss);
std::vector<google::monitoring::v3::TimeSeries>& tss,
std::unordered_map<std::string, OTelKeyMatch> const& extra_labels =
kExtraLabelsLookup);

GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace otel_internal
Expand Down
90 changes: 90 additions & 0 deletions google/cloud/opentelemetry/internal/time_series_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
#include "google/cloud/testing_util/scoped_log.h"
#include <google/protobuf/text_format.h>
#include <gmock/gmock.h>
#include <opentelemetry/sdk/common/attribute_utils.h>
#include <opentelemetry/sdk/metrics/export/metric_producer.h>
#include <opentelemetry/sdk/resource/resource.h>
#include <opentelemetry/sdk/resource/semantic_conventions.h>
#include <algorithm>
#include <cstdint>
#include <string>

namespace google {
namespace cloud {
Expand Down Expand Up @@ -155,6 +157,18 @@ auto IsTestResource() {
Pair("instance_id", "1020304050607080900"))));
}

auto ResourceWithAllExtraLabels() {
opentelemetry::sdk::common::AttributeMap labels;
for (auto const& kv : kExtraLabelsLookup) {
auto const& oks = kv.second.otel_keys;
if (oks.empty()) {
continue;
}
labels.SetAttribute(oks[0], kv.first);
}
return opentelemetry::sdk::resource::Resource::Create(labels);
}

auto MetricType(std::string const& type) {
return ResultOf(
"metric.type",
Expand Down Expand Up @@ -697,6 +711,82 @@ TEST(ToRequests, Batches) {
}
}

TEST(WithExtraLabels, CopyLabels) {
opentelemetry::sdk::metrics::PointDataAttributes pda;
pda.point_data = opentelemetry::sdk::metrics::SumPointData{};

opentelemetry::sdk::metrics::MetricData md;
md.point_data_attr_.push_back(std::move(pda));
md.instrument_descriptor.name_ = "metric-name";
md.instrument_descriptor.unit_ = "unit";
md.instrument_descriptor.type_ = {};
md.instrument_descriptor.value_type_ = {};

opentelemetry::sdk::metrics::ScopeMetrics sm;
sm.metric_data_.push_back(md);
sm.metric_data_.push_back(std::move(md));

opentelemetry::sdk::metrics::ResourceMetrics rm;
auto resource = ResourceWithAllExtraLabels();
rm.resource_ = &resource;
rm.scope_metric_data_.push_back(std::move(sm));

auto tss = ToTimeSeries(rm, PrefixWithWorkload);
for (auto const& kv : kExtraLabelsLookup) {
auto const& oks = kv.second.otel_keys;
if (oks.empty()) {
continue;
}
for (auto const& ts : tss) {
auto const& labels = ts.metric().labels();
ASSERT_TRUE(labels.find(kv.first) != labels.end());
EXPECT_EQ(labels.at(kv.first), kv.first);
}
}
}

TEST(WithExtraLabels, NoOverwrites) {
opentelemetry::sdk::metrics::PointDataAttributes pda;
opentelemetry::sdk::common::OrderedAttributeMap existing_labels;
existing_labels.SetAttribute("service_name", "do_not_overwrite");
pda.attributes = existing_labels;
pda.point_data = opentelemetry::sdk::metrics::SumPointData{};

opentelemetry::sdk::metrics::MetricData md;
md.point_data_attr_.push_back(std::move(pda));
md.instrument_descriptor.name_ = "metric-name";
md.instrument_descriptor.unit_ = "unit";
md.instrument_descriptor.type_ = {};
md.instrument_descriptor.value_type_ = {};

opentelemetry::sdk::metrics::ScopeMetrics sm;
sm.metric_data_.push_back(md);
sm.metric_data_.push_back(std::move(md));

opentelemetry::sdk::metrics::ResourceMetrics rm;
auto resource = ResourceWithAllExtraLabels();
rm.resource_ = &resource;
rm.scope_metric_data_.push_back(std::move(sm));

auto tss = ToTimeSeries(rm, PrefixWithWorkload);
for (auto const& kv : kExtraLabelsLookup) {
auto const& oks = kv.second.otel_keys;
if (oks.empty()) {
continue;
}
for (auto const& ts : tss) {
auto const& labels = ts.metric().labels();
ASSERT_TRUE(labels.find(kv.first) != labels.end());
if (existing_labels.GetAttributes().find(kv.first) ==
existing_labels.GetAttributes().end()) {
EXPECT_EQ(labels.at(kv.first), kv.first);
} else {
EXPECT_EQ(labels.at(kv.first), "do_not_overwrite");
}
}
}
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace otel_internal
Expand Down

0 comments on commit b17d474

Please sign in to comment.