diff --git a/exporters/statsd/include/opentelemetry/exporters/statsd/UnixDomainTransport.h b/exporters/statsd/include/opentelemetry/exporters/statsd/UnixDomainTransport.h new file mode 100644 index 000000000..d2b614e7a --- /dev/null +++ b/exporters/statsd/include/opentelemetry/exporters/statsd/UnixDomainTransport.h @@ -0,0 +1,75 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 +#pragma once + +#include "statsd_common.h" +#include "opentelemetry/version.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter { +namespace statsd { +namespace metrics { + + +class UnixDomainClient +{ + +UnixDomainClient(std::string path): socket_path_(path){} +bool Initialize() +{ + server_socket_fd_ = socket(AF_UNIX, SOCK_STREAM, 0); + if (server_socket_fd_ == -1) { + // log error + return false; + } + sockaddr_un remote; + remote.sun_family = AF_UNIX; + strncpy_s(remote.sun_path, sizeof(remote.sun_path), socket_path_.c_str(), + sizeof(remote.sun_path)); + if (connect(server_socket_fd_, (sockaddr*)&remote, strlen(remote.sun_path) + sizeof(remote.sun_family)) == -1) { + //connect failed + ::close(server_socket_fd_); + server_socket_fd_ = 0; + return false; + } + //success + return true; +} + +bool Send(const char* data, uint32_t size) +{ + auto bytes_left = size; + sizt_t num_bytes_sent; + while ((bytes_left > 0) && ((num_bytes_sent = ::send(server_socket_fd, buf, bytes_left, MSG_NOSIGNAL)) > 0)) { + bytes_left -= num_bytes_sent; + buf += num_bytes_sent; + } + return (bytes_left == 0); // success if all bytes sent +} + +~UnixDomainClient() +{ + if (server_socket_fd_) { + ::close(server_socket_fd_); + } +} + +private: +std::string socket_path_; +uint16_t server_socket_fd_ = 0; + +} +} +} +} +OPENTELEMETRY_END_NAMESPACE diff --git a/exporters/statsd/include/opentelemetry/exporters/statsd/exporter.h b/exporters/statsd/include/opentelemetry/exporters/statsd/exporter.h new file mode 100644 index 000000000..0fd93d1d7 --- /dev/null +++ b/exporters/statsd/include/opentelemetry/exporters/statsd/exporter.h @@ -0,0 +1,61 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#pragma once +#ifndef ENABLE_METRICS_PREVIEW + +# include +# include +# include "opentelemetry/common/spin_lock_mutex.h" +# include "opentelemetry/sdk/metrics/data/metric_data.h" +# include "opentelemetry/sdk/metrics/instruments.h" +# include "opentelemetry/sdk/metrics/metric_exporter.h" +# include "opentelemetry/version.h" + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace exporter +{ +namespace metrics +{ +struct StatsdExporterOptions +{ + std::string unixdomain_path = "'/var/run/stats.sock"; +}; + +class StatsdExporter : public opentelemetry::sdk::metrics::MetricExporter +{ +public: + explicit StatsdExporter(const StatsdExporterOptions &options); + + /** + * Export + * @param data metrics data + */ + sdk::common::ExportResult Export(const sdk::metrics::ResourceMetrics &data) noexcept override; + + /** + * Force flush the exporter. + */ + bool ForceFlush( + std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override; + + /** + * Shut down the exporter. + * @param timeout an optional timeout, the default timeout of 0 means that no + * timeout is applied. + * @return return the status of this operation + */ + bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override; + +private: + bool is_shutdown_ = false; + mutable opentelemetry::common::SpinLockMutex lock_; + UnixDomainTransport transport_; + + }; + + +} +} +OPENTELEMETRY_END_NAMESPACE +#endif \ No newline at end of file diff --git a/exporters/statsd/include/opentelemetry/exporters/statsd/statsd_common.h b/exporters/statsd/include/opentelemetry/exporters/statsd/statsd_common.h new file mode 100644 index 000000000..318b253c6 --- /dev/null +++ b/exporters/statsd/include/opentelemetry/exporters/statsd/statsd_common.h @@ -0,0 +1,16 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include +#pragma once + +#ifndef strncpy_s +#define strncpy_s(dest, destsz, src, count) \ + strncpy(dest, src, (destsz <= count) ? destsz : count) +#endif + +#ifndef NO_OTEL_STATSD_DEBUG +#define DEBUG_MSG(...) printf(__VA_ARGS__) +#else +#define DEBUG_MSG(...) +#endif \ No newline at end of file diff --git a/exporters/statsd/src/exporter.cc b/exporters/statsd/src/exporter.cc new file mode 100644 index 000000000..e69de29bb