Skip to content

Commit 668676d

Browse files
committed
resolving comments
1 parent ea2086f commit 668676d

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

google/cloud/opentelemetry/internal/monitoring_exporter.cc

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ std::string FormatProjectFullName(std::string const& project) {
3131
return absl::StrCat("projects/", project);
3232
}
3333

34-
otel_internal::ResourceFilterDataFn MakeFilter(Options const& options) {
34+
otel_internal::ResourceFilterDataFn MakeResourceFilterFn(
35+
Options const& options) {
3536
if (!options.has<otel_internal::ResourceFilterDataFnOption>()) {
3637
return nullptr;
3738
}
@@ -47,6 +48,24 @@ otel_internal::ResourceFilterDataFn MakeFilter(Options const& options) {
4748
};
4849
}
4950

51+
otel_internal::MonitoredResourceFromDataFn MakeDynamicResourceFn(
52+
Options const& options, absl::optional<Project> const& project,
53+
absl::optional<google::api::MonitoredResource> const& mr_proto) {
54+
if (!options.has<otel_internal::ResourceFilterDataFnOption>()) {
55+
return nullptr;
56+
}
57+
58+
// `resource_filter_fn_` and `dynamic_resource_fn_` are meant to be used as a
59+
// pair. Here we have a filter but no dynamic function, create a default one
60+
// that returns the same project and monitored resource for all data points.
61+
auto project_id = project->project_id();
62+
auto monitored_resource = mr_proto.value_or(google::api::MonitoredResource{});
63+
return [project_id, monitored_resource](
64+
opentelemetry::sdk::metrics::PointDataAttributes const&) {
65+
return std::make_pair(project_id, monitored_resource);
66+
};
67+
}
68+
5069
} // namespace
5170

5271
MonitoringExporter::MonitoringExporter(
@@ -65,9 +84,10 @@ MonitoringExporter::MonitoringExporter(
6584
Project project,
6685
std::shared_ptr<monitoring_v3::MetricServiceConnection> conn,
6786
Options const& options)
68-
: MonitoringExporter(std::move(conn), nullptr, MakeFilter(options),
69-
options) {
87+
: MonitoringExporter(std::move(conn), nullptr, nullptr, options) {
7088
project_ = std::move(project);
89+
resource_filter_fn_ = MakeResourceFilterFn(options);
90+
dynamic_resource_fn_ = MakeDynamicResourceFn(options, project_, mr_proto_);
7191
}
7292

7393
opentelemetry::sdk::common::ExportResult MonitoringExporter::Export(
@@ -115,19 +135,7 @@ opentelemetry::sdk::common::ExportResult MonitoringExporter::ExportImpl(
115135
}
116136

117137
std::vector<google::monitoring::v3::CreateTimeSeriesRequest> requests;
118-
if (dynamic_resource_fn_ || resource_filter_fn_) {
119-
// If `resource_filter_fn_` is provided, we must use
120-
// `ToTimeSeriesWithResources`, which requires a dynamic resource
121-
// function. If `dynamic_resource_fn_` is not provided, create a
122-
// default implementation.
123-
if (!dynamic_resource_fn_) {
124-
auto mr = otel_internal::ToMonitoredResource(data, mr_proto_);
125-
dynamic_resource_fn_ =
126-
[mr, p = project_->project_id()](
127-
opentelemetry::sdk::metrics::PointDataAttributes const&) {
128-
return std::make_pair(p, mr);
129-
};
130-
}
138+
if (dynamic_resource_fn_) {
131139
auto tss_map = otel_internal::ToTimeSeriesWithResources(
132140
data, formatter_, resource_filter_fn_, dynamic_resource_fn_);
133141
for (auto& tss : tss_map) {

0 commit comments

Comments
 (0)