-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathotlp_metrics.cc
More file actions
137 lines (123 loc) · 5.07 KB
/
Copy pathotlp_metrics.cc
File metadata and controls
137 lines (123 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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 "otlp_common.h"
#include "debug_utils-inl.h"
#include "env-inl.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"
#include <algorithm>
// NOLINTNEXTLINE(build/c++11)
#include <chrono>
#include "asserts-cpp/asserts.h"
using ProcessMetricsStor = node::nsolid::ProcessMetrics::MetricsStor;
using ThreadMetricsStor = node::nsolid::ThreadMetrics::MetricsStor;
using std::chrono::duration_cast;
using time_point = std::chrono::system_clock::time_point;
using std::chrono::microseconds;
using std::chrono::milliseconds;
using opentelemetry::exporter::otlp::HttpRequestContentType;
using opentelemetry::sdk::instrumentationscope::InstrumentationScope;
using opentelemetry::sdk::metrics::AggregationTemporality;
using opentelemetry::sdk::metrics::MetricData;
using opentelemetry::sdk::metrics::InstrumentDescriptor;
using opentelemetry::sdk::metrics::InstrumentType;
using opentelemetry::sdk::metrics::InstrumentValueType;
using opentelemetry::sdk::metrics::PointAttributes;
using opentelemetry::sdk::metrics::PointDataAttributes;
using opentelemetry::sdk::metrics::ResourceMetrics;
using opentelemetry::sdk::metrics::ScopeMetrics;
using opentelemetry::sdk::metrics::SumPointData;
using opentelemetry::sdk::metrics::ValueType;
using opentelemetry::sdk::resource::Resource;
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;
namespace node {
namespace nsolid {
namespace otlp {
template <typename... Args>
inline void Debug(Args&&... args) {
per_process::Debug(DebugCategory::NSOLID_OTLP_AGENT,
std::forward<Args>(args)...);
}
static std::vector<std::string> discarded_metrics = {
"thread_id", "timestamp"
};
OTLPMetrics::OTLPMetrics(uv_loop_t* loop,
InstrumentationScope* scope,
const std::string& cacert):
scope_(scope) {
const std::string prot = GetOtlpDefaultHttpMetricsProtocol();
if (prot == "grpc") {
OtlpGrpcMetricExporterOptions opts;
opts.ssl_credentials_cacert_as_string = cacert;
otlp_metric_exporter_ = std::make_unique<OtlpGrpcMetricExporter>(opts);
} else {
OtlpHttpMetricExporterOptions opts;
opts.console_debug = true;
opts.ssl_ca_cert_string = cacert;
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,
InstrumentationScope* scope,
const std::string& cacert):
scope_(scope),
key_(key),
url_(url) {
if (is_http) {
OtlpHttpMetricExporterOptions opts;
opts.url = url + "/v1/metrics";
opts.content_type = HttpRequestContentType::kBinary;
opts.console_debug = true;
opts.ssl_ca_cert_string = cacert;
otlp_metric_exporter_ = std::make_unique<OtlpHttpMetricExporter>(opts);
} else {
OtlpGrpcMetricExporterOptions opts;
opts.endpoint = url + "/v1/metrics";
opts.ssl_credentials_cacert_as_string = cacert;
otlp_metric_exporter_ = std::make_unique<OtlpGrpcMetricExporter>(opts);
}
}
/*virtual*/
OTLPMetrics::~OTLPMetrics() {
}
/*virtual*/
void OTLPMetrics::got_proc_metrics(const ProcessMetricsStor& stor,
const ProcessMetricsStor& prev_stor) {
std::vector<MetricData> metrics;
fill_proc_metrics(metrics, stor, prev_stor);
ResourceMetrics data;
data.resource_ = GetResource();
data.scope_metric_data_ = std::vector<ScopeMetrics>{{scope_, metrics}};
auto result = otlp_metric_exporter_->Export(data);
Debug("# ProcessMetrics Exported. Result: %d\n", static_cast<int>(result));
}
/*virtual*/
void OTLPMetrics::got_thr_metrics(
const std::vector<MetricsExporter::ThrMetricsStor>& thr_metrics) {
ResourceMetrics data;
data.resource_ = GetResource();
std::vector<MetricData> metrics;
for (const auto& tm : thr_metrics) {
fill_env_metrics(metrics, tm.stor);
}
data.scope_metric_data_ =
std::vector<ScopeMetrics>{{scope_, metrics}};
auto result = otlp_metric_exporter_->Export(data);
Debug("# ThreadMetrics Exported. Result: %d\n", static_cast<int>(result));
}
} // namespace otlp
} // namespace nsolid
} // namespace node