Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTel Cpp Metrics with OTLP gRPC Exporter #1967

Closed
julianocosta89 opened this issue Feb 8, 2023 · 12 comments
Closed

OTel Cpp Metrics with OTLP gRPC Exporter #1967

julianocosta89 opened this issue Feb 8, 2023 · 12 comments
Assignees
Labels
bug Something isn't working

Comments

@julianocosta89
Copy link
Member

request for assistance
I'm trying to add metrics to the OTel demo, but I'm struggling for some time.
Would someone be able to shed some light in my path?

I have everything that I've added in a branch: https://github.com/julianocosta89/opentelemetry-demo/tree/cpp-metrics/src/currencyservice

And the detailed changes below:

I'm using OTel version 1.8.2
I've added to the target_link_libraries the following:

opentelemetry_metrics opentelemetry_exporter_otlp_grpc_metrics

And at the moment I have everything in a single header file, just to test it out:

#include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter_factory.h"
#include "opentelemetry/metrics/provider.h"
#include "opentelemetry/sdk/metrics/aggregation/default_aggregation.h"
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
#include "opentelemetry/sdk/metrics/meter.h"
#include "opentelemetry/sdk/metrics/meter_provider.h"

namespace metric_sdk    = opentelemetry::sdk::metrics;
namespace common        = opentelemetry::common;
namespace metrics_api   = opentelemetry::metrics;
namespace otlp_exporter = opentelemetry::exporter::otlp;

namespace
{

	void initMeter() {
		std::string version{"0.1.0"};
		std::string name{"otel"};
		std::string schema{"https://opentelemetry.io/schemas/1.2.0"};

		//Build MetricExporter
		otlp_exporter::OtlpGrpcMetricExporterOptions otlpOptions;
		otlpOptions.endpoint = "otelcol:4317";
		otlpOptions.aggregation_temporality = metric_sdk::AggregationTemporality::kDelta;
		auto exporter = otlp_exporter::OtlpGrpcMetricExporterFactory::Create(otlpOptions);

		//Build MeterProvider and Reader
		metric_sdk::PeriodicExportingMetricReaderOptions options;
		options.export_interval_millis = std::chrono::milliseconds(1000);
		options.export_timeout_millis = std::chrono::milliseconds(500);
		std::unique_ptr<metric_sdk::MetricReader> reader{
			new metric_sdk::PeriodicExportingMetricReader(std::move(exporter), options) };
		auto provider = std::shared_ptr<metrics_api::MeterProvider>(new metric_sdk::MeterProvider());
		auto p = std::static_pointer_cast<metric_sdk::MeterProvider>(provider);
		p->AddMetricReader(std::move(reader));
		metrics_api::Provider::SetMeterProvider(provider);

		//Build Meter for Counter
		std::string counter_name = name + "_counter";
		std::unique_ptr<metric_sdk::InstrumentSelector> instrument_selector{
			new metric_sdk::InstrumentSelector(metric_sdk::InstrumentType::kCounter, counter_name) };
		std::unique_ptr<metric_sdk::MeterSelector> meter_selector{
			new metric_sdk::MeterSelector(name, version, schema) };
		std::unique_ptr<metric_sdk::View> sum_view{
			new metric_sdk::View{name, "description", metric_sdk::AggregationType::kSum} };
		p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

		//Build instrument
		auto meter = provider->GetMeter(name, "0.0.0");
		auto double_counter = meter->CreateDoubleCounter(counter_name);
		// Create a label set which annotates metric values
		std::map<std::string, std::string> labels = { {"key", "value"} };
		auto labelkv = common::KeyValueIterableView<decltype(labels)>{ labels };
		double_counter->Add(1.0, labelkv);

	}
}

Whenever I try to build the sample, I'm getting:

#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/local/lib/libopentelemetry_exporter_otlp_grpc_metrics.a(otlp_grpc_metric_exporter.cc.o): in function `opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporterOptions const&)':
#0 11.86 otlp_grpc_metric_exporter.cc:(.text+0xb64): undefined reference to `opentelemetry::v1::exporter::otlp::OtlpMetricUtils::ChooseTemporalitySelector(opentelemetry::v1::sdk::metrics::AggregationTemporality)'
#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: otlp_grpc_metric_exporter.cc:(.text+0xba2): undefined reference to `opentelemetry::proto::collector::metrics::v1::MetricsService::NewStub(std::shared_ptr<grpc::ChannelInterface> const&, grpc::StubOptions const&)'
#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/local/lib/libopentelemetry_exporter_otlp_grpc_metrics.a(otlp_grpc_metric_exporter.cc.o): in function `opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporter::OtlpGrpcMetricExporter(std::unique_ptr<opentelemetry::proto::collector::metrics::v1::MetricsService::StubInterface, std::default_delete<opentelemetry::proto::collector::metrics::v1::MetricsService::StubInterface> >)':
#0 11.86 otlp_grpc_metric_exporter.cc:(.text+0xec7): undefined reference to `opentelemetry::v1::exporter::otlp::OtlpMetricUtils::ChooseTemporalitySelector(opentelemetry::v1::sdk::metrics::AggregationTemporality)'
#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: /usr/local/lib/libopentelemetry_exporter_otlp_grpc_metrics.a(otlp_grpc_metric_exporter.cc.o): in function `opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporter::Export(opentelemetry::v1::sdk::metrics::ResourceMetrics const&)':
#0 11.86 otlp_grpc_metric_exporter.cc:(.text+0x10dc): undefined reference to `opentelemetry::proto::collector::metrics::v1::ExportMetricsServiceRequest::ExportMetricsServiceRequest(google::protobuf::Arena*, bool)'
#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: otlp_grpc_metric_exporter.cc:(.text+0x10ef): undefined reference to `opentelemetry::v1::exporter::otlp::OtlpMetricUtils::PopulateRequest(opentelemetry::v1::sdk::metrics::ResourceMetrics const&, opentelemetry::proto::collector::metrics::v1::ExportMetricsServiceRequest*)'
#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: otlp_grpc_metric_exporter.cc:(.text+0x110d): undefined reference to `opentelemetry::proto::collector::metrics::v1::ExportMetricsServiceResponse::ExportMetricsServiceResponse(google::protobuf::Arena*, bool)'
#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: otlp_grpc_metric_exporter.cc:(.text+0x117e): undefined reference to `opentelemetry::proto::collector::metrics::v1::ExportMetricsServiceResponse::~ExportMetricsServiceResponse()'
#0 11.86 /usr/lib/gcc/x86_64-alpine-linux-musl/12.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: otlp_grpc_metric_exporter.cc:(.text+0x11a3): undefined reference to `opentelemetry::proto::collector::metrics::v1::ExportMetricsServiceRequest::~ExportMetricsServiceRequest()'
#0 11.92 collect2: error: ld returned 1 exit status
#0 11.92 make[2]: *** [CMakeFiles/currencyservice.dir/build.make:154: currencyservice] Error 1
#0 11.92 make[1]: *** [CMakeFiles/Makefile2:111: CMakeFiles/currencyservice.dir/all] Error 2
#0 11.92 make: *** [Makefile:136: all] Error 2
------
failed to solve: process "/bin/sh -c cd /currencyservice     && mkdir -p build && cd build     && cmake ..     && make -j$(nproc || sysctl -n hw.ncpu || echo 1) install" did not complete successfully: exit code: 2

Any ideas what am I missing?

@julianocosta89 julianocosta89 added the bug Something isn't working label Feb 8, 2023
@lalitb lalitb self-assigned this Feb 8, 2023
@lalitb
Copy link
Member

lalitb commented Feb 9, 2023

Which platform? I can successfully currencyservice from your branch on ubuntu 20.04.

@julianocosta89
Copy link
Member Author

Oh, really?
You didn't need to change anything?
I can't build it locally on Ubuntu 20.04, maybe I'm still missing some dependencies on my machine.

But the Dockerfile is using Alpine.
Maybe @svrnm also has some thoughts, as he recently changed it.

@lalitb
Copy link
Member

lalitb commented Feb 9, 2023

Oh, really?
You didn't need to change anything?

Yes for ubuntu. But I had grpc and protobuf already installed on my machine.

But the Dockerfile is using Alpine.

Ok, this could be something similar to #1940 raised by @svrnm, will check on Alpine.

@julianocosta89
Copy link
Member Author

I see 😢
I'll close this ticket and follow the other one.

Thanks for taking a look @lalitb 🤩

@julianocosta89
Copy link
Member Author

@lalitb is the OTLP Metric exporter actually working on release 1.8.2?
Are metrics really GA for OTel C++?

@lalitb
Copy link
Member

lalitb commented Feb 9, 2023

@julianocosta89 yes it is GA. What issue do you see in OTLP export?

@julianocosta89
Copy link
Member Author

I don't see... That's the issue 😅
I can't build it.

Do I need to use the factory?
I can't find the Create method.
The docs do not say which headers I need to include.
https://opentelemetry.io/docs/instrumentation/cpp/exporters/#otlp-grpc-exporter-1

@julianocosta89
Copy link
Member Author

Tried also the HTTP one, but then I couldn't find the temporality... It was a long day 😕

@lalitb
Copy link
Member

lalitb commented Feb 9, 2023

@lalitb
Copy link
Member

lalitb commented Feb 10, 2023

If these are still the build issues, they should be related to Alpine as it provides binary package for grpc, and we haven't tested this env.

@julianocosta89
Copy link
Member Author

I'll try this out.
I've moved away from Alpine in my local development.
Now I'm using Ubuntu 20.04.

Thx again for the Help @lalitb!

@julianocosta89
Copy link
Member Author

Thanks for the help again @lalitb.
Just sent a PR to the demo repo: open-telemetry/opentelemetry-demo#759

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants