Skip to content

Commit

Permalink
Add unit test for OtlpHttpMetricExporter
Browse files Browse the repository at this point in the history
Signed-off-by: owentou <owentou@tencent.com>
  • Loading branch information
owent committed Jul 16, 2022
1 parent 9a79f7d commit 330e514
Show file tree
Hide file tree
Showing 5 changed files with 583 additions and 23 deletions.
3 changes: 3 additions & 0 deletions exporters/otlp/src/otlp_http_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ opentelemetry::sdk::common::ExportResult OtlpHttpExporter::Export(
{
if (http_client_->IsShutdown())
{
std::size_t span_count = spans.size();
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] ERROR: Export "
<< span_count << " trace span(s) failed, exporter is shutdown");
return opentelemetry::sdk::common::ExportResult::kFailure;
}

Expand Down
3 changes: 3 additions & 0 deletions exporters/otlp/src/otlp_http_log_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ opentelemetry::sdk::common::ExportResult OtlpHttpLogExporter::Export(
{
if (http_client_->IsShutdown())
{
std::size_t log_count = logs.size();
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] ERROR: Export "
<< log_count << " log(s) failed, exporter is shutdown");
return opentelemetry::sdk::common::ExportResult::kFailure;
}

Expand Down
3 changes: 3 additions & 0 deletions exporters/otlp/src/otlp_http_metric_exporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ opentelemetry::sdk::common::ExportResult OtlpHttpMetricExporter::Export(
{
if (http_client_->IsShutdown())
{
std::size_t metric_count = data.instrumentation_info_metric_data_.size();
OTEL_INTERNAL_LOG_ERROR("[OTLP HTTP Client] ERROR: Export "
<< metric_count << " metric(s) failed, exporter is shutdown");
return opentelemetry::sdk::common::ExportResult::kFailure;
}

Expand Down
31 changes: 13 additions & 18 deletions exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,23 @@ void OtlpMetricUtils::ConvertGaugeMetric(const opentelemetry::sdk::metrics::Metr
auto ts = metric_data.end_ts.time_since_epoch().count();
for (auto &point_data_with_attributes : metric_data.point_data_attr_)
{
proto::metrics::v1::NumberDataPoint *proto_sum_point_data = gauge->add_data_points();
proto_sum_point_data->set_start_time_unix_nano(start_ts);
proto_sum_point_data->set_time_unix_nano(ts);
auto sum_data = nostd::get<sdk::metrics::SumPointData>(point_data_with_attributes.point_data);
proto::metrics::v1::NumberDataPoint *proto_gauge_point_data = gauge->add_data_points();
proto_gauge_point_data->set_start_time_unix_nano(start_ts);
proto_gauge_point_data->set_time_unix_nano(ts);
auto gauge_data = nostd::get<sdk::metrics::LastValuePointData>(point_data_with_attributes.point_data);

if ((nostd::holds_alternative<long>(sum_data.value_)))
if ((nostd::holds_alternative<long>(gauge_data.value_)))
{
proto_sum_point_data->set_as_int(nostd::get<long>(sum_data.value_));
proto_gauge_point_data->set_as_int(nostd::get<long>(gauge_data.value_));
}
else
{
proto_sum_point_data->set_as_double(nostd::get<double>(sum_data.value_));
proto_gauge_point_data->set_as_double(nostd::get<double>(gauge_data.value_));
}
// set attributes
for (auto &kv_attr : point_data_with_attributes.attributes)
{
OtlpPopulateAttributeUtils::PopulateAttribute(proto_sum_point_data->add_attributes(),
OtlpPopulateAttributeUtils::PopulateAttribute(proto_gauge_point_data->add_attributes(),
kv_attr.first, kv_attr.second);
}
}
Expand Down Expand Up @@ -197,9 +197,7 @@ void OtlpMetricUtils::PopulateResourceMetrics(
const opentelemetry::sdk::metrics::ResourceMetrics &data,
proto::metrics::v1::ResourceMetrics *resource_metrics) noexcept
{
proto::resource::v1::Resource proto;
OtlpPopulateAttributeUtils::PopulateAttribute(&proto, *(data.resource_));
*resource_metrics->mutable_resource() = proto;
OtlpPopulateAttributeUtils::PopulateAttribute(resource_metrics->mutable_resource(), *(data.resource_));

for (auto &instrumentation_metrics : data.instrumentation_info_metric_data_)
{
Expand All @@ -208,17 +206,14 @@ void OtlpMetricUtils::PopulateResourceMetrics(
continue;
}
auto instrumentation_lib_metrics = resource_metrics->add_instrumentation_library_metrics();
proto::common::v1::InstrumentationLibrary instrumentation_library;
instrumentation_library.set_name(instrumentation_metrics.instrumentation_library_->GetName());
instrumentation_library.set_version(
proto::common::v1::InstrumentationLibrary* instrumentation_library = instrumentation_lib_metrics->mutable_instrumentation_library();
instrumentation_library->set_name(instrumentation_metrics.instrumentation_library_->GetName());
instrumentation_library->set_version(
instrumentation_metrics.instrumentation_library_->GetVersion());
*instrumentation_lib_metrics->mutable_instrumentation_library() = instrumentation_library;

for (auto &metric_data : instrumentation_metrics.metric_data_)
{
proto::metrics::v1::Metric metric;
PopulateInstrumentationInfoMetric(metric_data, &metric);
*instrumentation_lib_metrics->add_metrics() = metric;
PopulateInstrumentationInfoMetric(metric_data, instrumentation_lib_metrics->add_metrics());
}
}
}
Expand Down
Loading

0 comments on commit 330e514

Please sign in to comment.