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

Santi/rebase 18.x #141

Merged
merged 13 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ test-with-console:
test-agents-prereqs:
env npm_config_nodedir=$(PWD) $(NODE) ./deps/npm install zeromq@5 base85 --prefix test/common/nsolid-zmq-agent --no-save --no-package-lock
env npm_config_nodedir=$(PWD) $(NODE) ./deps/npm run build:libzmq --prefix test/common/nsolid-zmq-agent/node_modules/zeromq
env npm_config_nodedir=$(PWD) $(NODE) ./deps/npm install @opentelemetry/otlp-proto-exporter-base --prefix test/common/nsolid-otlp-agent --no-save --no-package-lock
env npm_config_nodedir=$(PWD) $(NODE) ./deps/npm install @opentelemetry/otlp-proto-exporter-base @grpc/grpc-js @grpc/proto-loader --prefix test/common/nsolid-otlp-agent --no-save --no-package-lock

.PHONY: test-agents-prereqs-clean
test-agents-prereqs-clean:
Expand Down
51 changes: 44 additions & 7 deletions agents/otlp/src/otlp_agent.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// This header needs to be included before any other grpc header otherwise there
// will be a compilation error because of abseil.
// Refs: https://github.com/open-telemetry/opentelemetry-cpp/blob/32cd66b62333e84aa8e92a4447e0aa667b6735e5/examples/otlp/README.md#additional-notes-regarding-abseil-library
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h"
#include "otlp_agent.h"
#include "nsolid/nsolid_api.h"
#include "env-inl.h"
Expand All @@ -9,6 +13,7 @@
#include "otlp_metrics.h"
#include "opentelemetry/sdk/resource/semantic_conventions.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/exporters/otlp/otlp_environment.h"
#include "opentelemetry/exporters/otlp/otlp_http_exporter.h"
#include "opentelemetry/ext/http/client/curl/http_client_curl.h"
#include "opentelemetry/trace/propagation/detail/hex.h"
Expand Down Expand Up @@ -580,8 +585,7 @@ void OTLPAgent::config_otlp_agent(const json& config) {
metrics_exporter_.reset(nullptr);
std::string type = *it;
it = config.find("otlpConfig");
ASSERT(it != config.end());
const nlohmann::json& otlp = it.value();
const nlohmann::json otlp = it == config.end() ? json::object() : *it;
config_endpoint(type, otlp);
} else {
Debug("No otlp agent configuration. Stopping the agent\n");
Expand All @@ -591,13 +595,40 @@ void OTLPAgent::config_otlp_agent(const json& config) {


void OTLPAgent::config_otlp_endpoint(const json& config) {
if (config.empty()) {
const std::string prot = exporter::otlp::GetOtlpDefaultHttpTracesProtocol();
if (prot == "grpc") {
exporter::otlp::OtlpGrpcExporterOptions opts;
setup_trace_grpc_otlp_exporter(opts);
} else {
exporter::otlp::OtlpHttpExporterOptions opts;
setup_trace_otlp_exporter(opts);
}

metrics_exporter_.reset(new OTLPMetrics(&loop_, *this));
return;
}

auto it = config.find("url");
ASSERT(it != config.end());
exporter::otlp::OtlpHttpExporterOptions opts;
const std::string url = it->get<std::string>();
opts.url = url + "/v1/traces";
setup_trace_otlp_exporter(opts);
metrics_exporter_.reset(new OTLPMetrics(&loop_, url, "", *this));
bool is_http = true;
it = config.find("protocol");
if (it != config.end()) {
is_http = *it == "http";
}

const std::string url = config["url"].get<std::string>() + "/v1/traces";
if (is_http) {
exporter::otlp::OtlpHttpExporterOptions opts;
opts.url = url;
setup_trace_otlp_exporter(opts);
} else {
exporter::otlp::OtlpGrpcExporterOptions opts;
opts.endpoint = url;
setup_trace_grpc_otlp_exporter(opts);
}

metrics_exporter_.reset(new OTLPMetrics(&loop_, url, "", is_http, *this));
}

resource::Resource OTLPAgent::create_resource() const {
Expand Down Expand Up @@ -639,6 +670,12 @@ void OTLPAgent::setup_trace_otlp_exporter(
}


void OTLPAgent::setup_trace_grpc_otlp_exporter(
exporter::otlp::OtlpGrpcExporterOptions& opts) {
otlp_http_exporter_.reset(new exporter::otlp::OtlpGrpcExporter(opts));
}


int OTLPAgent::setup_metrics_timer(uint64_t period) {
if (period == 0) {
return metrics_timer_.stop();
Expand Down
10 changes: 8 additions & 2 deletions agents/otlp/src/otlp_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@
OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter {
namespace otlp {
class OtlpHttpExporter;
struct OtlpHttpExporterOptions;
struct OtlpGrpcExporterOptions;
}
}
namespace sdk {
namespace instrumentationscope {
class InstrumentationScope;
}
namespace trace {
class SpanExporter;
}
}
OPENTELEMETRY_END_NAMESPACE

Expand Down Expand Up @@ -104,6 +107,9 @@ class OTLPAgent {
void setup_trace_otlp_exporter( // NOLINTNEXTLINE(runtime/references)
OPENTELEMETRY_NAMESPACE::exporter::otlp::OtlpHttpExporterOptions& opts);

void setup_trace_grpc_otlp_exporter( // NOLINTNEXTLINE(runtime/references)
OPENTELEMETRY_NAMESPACE::exporter::otlp::OtlpGrpcExporterOptions& opts);

int setup_metrics_timer(uint64_t period);

void update_tracer(uint32_t flags);
Expand All @@ -128,7 +134,7 @@ class OTLPAgent {
TSQueue<Tracer::SpanStor> span_msg_q_;
uint32_t trace_flags_;

std::unique_ptr<OPENTELEMETRY_NAMESPACE::exporter::otlp::OtlpHttpExporter>
std::unique_ptr<OPENTELEMETRY_NAMESPACE::sdk::trace::SpanExporter>
otlp_http_exporter_;
OPENTELEMETRY_NAMESPACE::sdk::resource::Resource resource_;
std::unique_ptr
Expand Down
65 changes: 47 additions & 18 deletions agents/otlp/src/otlp_metrics.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// This header needs to be included before any other grpc header otherwise there
// will be a compilation error because of abseil.
// Refs: https://github.com/open-telemetry/opentelemetry-cpp/blob/32cd66b62333e84aa8e92a4447e0aa667b6735e5/examples/otlp/README.md#additional-notes-regarding-abseil-library
#include "opentelemetry/exporters/otlp/otlp_grpc_metric_exporter.h"
#include "otlp_metrics.h"
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "otlp_agent.h"
#include "opentelemetry/exporters/otlp/otlp_environment.h"
#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h"
#include "opentelemetry/trace/semantic_conventions.h"
#include "opentelemetry/sdk/metrics/data/metric_data.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
Expand Down Expand Up @@ -29,6 +35,9 @@ using opentelemetry::sdk::metrics::ResourceMetrics;
using opentelemetry::sdk::metrics::ScopeMetrics;
using opentelemetry::sdk::metrics::SumPointData;
using opentelemetry::sdk::metrics::ValueType;
using opentelemetry::v1::exporter::otlp::GetOtlpDefaultHttpMetricsProtocol;
using opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporter;
using opentelemetry::v1::exporter::otlp::OtlpGrpcMetricExporterOptions;
using opentelemetry::v1::exporter::otlp::OtlpHttpMetricExporter;
using opentelemetry::v1::exporter::otlp::OtlpHttpMetricExporterOptions;
using opentelemetry::v1::trace::SemanticConventions::kThreadId;
Expand All @@ -47,21 +56,44 @@ static std::vector<std::string> discarded_metrics = {
"thread_id", "timestamp"
};

OTLPMetrics::OTLPMetrics(uv_loop_t* loop, const OTLPAgent& agent):
start_(duration_cast<time_point::duration>(
microseconds(static_cast<uint64_t>(
performance::performance_process_start_timestamp)))),
agent_(agent) {
const std::string prot = GetOtlpDefaultHttpMetricsProtocol();
if (prot == "grpc") {
OtlpGrpcMetricExporterOptions opts;
otlp_metric_exporter_ = std::make_unique<OtlpGrpcMetricExporter>(opts);
} else {
OtlpHttpMetricExporterOptions opts;
opts.console_debug = true;
otlp_metric_exporter_ = std::make_unique<OtlpHttpMetricExporter>(opts);
}
}

OTLPMetrics::OTLPMetrics(uv_loop_t* loop,
const std::string& url,
const std::string& key,
bool is_http,
const OTLPAgent& agent):
key_(key),
url_(url),
start_(duration_cast<time_point::duration>(
microseconds(static_cast<uint64_t>(
performance::performance_process_start_timestamp)))),
agent_(agent) {
OtlpHttpMetricExporterOptions opts;
opts.url = url + "/v1/metrics";
opts.content_type = HttpRequestContentType::kBinary;
opts.console_debug = true;
otlp_http_metric_exporter_ = std::make_unique<OtlpHttpMetricExporter>(opts);
if (is_http) {
OtlpHttpMetricExporterOptions opts;
opts.url = url + "/v1/metrics";
opts.content_type = HttpRequestContentType::kBinary;
opts.console_debug = true;
otlp_metric_exporter_ = std::make_unique<OtlpHttpMetricExporter>(opts);
} else {
OtlpGrpcMetricExporterOptions opts;
opts.endpoint = url + "/v1/metrics";
otlp_metric_exporter_ = std::make_unique<OtlpGrpcMetricExporter>(opts);
}
}

/*virtual*/
Expand Down Expand Up @@ -163,18 +195,15 @@ NSOLID_PROCESS_METRICS_DOUBLE(V)

data.scope_metric_data_ =
std::vector<ScopeMetrics>{{agent_.scope_.get(), metrics}};
auto result = otlp_http_metric_exporter_->Export(data);
auto result = otlp_metric_exporter_->Export(data);
Debug("# ProcessMetrics Exported. Result: %d\n", static_cast<int>(result));
}

// NOLINTNEXTLINE(runtime/references)
void OTLPMetrics::got_env_metrics(std::vector<MetricData>& metrics,
const ThreadMetricsStor& stor,
const ThreadMetricsStor& prev_stor,
double loop_start) {
time_point start{
duration_cast<time_point::duration>(
microseconds(static_cast<uint64_t>(loop_start)))};
static void got_env_metrics(std::vector<MetricData>& metrics,
const ThreadMetricsStor& stor,
const ThreadMetricsStor& prev_stor,
time_point start) {
time_point end{
duration_cast<time_point::duration>(
milliseconds(static_cast<uint64_t>(stor.timestamp)))};
Expand Down Expand Up @@ -207,12 +236,12 @@ void OTLPMetrics::got_env_metrics(std::vector<MetricData>& metrics,
switch (MetricsType::MType) { \
case MetricsType::ECounter: \
{ \
add_counter(metrics, start_, end, #CName, Unit, type, value, attrs); \
add_counter(metrics, start, end, #CName, Unit, type, value, attrs); \
} \
break; \
case MetricsType::EGauge: \
case MetricsType::EGauge: \
{ \
add_gauge(metrics, start_, end, #CName, Unit, type, value, attrs); \
add_gauge(metrics, start, end, #CName, Unit, type, value, attrs); \
} \
break; \
default: \
Expand All @@ -232,12 +261,12 @@ void OTLPMetrics::got_thr_metrics(
std::vector<MetricData> metrics;

for (const auto& tm : thr_metrics) {
got_env_metrics(metrics, tm.stor, tm.prev_stor, tm.loop_start);
got_env_metrics(metrics, tm.stor, tm.prev_stor, start_);
}

data.scope_metric_data_ =
std::vector<ScopeMetrics>{{agent_.scope_.get(), metrics}};
auto result = otlp_http_metric_exporter_->Export(data);
auto result = otlp_metric_exporter_->Export(data);
Debug("# ThreadMetrics Exported. Result: %d\n", static_cast<int>(result));
}
} // namespace otlp
Expand Down
34 changes: 21 additions & 13 deletions agents/otlp/src/otlp_metrics.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
#ifndef AGENTS_OTLP_SRC_OTLP_METRICS_H_
#define AGENTS_OTLP_SRC_OTLP_METRICS_H_

// NOLINTNEXTLINE(build/c++11)
#include <chrono>

#include "metrics_exporter.h"
#include "opentelemetry/exporters/otlp/otlp_http_metric_exporter.h"
#include "opentelemetry/version.h"

// Class pre-declaration
OPENTELEMETRY_BEGIN_NAMESPACE
namespace sdk {
namespace metrics {
class PushMetricExporter;
}
}
OPENTELEMETRY_END_NAMESPACE

namespace node {
namespace nsolid {
Expand All @@ -14,10 +25,13 @@ class OTLPAgent;

class OTLPMetrics final: public MetricsExporter {
public:
OTLPMetrics(uv_loop_t* loop,
const std::string& url,
const std::string& key,
const OTLPAgent& agent);
explicit OTLPMetrics(uv_loop_t* loop,
const OTLPAgent& agent);
explicit OTLPMetrics(uv_loop_t* loop,
const std::string& url,
const std::string& key,
bool is_http,
const OTLPAgent& agent);
virtual ~OTLPMetrics();

virtual void got_proc_metrics(const ProcessMetrics::MetricsStor& stor,
Expand All @@ -27,14 +41,8 @@ class OTLPMetrics final: public MetricsExporter {
const std::vector<MetricsExporter::ThrMetricsStor>&);

private:
// NOLINTNEXTLINE(runtime/references)
void got_env_metrics(std::vector<opentelemetry::sdk::metrics::MetricData>& m,
const ThreadMetrics::MetricsStor& stor,
const ThreadMetrics::MetricsStor& prev_stor,
double loop_start);

std::unique_ptr<opentelemetry::v1::exporter::otlp::OtlpHttpMetricExporter>
otlp_http_metric_exporter_;
std::unique_ptr<OPENTELEMETRY_NAMESPACE::sdk::metrics::PushMetricExporter>
otlp_metric_exporter_;
std::string key_;
std::string url_;
std::chrono::system_clock::time_point start_;
Expand Down
12 changes: 0 additions & 12 deletions agents/statsd/src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,6 @@ static void Send(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(StatsDAgent::Inst()->send(sv, full_size));
}

static void Start(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(StatsDAgent::Inst()->start());
}

static void Stop(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(StatsDAgent::Inst()->stop());
}

static void Tags(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
std::string tags = StatsDAgent::Inst()->tags();
Expand Down Expand Up @@ -156,9 +148,7 @@ static void RegisterStatusCb(const FunctionCallbackInfo<Value>& args) {
static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Bucket);
registry->Register(Send);
registry->Register(Start);
registry->Register(Status);
registry->Register(Stop);
registry->Register(Tags);
registry->Register(TcpIp);
registry->Register(UdpIp);
Expand All @@ -178,8 +168,6 @@ void InitStatsDAgent(Local<Object> exports,

NODE_SET_METHOD(exports, "bucket", Bucket);
NODE_SET_METHOD(exports, "send", Send);
NODE_SET_METHOD(exports, "start", Start);
NODE_SET_METHOD(exports, "stop", Stop);
NODE_SET_METHOD(exports, "tags", Tags);
NODE_SET_METHOD(exports, "tcpIp", TcpIp);
NODE_SET_METHOD(exports, "udpIp", UdpIp);
Expand Down
Loading
Loading