-
Notifications
You must be signed in to change notification settings - Fork 410
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
Add Prometheus Exporter: Step 3 - PrometheusExporter class #263
Closed
Closed
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
38f811a
Add PrometheusExporter header, source, and test files
erichsueh3 140efcd
CI formatting, added copyright headers
erichsueh3 feb7126
Added build files
erichsueh3 5d6fc7e
modified comments to reflect removal of ReturnCodes class, removal of…
erichsueh3 845d6f1
Merge branch 'master' into prom_exporter
erichsueh3 32880b0
minor change to structure of friend class
erichsueh3 61783a7
Merge branch 'prom_exporter' of https://github.com/erichsueh3/opentel…
erichsueh3 b8d8f5a
Merge branch 'master' into prom_exporter
erichsueh3 94af70d
removed assignment of exposer_ to nullptr in testing constructor
erichsueh3 550bb7f
Merge branch 'master' into prom_exporter
erichsueh3 367d6a3
removed usage of release9.0 of Prometheus
erichsueh3 a5e2cfb
BUILD file changes
erichsueh3 6bbb051
changed Prometheus client back to 9.0
erichsueh3 d3d44f3
removed a test for debugging
erichsueh3 8dc5cb1
Revert "removed a test for debugging"
erichsueh3 94f78c8
format
erichsueh3 5938aa8
added MAKE_EXE_LINKER_FLAGS cmake link option
erichsueh3 dc28f84
added -D to flag
erichsueh3 75e23e5
changed -DMAKE_EXE_LINKER_FLAGS to -DCMAKE_EXE_LINKER_FLAGS
erichsueh3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
exporters/prometheus/include/opentelemetry/exporters/prometheus/prometheus_exporter.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "opentelemetry/sdk/metrics/exporter.h" | ||
#include "opentelemetry/sdk/metrics/record.h" | ||
#include "opentelemetry/version.h" | ||
#include "prometheus/exposer.h" | ||
#include "prometheus_collector.h" | ||
|
||
/** | ||
* This class is an implementation of the MetricsExporter interface and | ||
* exports Prometheus metrics data. Functions in this class should be | ||
* called by the Controller in our data pipeline. | ||
*/ | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace exporter | ||
{ | ||
namespace prometheus | ||
{ | ||
class PrometheusExporter : public sdk::metrics::MetricsExporter | ||
{ | ||
public: | ||
/** | ||
* Constructor - binds an exposer and collector to the exporter | ||
* @param address: an address for an exposer that exposes | ||
* an HTTP endpoint for the exporter to connect to | ||
*/ | ||
PrometheusExporter(std::string &address); | ||
|
||
/** | ||
* 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 | ||
*/ | ||
sdk::metrics::ExportResult Export( | ||
const std::vector<sdk::metrics::Record> &records) 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. | ||
*/ | ||
void Shutdown() noexcept; | ||
|
||
/** | ||
* @return: returns a shared_ptr to | ||
* the PrometheusCollector instance | ||
*/ | ||
std::shared_ptr<PrometheusCollector> &GetCollector(); | ||
|
||
/** | ||
* @return: Gets the shutdown status of the exporter | ||
*/ | ||
bool IsShutdown() const; | ||
|
||
private: | ||
/** | ||
* exporter shutdown status | ||
*/ | ||
bool is_shutdown_; | ||
|
||
/** | ||
* Pointer to a | ||
* PrometheusCollector instance | ||
*/ | ||
std::shared_ptr<PrometheusCollector> collector_; | ||
|
||
/** | ||
* Pointer to an | ||
* Exposer instance | ||
*/ | ||
std::unique_ptr<::prometheus::Exposer> exposer_; | ||
|
||
/** | ||
* friend class for testing | ||
*/ | ||
friend class PrometheusExporterTest; | ||
|
||
/** | ||
* PrometheusExporter constructor with no parameters | ||
* Used for testing only | ||
*/ | ||
PrometheusExporter(); | ||
}; | ||
} // namespace prometheus | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "opentelemetry/exporters/prometheus/prometheus_exporter.h" | ||
|
||
OPENTELEMETRY_BEGIN_NAMESPACE | ||
namespace exporter | ||
{ | ||
namespace prometheus | ||
{ | ||
/** | ||
* Constructor - binds an exposer and collector to the exporter | ||
* @param address: an address for an exposer that exposes | ||
* an HTTP endpoint for the exporter to connect to | ||
*/ | ||
PrometheusExporter::PrometheusExporter(std::string &address) : is_shutdown_(false) | ||
{ | ||
exposer_ = std::unique_ptr<::prometheus::Exposer>(new ::prometheus::Exposer{address}); | ||
collector_ = std::shared_ptr<PrometheusCollector>(new PrometheusCollector); | ||
|
||
exposer_->RegisterCollectable(collector_); | ||
} | ||
|
||
/** | ||
* PrometheusExporter constructor with no parameters | ||
* Used for testing only | ||
*/ | ||
PrometheusExporter::PrometheusExporter() : is_shutdown_(false) | ||
{ | ||
collector_ = std::unique_ptr<PrometheusCollector>(new PrometheusCollector); | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
sdk::metrics::ExportResult PrometheusExporter::Export( | ||
const std::vector<sdk::metrics::Record> &records) noexcept | ||
{ | ||
if (is_shutdown_) | ||
{ | ||
return sdk::metrics::ExportResult::kFailure; | ||
} | ||
else if (records.empty()) | ||
{ | ||
return sdk::metrics::ExportResult::kFailureInvalidArgument; | ||
} | ||
else if (collector_->GetCollection().size() + records.size() > collector_->GetMaxCollectionSize()) | ||
{ | ||
return sdk::metrics::ExportResult::kFailureFull; | ||
} | ||
else | ||
{ | ||
collector_->AddMetricData(records); | ||
return sdk::metrics::ExportResult::kSuccess; | ||
} | ||
} | ||
|
||
/** | ||
* 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. | ||
*/ | ||
void PrometheusExporter::Shutdown() noexcept | ||
{ | ||
is_shutdown_ = true; | ||
|
||
collector_->GetCollection().clear(); | ||
} | ||
|
||
/** | ||
* @return: returns a shared_ptr to | ||
* the PrometheusCollector instance | ||
*/ | ||
std::shared_ptr<PrometheusCollector> &PrometheusExporter::GetCollector() | ||
{ | ||
return collector_; | ||
} | ||
|
||
/** | ||
* @return: Gets the shutdown status of the exporter | ||
*/ | ||
bool PrometheusExporter::IsShutdown() const | ||
{ | ||
return is_shutdown_; | ||
} | ||
|
||
} // namespace prometheus | ||
} // namespace exporter | ||
OPENTELEMETRY_END_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is Shutdown called from a different thread? Should there be a lock here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking into it, it seems as though Shutdown() will never be called by the controller in the SDK, since the Metrics Exporter Interface does not require a shutdown function - is there any benefit to keeping this in? If yes, then it seems we might need a lock here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the specification say about exporter shutdowns?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that it is not required for metrics exporters currently, but is required for span exporters. Here is the requirement for span exporters, but looking at the shutdown function in the ostream metrics exporter, it is mentioned but not required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point this might be nice to have. I think Ryan was saying that e.g. Envoy wants to be able to handle SIGHUP by completely shutting everything down, changing the config, and then bringing it back up.
Maybe the right answer is to punt on Shutdown for now and just leave a TODO? Idk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can comment the shutdown function out, and add a TODO comment once the metrics specification adds a requirement for a shutdown?