Skip to content

Commit

Permalink
Update otel_tracer plugin (#11873)
Browse files Browse the repository at this point in the history
* update otel_tracer plugin

* fix format
  • Loading branch information
shukitchan authored Dec 9, 2024
1 parent 1090a38 commit 8f07c04
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 30 deletions.
4 changes: 4 additions & 0 deletions cmake/Findopentelemetry.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ set(OTEL_LIBS
opentelemetry_exporter_ostream_span
opentelemetry_exporter_otlp_http
opentelemetry_exporter_otlp_http_client
opentelemetry_exporter_otlp_http_log
opentelemetry_exporter_otlp_http_metric
opentelemetry_http_client_curl
opentelemetry_metrics
opentelemetry_otlp_recordable
Expand All @@ -41,6 +43,8 @@ set(OTEL_LIBS
opentelemetry_trace
opentelemetry_version
opentelemetry_common
opentelemetry_metrics
opentelemetry_logs
)

find_path(opentelemetry_INCLUDE_DIR NAMES opentelemetry/version.h)
Expand Down
40 changes: 19 additions & 21 deletions doc/admin-guide/plugins/otel_tracer.en.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,14 @@ Compiling the Plugin

To compile this plugin, we need nlohmann-json, protobuf and opentelemetry-cpp

nlohmann-json:

::

cd
wget https://github.com/nlohmann/json/archive/refs/tags/v3.9.1.tar.gz
tar zxvf v3.9.1.tar.gz
cd json-3.9.1
mkdir build
cd build
cmake .. -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
make
make install

protobuf:

::

cd
wget https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.19.4.tar.gz
tar zxvf v3.19.4.tar.gz
cd protobuf-3.19.4
wget https://github.com/protocolbuffers/protobuf/archive/refs/tags/v3.21.12.tar.gz
tar zxvf v3.21.12.tar.gz
cd protobuf-3.21.12
./autogen.sh
./configure --enable-shared=no --enable-static=yes CXXFLAGS="-std=c++17 -fPIC" CFLAGS="-fPIC"
make
Expand All @@ -82,12 +68,12 @@ opentelemetry-cpp
::

cd
wget https://github.com/open-telemetry/opentelemetry-cpp/archive/refs/tags/v1.3.0.tar.gz
tar zxvf v1.3.0.tar.gz
cd opentelemetry-cpp-1.3.0
wget https://github.com/open-telemetry/opentelemetry-cpp/archive/refs/tags/v1.11.0.tar.gz
tar zxvf v1.11.0.tar.gz
cd opentelemetry-cpp-1.11.0
mkdir build
cd build
cmake .. -DBUILD_TESTING=OFF -DWITH_EXAMPLES=OFF -DWITH_JAEGER=OFF -DWITH_OTLP=ON -DWITH_OTLP_GRPC=OFF -DWITH_OTLP_HTTP=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
cmake .. -DBUILD_TESTING=OFF -DWITH_EXAMPLES=OFF -DWITH_JAEGER=OFF -DWITH_OTLP_GRPC=OFF -DWITH_OTLP_HTTP=ON -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON -DWITH_ABSEIL=OFF
cmake --build . --target all
cmake --install . --config Debug --prefix /usr/local/

Expand All @@ -112,3 +98,15 @@ This is the service name that will be sent as part of the information to the OTL
* ``-r=[sampling rate]`` (default: ``1.0``)

The value can be between 0.0 to 1.0. It controls the sampling rate of the trace information.

* ``-q=[queue size]`` (default: ``25``)

The size of the batch processor queue.

* ``-d=[delay]`` (default: ``3000``)

The time interval between two consecutive exports in milliseconds.

* ``-b=[batch size]`` (default: ``10``)

The maximum batch size of every export. Should be smaller than queue size.
31 changes: 29 additions & 2 deletions plugins/experimental/otel_tracer/otel_tracer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,23 @@ TSPluginInit(int argc, const char *argv[])
std::string url = "";
std::string service_name = "otel_tracer";
double rate = 1.0;
int qsize = 25;
int delay = 3000;
int bsize = 10;
if (argc > 1) {
int c;
static const struct option longopts[] = {
{const_cast<char *>("url"), required_argument, nullptr, 'u'},
{const_cast<char *>("service-name"), required_argument, nullptr, 's'},
{const_cast<char *>("sampling-rate"), required_argument, nullptr, 'r'},
{const_cast<char *>("queue-size"), required_argument, nullptr, 'q'},
{const_cast<char *>("delay"), required_argument, nullptr, 'd'},
{const_cast<char *>("batch-size"), required_argument, nullptr, 'b'},
{nullptr, 0, nullptr, 0 },
};

int longindex = 0;
while ((c = getopt_long(argc, const_cast<char *const *>(argv), "u:s:r:", longopts, &longindex)) != -1) {
while ((c = getopt_long(argc, const_cast<char *const *>(argv), "u:s:r:q:d:b:", longopts, &longindex)) != -1) {
switch (c) {
case 'u':
url = optarg;
Expand All @@ -354,13 +360,34 @@ TSPluginInit(int argc, const char *argv[])
break;
case 'r':
rate = atof(optarg);
if (rate < 0) {
TSEmergency("[otel_tracer][%s] Invalid rate parameter", __FUNCTION__);
}
break;
case 'q':
qsize = atoi(optarg);
if (qsize < 0) {
TSEmergency("[otel_tracer][%s] Invalid queue size parameter", __FUNCTION__);
}
break;
case 'd':
delay = atoi(optarg);
if (delay < 0) {
TSEmergency("[otel_tracer][%s] Invalid delay parameter", __FUNCTION__);
}
break;
case 'b':
bsize = atoi(optarg);
if (bsize < 0) {
TSEmergency("[otel_tracer][%s] Invalid batch size parameter", __FUNCTION__);
}
break;
default:
break;
}
}
}
InitTracer(url, service_name, rate);
InitTracer(url, service_name, rate, qsize, delay, bsize);

if (TSPluginRegister(&info) != TS_SUCCESS) {
TSError("[%s] Plugin registration failed", PLUGIN_NAME);
Expand Down
28 changes: 21 additions & 7 deletions plugins/experimental/otel_tracer/tracer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "opentelemetry/exporters/ostream/span_exporter.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/tracer_context.h"
#include "opentelemetry/sdk/trace/tracer_provider.h"
#include "opentelemetry/trace/provider.h"

Expand All @@ -35,6 +36,13 @@
#include "opentelemetry/sdk/trace/samplers/parent.h"
#include "opentelemetry/sdk/trace/samplers/trace_id_ratio.h"

#include "opentelemetry/exporters/otlp/otlp_http_exporter_factory.h"
#include "opentelemetry/sdk/trace/simple_processor_factory.h"
#include "opentelemetry/sdk/trace/batch_span_processor_factory.h"
#include "opentelemetry/sdk/trace/batch_span_processor_options.h"
#include "opentelemetry/sdk/trace/tracer_context_factory.h"
#include "opentelemetry/sdk/trace/tracer_provider_factory.h"

#include <cstring>
#include <iostream>
#include <vector>
Expand Down Expand Up @@ -112,16 +120,21 @@ struct ExtraRequestData {
};

void
InitTracer(const std::string &url, const std::string &service_name, double rate)
InitTracer(const std::string &url, const std::string &service_name, double rate, int qsize, int delay, int bsize)
{
otlp::OtlpHttpExporterOptions opts;

if (url != "") {
opts.url = url;
}

auto exporter = std::unique_ptr<sdktrace::SpanExporter>(new otlp::OtlpHttpExporter(opts));
auto processor = std::unique_ptr<sdktrace::SpanProcessor>(new sdktrace::SimpleSpanProcessor(std::move(exporter)));
auto exporter = otlp::OtlpHttpExporterFactory::Create(opts);

sdktrace::BatchSpanProcessorOptions options{};
options.max_queue_size = qsize;
options.schedule_delay_millis = std::chrono::milliseconds(delay);
options.max_export_batch_size = bsize;
auto processor = sdktrace::BatchSpanProcessorFactory::Create(std::move(exporter), options);

std::vector<std::unique_ptr<sdktrace::SpanProcessor>> processors;
processors.push_back(std::move(processor));
Expand All @@ -132,11 +145,12 @@ InitTracer(const std::string &url, const std::string &service_name, double rate)
{"version", (uint32_t)1 }
};
auto resource = opentelemetry::sdk::resource::Resource::Create(attributes);
auto context = sdktrace::TracerContextFactory::Create(std::move(processors), resource,
std::unique_ptr<sdktrace::Sampler>(new sdktrace::ParentBasedSampler(
std::make_shared<sdktrace::TraceIdRatioBasedSampler>(rate))));

auto context = std::make_shared<sdktrace::TracerContext>(std::move(processors), resource,
std::unique_ptr<sdktrace::Sampler>(new sdktrace::ParentBasedSampler(
std::make_shared<sdktrace::TraceIdRatioBasedSampler>(rate))));
auto provider = nostd::shared_ptr<trace::TracerProvider>(new sdktrace::TracerProvider(context));
// create provider through factory
std::shared_ptr<trace::TracerProvider> provider = sdktrace::TracerProviderFactory::Create(std::move(context));

// Set the global trace provider
trace::Provider::SetTracerProvider(provider);
Expand Down

0 comments on commit 8f07c04

Please sign in to comment.