[Performance] Do not construct `KeyValueIterable` when calling OTel for instruments with empty label/optional label (#37311)

This seems to cut ~5 nanoseconds (10%) off of the `BM_AddCounterWithOTelPlugin` benchmark.

<!--

If you know who should review your pull request, please assign it to that
person, otherwise the pull request would get assigned randomly.

If your pull request is for a specific language, please add the appropriate
lang label.

-->

Closes #37311

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/37311 from yijiem:otel-metrics-benchmark 2f8c975cfc
PiperOrigin-RevId: 658915810
pull/37331/head
Yijie Ma 6 months ago committed by Copybara-Service
parent 116bcb46ee
commit ecc1ea512e
  1. 115
      src/cpp/ext/otel/otel_plugin.cc

@ -637,12 +637,18 @@ void OpenTelemetryPluginImpl::AddCounter(
grpc_core::GlobalInstrumentsRegistry::GetInstrumentDescriptor(handle);
CHECK(descriptor.label_keys.size() == label_values.size());
CHECK(descriptor.optional_label_keys.size() == optional_values.size());
absl::get<std::unique_ptr<opentelemetry::metrics::Counter<uint64_t>>>(
instrument_data.instrument)
->Add(value, NPCMetricsKeyValueIterable(
descriptor.label_keys, label_values,
descriptor.optional_label_keys, optional_values,
instrument_data.optional_labels_bits));
if (label_values.empty() && optional_values.empty()) {
absl::get<std::unique_ptr<opentelemetry::metrics::Counter<uint64_t>>>(
instrument_data.instrument)
->Add(value);
} else {
absl::get<std::unique_ptr<opentelemetry::metrics::Counter<uint64_t>>>(
instrument_data.instrument)
->Add(value, NPCMetricsKeyValueIterable(
descriptor.label_keys, label_values,
descriptor.optional_label_keys, optional_values,
instrument_data.optional_labels_bits));
}
}
void OpenTelemetryPluginImpl::AddCounter(
@ -661,12 +667,18 @@ void OpenTelemetryPluginImpl::AddCounter(
grpc_core::GlobalInstrumentsRegistry::GetInstrumentDescriptor(handle);
CHECK(descriptor.label_keys.size() == label_values.size());
CHECK(descriptor.optional_label_keys.size() == optional_values.size());
absl::get<std::unique_ptr<opentelemetry::metrics::Counter<double>>>(
instrument_data.instrument)
->Add(value, NPCMetricsKeyValueIterable(
descriptor.label_keys, label_values,
descriptor.optional_label_keys, optional_values,
instrument_data.optional_labels_bits));
if (label_values.empty() && optional_values.empty()) {
absl::get<std::unique_ptr<opentelemetry::metrics::Counter<double>>>(
instrument_data.instrument)
->Add(value);
} else {
absl::get<std::unique_ptr<opentelemetry::metrics::Counter<double>>>(
instrument_data.instrument)
->Add(value, NPCMetricsKeyValueIterable(
descriptor.label_keys, label_values,
descriptor.optional_label_keys, optional_values,
instrument_data.optional_labels_bits));
}
}
void OpenTelemetryPluginImpl::RecordHistogram(
@ -685,14 +697,20 @@ void OpenTelemetryPluginImpl::RecordHistogram(
grpc_core::GlobalInstrumentsRegistry::GetInstrumentDescriptor(handle);
CHECK(descriptor.label_keys.size() == label_values.size());
CHECK(descriptor.optional_label_keys.size() == optional_values.size());
absl::get<std::unique_ptr<opentelemetry::metrics::Histogram<uint64_t>>>(
instrument_data.instrument)
->Record(value,
NPCMetricsKeyValueIterable(descriptor.label_keys, label_values,
descriptor.optional_label_keys,
optional_values,
instrument_data.optional_labels_bits),
opentelemetry::context::Context{});
if (label_values.empty() && optional_values.empty()) {
absl::get<std::unique_ptr<opentelemetry::metrics::Histogram<uint64_t>>>(
instrument_data.instrument)
->Record(value, opentelemetry::context::Context{});
} else {
absl::get<std::unique_ptr<opentelemetry::metrics::Histogram<uint64_t>>>(
instrument_data.instrument)
->Record(value,
NPCMetricsKeyValueIterable(
descriptor.label_keys, label_values,
descriptor.optional_label_keys, optional_values,
instrument_data.optional_labels_bits),
opentelemetry::context::Context{});
}
}
void OpenTelemetryPluginImpl::RecordHistogram(
@ -711,14 +729,20 @@ void OpenTelemetryPluginImpl::RecordHistogram(
grpc_core::GlobalInstrumentsRegistry::GetInstrumentDescriptor(handle);
CHECK(descriptor.label_keys.size() == label_values.size());
CHECK(descriptor.optional_label_keys.size() == optional_values.size());
absl::get<std::unique_ptr<opentelemetry::metrics::Histogram<double>>>(
instrument_data.instrument)
->Record(value,
NPCMetricsKeyValueIterable(descriptor.label_keys, label_values,
descriptor.optional_label_keys,
optional_values,
instrument_data.optional_labels_bits),
opentelemetry::context::Context{});
if (label_values.empty() && optional_values.empty()) {
absl::get<std::unique_ptr<opentelemetry::metrics::Histogram<double>>>(
instrument_data.instrument)
->Record(value, opentelemetry::context::Context{});
} else {
absl::get<std::unique_ptr<opentelemetry::metrics::Histogram<double>>>(
instrument_data.instrument)
->Record(value,
NPCMetricsKeyValueIterable(
descriptor.label_keys, label_values,
descriptor.optional_label_keys, optional_values,
instrument_data.optional_labels_bits),
opentelemetry::context::Context{});
}
}
void OpenTelemetryPluginImpl::AddCallback(
@ -881,20 +905,27 @@ void OpenTelemetryPluginImpl::CallbackGaugeState<ValueType>::Observe(
for (const auto& pair : cache) {
CHECK(pair.first.size() <= (descriptor.label_keys.size() +
descriptor.optional_label_keys.size()));
auto& instrument_data = ot_plugin->instruments_data_.at(id);
opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::ObserverResultT<ValueType>>>(result)
->Observe(pair.second,
NPCMetricsKeyValueIterable(
descriptor.label_keys,
absl::FixedArray<absl::string_view>(
pair.first.begin(),
pair.first.begin() + descriptor.label_keys.size()),
descriptor.optional_label_keys,
absl::FixedArray<absl::string_view>(
pair.first.begin() + descriptor.label_keys.size(),
pair.first.end()),
instrument_data.optional_labels_bits));
if (descriptor.label_keys.empty() &&
descriptor.optional_label_keys.empty()) {
opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::ObserverResultT<ValueType>>>(result)
->Observe(pair.second);
} else {
auto& instrument_data = ot_plugin->instruments_data_.at(id);
opentelemetry::nostd::get<opentelemetry::nostd::shared_ptr<
opentelemetry::metrics::ObserverResultT<ValueType>>>(result)
->Observe(pair.second,
NPCMetricsKeyValueIterable(
descriptor.label_keys,
absl::FixedArray<absl::string_view>(
pair.first.begin(),
pair.first.begin() + descriptor.label_keys.size()),
descriptor.optional_label_keys,
absl::FixedArray<absl::string_view>(
pair.first.begin() + descriptor.label_keys.size(),
pair.first.end()),
instrument_data.optional_labels_bits));
}
}
}

Loading…
Cancel
Save