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

[WIP] Statsd exporter #157

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 <string>

#include <arpa/inet.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <sys/un.h>

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once
#ifndef ENABLE_METRICS_PREVIEW

# include <iostream>
# include <string>
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <string>
#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
Empty file.