Skip to content

Commit 3050dfd

Browse files
committed
Add impl of statsd
1 parent 4eac619 commit 3050dfd

File tree

5 files changed

+110
-416
lines changed

5 files changed

+110
-416
lines changed

exporters/statsd/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# statsd Exporter for OpenTelemetry C++
22

3-
## Installation
3+
## SPEC
4+
Check [metric-instrument-optional-refinements](https://github.com/open-telemetry/opentelemetry-specification/blob/main/oteps/metrics/0088-metric-instrument-optional-refinements.md#statsd) for what we plan to support.
5+
6+
## Installation
7+
todo

exporters/statsd/include/opentelemetry/exporters/statsd/metrics/data_transport.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#pragma once
55

6+
#include <string>
67
#include "opentelemetry/version.h"
78

89
#include <vector>
@@ -12,21 +13,35 @@ namespace exporter {
1213
namespace statsd {
1314
namespace metrics {
1415

15-
// These enums are defined in
16-
// file: test/decoder/ifx_metrics_bin.ksy (enum metric_event_type)
17-
enum class MetricsEventType : uint16_t {
18-
Uint64Metric = 50,
19-
DoubleScaledToLongMetric = 51,
20-
BatchMetric = 52,
21-
ExternallyAggregatedUlongMetric = 53,
22-
ExternallyAggregatedDoubleMetric = 54,
23-
DoubleMetric = 55,
24-
ExternallyAggregatedUlongDistributionMetric = 56,
25-
ExternallyAggregatedDoubleDistributionMetric = 57,
26-
ExternallyAggregatedDoubleScaledToLongDistributionMetric = 58,
27-
Undefined = 100
16+
// Metrics Type defined in Statsd Protocol
17+
// Other statsd metric types, such as c ("c"ounter), t ("t"imer), m ("m"eters), h ("h"istograms), etc are not supported, typically because such functionality is implemented differently in MDM.
18+
enum class MetricsEventType {
19+
// g ("g"auge) is a standard metric with a single 64-bit integer value; <value> is an integer number.
20+
Gauge,
21+
22+
// s ("s"caled fixed-point number) is used to represent fixed-point values (such as CPU usages, load averages, fractions, etc).
23+
// Precision for a fixed-point metric is determined by a precision setting in per-metric setup. <value> can be any fractional number, but note that the actual value would in Geneva Metrics be truncated to a specified precision and still stored in 64-bit container.
24+
ScaledFixedPointNumber,
25+
26+
// f ("f"loating point number) is used to represent floating point numbers which vary a lot in its magnitude and thus warrant storage in a mantissa + expontent format (such as physical quantities, results of complex calculations, etc). <value> can be any fractional number, which would be stored as double precision IEEE 754 float.
27+
FloatingPointNumber,
28+
29+
Unknown
2830
};
2931

32+
inline std::string MetricsEventTypeToString(MetricsEventType type) {
33+
switch (type) {
34+
case MetricsEventType::Gauge:
35+
return "g";
36+
case MetricsEventType::ScaledFixedPointNumber:
37+
return "s";
38+
case MetricsEventType::FloatingPointNumber:
39+
return "f";
40+
default:
41+
return "";
42+
}
43+
}
44+
3045
class DataTransport {
3146
public:
3247
virtual bool Connect() noexcept = 0;

exporters/statsd/include/opentelemetry/exporters/statsd/metrics/exporter.h

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,10 @@ class Exporter final : public opentelemetry::sdk::metrics::PushMetricExporter {
7070
mutable opentelemetry::common::SpinLockMutex lock_;
7171
std::unique_ptr<DataTransport> data_transport_;
7272

73-
// metrics storage
74-
char buffer_[kBufferSize];
75-
76-
size_t SerializeNonHistogramMetrics(sdk::metrics::AggregationType,
77-
MetricsEventType,
78-
const sdk::metrics::ValueType &,
79-
common::SystemTimestamp,
80-
const std::string &,
81-
const sdk::metrics::PointAttributes &);
82-
size_t SerializeHistogramMetrics(
83-
sdk::metrics::AggregationType, MetricsEventType, uint64_t,
84-
const sdk::metrics::ValueType &, const sdk::metrics::ValueType &,
85-
const sdk::metrics::ValueType &, const std::vector<double> &boundaries,
86-
const std::vector<uint64_t> &counts, common::SystemTimestamp,
87-
const std::string &, const sdk::metrics::PointAttributes &);
88-
};
89-
90-
template <class T>
91-
static void SerializeInt(char *buffer, size_t &index, T value) {
92-
*(reinterpret_cast<T *>(buffer + index)) = value;
93-
index += sizeof(T);
94-
}
73+
void SendMetrics(std::string metric_name, MetricsEventType type,
74+
ValueType value) noexcept;
9575

96-
static void SerializeString(char *buffer, size_t &index,
97-
const std::string &str) {
98-
auto size = str.size();
99-
SerializeInt<uint16_t>(buffer, index, static_cast<uint16_t>(size));
100-
if (size > 0) {
101-
memcpy(buffer + index, str.c_str(), size);
102-
}
103-
index += size;
104-
}
76+
};
10577

10678
static std::string AttributeValueToString(
10779
const opentelemetry::sdk::common::OwnedAttributeValue &value) {

exporters/statsd/include/opentelemetry/exporters/statsd/metrics/exporter_options.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ namespace exporter {
1212
namespace statsd {
1313
namespace metrics {
1414

15+
// Tags are used to represent dimensions in the metric data.
16+
// We support for different style of tags
17+
enum class TagStyle {
18+
// For Librato-style tags, they must be appended to the metric name with a delimiting #,
19+
// as so: "foo#tag1=bar,tag2=baz:100|c"
20+
Librato,
21+
22+
// For Geneva-style tags, they must be serialized in JSON format,
23+
// and appended to the metric name as a JSON object, with the metric name and account and namespace as keys,
24+
// as so: { "Metric": "TestMetricName", "Account": "TestAccount", "Namespace": "TestNamespace", "Dims": { "Dimension1Name": "Dim1Value", "Dimension2Name": "Dim2Value" }, "TS": "2018-01-01T01:02:03.004" }
25+
Geneva,
26+
27+
// The simplest way is not to use tags at all, and just send the metric name and value.
28+
// This is the default style.
29+
None
30+
};
31+
1532
struct ExporterOptions {
1633
// clang-format off
1734
/*
@@ -23,7 +40,10 @@ struct ExporterOptions {
2340
*/
2441
// clang-format off
2542
std::string connection_string;
43+
2644
const std::map<std::string, std::string> prepopulated_dimensions;
45+
46+
TagStyle tag_style{TagStyle::None};
2747
};
2848
} // namespace metrics
2949
} // namespace statsd

0 commit comments

Comments
 (0)