Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…p-contrib/tree/main/exporters/prometheus (the push exporter) in this repo
  • Loading branch information
dstanev-atvi committed Feb 15, 2024
1 parent 26033a1 commit f42c42c
Show file tree
Hide file tree
Showing 12 changed files with 1,040 additions and 6 deletions.
7 changes: 5 additions & 2 deletions api/test/trace/propagation/detail/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# SPDX-License-Identifier: Apache-2.0

load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark")
load("//:dll_deps.bzl", "dll_deps", "avoid_dll_lock")

avoid_dll_lock()

cc_test(
name = "hex_test",
Expand All @@ -13,8 +16,8 @@ cc_test(
"test",
"trace",
],
deps = [
deps = dll_deps([
"//api",
"@com_google_googletest//:gtest_main",
],
]),
)
5 changes: 3 additions & 2 deletions exporters/ostream/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ cc_library(

cc_library(
name = "ostream_capture",
testonly = 1,
hdrs = [
"test/ostream_capture.h",
],
tags = ["ostream"],
deps = [
deps = dll_deps([
"//api",
],
]),
)

cc_test(
Expand Down
63 changes: 61 additions & 2 deletions exporters/prometheus/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ cc_library(

cc_library(
name = "prometheus_test_helper",
testonly = 1,
hdrs = [
"test/prometheus_test_helper.h",
],
tags = ["prometheus"],
deps = [
deps = dll_deps([
"//api",
"//sdk:headers",
"//sdk/src/trace",
],
]),
)

cc_test(
Expand Down Expand Up @@ -137,6 +138,64 @@ cc_test(
]),
)

cc_library(
name = "prometheus_push_exporter",
srcs = [
"src/push_exporter.cc",
"src/push_exporter_factory.cc",
],
hdrs = [
"include/opentelemetry/exporters/prometheus/push_exporter.h",
"include/opentelemetry/exporters/prometheus/push_exporter_factory.h",
"include/opentelemetry/exporters/prometheus/push_exporter_options.h",
],
strip_include_prefix = "include",
tags = ["prometheus"],
deps = [
"@com_github_jupp0r_prometheus_cpp//core",
"@com_github_jupp0r_prometheus_cpp//push",
"//api",
"//exporters/prometheus:prometheus_collector",
"//exporters/prometheus:prometheus_exporter_utils",
"//sdk:headers",
"//sdk/src/metrics",
],
)

cc_library(
name = "prometheus_push_test_helper",
testonly = 1,
hdrs = [
"test/prometheus_push_test_helper.h",
],
tags = ["prometheus"],
deps = dll_deps([
"//api",
"//sdk:headers",
"//sdk/src/trace",
]),
)

cc_test(
name = "prometheus_push_exporter_test",
srcs = [
"test/push_exporter_test.cc",
],
tags = [
"prometheus",
"test",
],
deps = dll_deps([
":prometheus_push_exporter",
":prometheus_push_test_helper",
"@com_google_googletest//:gtest_main",
"//api",
"//exporters/prometheus:prometheus_collector",
"//exporters/prometheus:prometheus_exporter_utils",
"//sdk:headers",
]),
)

cc_library(
name = "headers",
hdrs = glob(["include/**/*.h"]),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2023, OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
#endif

#ifndef NOMINMAX
# define NOMINMAX
#endif

#ifdef _WIN32
# include <io.h> // NOLINT
# include <winsock2.h> // NOLINT
#else
# include <unistd.h> // NOLINT
#endif

#include <chrono>
#include <memory>
#include <string>
#include <vector>

#include "opentelemetry/version.h"
#include "prometheus/gateway.h"

#include "opentelemetry/exporters/prometheus/exporter_utils.h"
#include "opentelemetry/exporters/prometheus/push_exporter_options.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/sdk/common/env_variables.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"


OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace metrics
{

class PrometheusPushCollector;

class PrometheusPushExporter : public ::opentelemetry::sdk::metrics::PushMetricExporter
{
public:
/**
* Constructor - binds an exposer and collector to the exporter
* @param options: options for an exposer that exposes
* an HTTP endpoint for the exporter to connect to
*/
explicit PrometheusPushExporter(const PrometheusPushExporterOptions &options);

/**
* Get the AggregationTemporality for Prometheus exporter
*
* @return AggregationTemporality
*/
::opentelemetry::sdk::metrics::AggregationTemporality GetAggregationTemporality(
::opentelemetry::sdk::metrics::InstrumentType instrument_type) const noexcept override;

/**
* Exports a batch of Metric Records.
* @param records: a collection of records to export
* @return: returns a ReturnCode detailing a success, or type of failure
*/
::opentelemetry::sdk::common::ExportResult Export(
const ::opentelemetry::sdk::metrics::ResourceMetrics &data) noexcept override;

/**
* Force flush the exporter.
*/
bool ForceFlush(
std::chrono::microseconds timeout = (std::chrono::microseconds::max)()) noexcept override;

/**
* Shuts down the exporter and does cleanup.
* Since Prometheus is a pull based interface,
* we cannot serve data remaining in the intermediate
* collection to to client an HTTP request being sent,
* so we flush the data.
*/
bool Shutdown(std::chrono::microseconds timeout = std::chrono::microseconds(0)) noexcept override;

/**
* Gets the maximum size of the collection.
*
* @return max collection size
*/
std::size_t GetMaxCollectionSize() const noexcept;

/**
* @return: Gets the shutdown status of the exporter
*/
bool IsShutdown() const;

private:
// The configuration options associated with this exporter.
const PrometheusPushExporterOptions options_;
/**
* exporter shutdown status
*/
bool is_shutdown_;

/**
* Pointer to a
* PrometheusCollector instance
*/
std::shared_ptr<PrometheusPushCollector> collector_;

/**
* Pointer to an
* Gateway instance
*/
std::unique_ptr<::prometheus::Gateway> gateway_;

/**
* friend class for testing
*/
friend class PrometheusPushExporterTest;

/**
* PrometheusPushExporter constructor with no parameters
* Used for testing only
*/
PrometheusPushExporter();
};

} // namespace metrics
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2023, OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <memory>

#include "opentelemetry/version.h"
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace metrics
{

struct PrometheusPushExporterOptions;

/**
* Factory class for PrometheusExporter.
*/
class PrometheusPushExporterFactory
{
public:
/**
* Create a PrometheusExporter using the given options.
*/
static std::unique_ptr<opentelemetry::sdk::metrics::PushMetricExporter> Create(
const PrometheusPushExporterOptions &options);
};

} // namespace metrics
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023, OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstdint>
#include <string>
#include <unordered_map>

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace exporter
{
namespace metrics
{

/**
* Struct to hold Prometheus exporter options.
*/
struct PrometheusPushExporterOptions
{
std::string host;
std::string port;
std::string jobname;
std::unordered_map<std::string, std::string> labels;
std::string username;
std::string password;

std::size_t max_collection_size = 2000;

inline PrometheusPushExporterOptions() noexcept {}
};

} // namespace metrics
} // namespace exporter
OPENTELEMETRY_END_NAMESPACE
Loading

0 comments on commit f42c42c

Please sign in to comment.