-
Notifications
You must be signed in to change notification settings - Fork 11
Add instrumentation and expose measurement in prometheus format #210
base: master
Are you sure you want to change the base?
Changes from all commits
8ab1570
2427bce
7b3f07e
955a061
12b4a86
5c065da
c667388
cf5f833
b788df1
e49c64d
ec5a859
bbbd851
acac889
ac7c9ea
5ac289e
d8b3e37
93790ed
786245f
c22faac
cdfe9c1
7514b25
4f37882
96b268a
64929c8
88830d0
6ee8ab8
bee8a32
2687728
5c6526e
546958f
775737c
fc935e6
1fe4649
1099767
1e56d36
d6c6ac3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
init-submodules | ||
build-cpp-netlib | ||
build-yaml-cpp | ||
build-opencensus-cpp | ||
metadatad |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* 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 "metrics.h" | ||
|
||
#include <opencensus/exporters/stats/prometheus/prometheus_exporter.h> | ||
#include <opencensus/stats/stats.h> | ||
#include <prometheus/text_serializer.h> | ||
|
||
namespace google { | ||
|
||
namespace { | ||
|
||
constexpr const char kCount[] = "1"; | ||
StevenYCChou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
::opencensus::stats::TagKey MethodTagKey() { | ||
static const auto method_tag_key = | ||
::opencensus::stats::TagKey::Register("method"); | ||
return method_tag_key; | ||
} | ||
|
||
} // namespace | ||
|
||
const char Metrics::kGceApiRequestErrors[] = | ||
"container.googleapis.com/internal/metadata_agent/gce_api_request_errors"; | ||
|
||
void Metrics::RecordGceApiRequestErrors(int64_t value, | ||
const std::string& method) { | ||
::opencensus::stats::Record( | ||
{{GceApiRequestErrors(), value}}, | ||
{{MethodTagKey(), method}}); | ||
} | ||
|
||
::opencensus::stats::MeasureInt64 Metrics::GceApiRequestErrors() { | ||
static const auto measure = Metrics::GceApiRequestErrorsInitialize(); | ||
return measure; | ||
} | ||
|
||
::opencensus::stats::MeasureInt64 Metrics::GceApiRequestErrorsInitialize() { | ||
const auto measure = | ||
::opencensus::stats::MeasureInt64::Register( | ||
kGceApiRequestErrors, | ||
"Number of API request errors encountered.", | ||
kCount); | ||
Metrics::GceApiRequestErrorsCumulativeViewDescriptor().RegisterForExport(); | ||
return measure; | ||
} | ||
|
||
const ::opencensus::stats::ViewDescriptor | ||
Metrics::GceApiRequestErrorsCumulativeViewDescriptor() { | ||
return ::opencensus::stats::ViewDescriptor() | ||
.set_name(kGceApiRequestErrors) | ||
.set_measure(kGceApiRequestErrors) | ||
.set_aggregation(::opencensus::stats::Aggregation::Count()) | ||
.set_description("The total number of HTTP request errors.") | ||
.add_column(MethodTagKey()); | ||
} | ||
|
||
std::string Metrics::SerializeMetricsToPrometheusTextFormat() { | ||
return ::prometheus::TextSerializer().Serialize( | ||
::opencensus::exporters::stats::PrometheusExporter().Collect()); | ||
} | ||
|
||
} // namespace google |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* 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. | ||
**/ | ||
|
||
#ifndef METADATA_AGENT_METRICS_H_ | ||
#define METADATA_AGENT_METRICS_H_ | ||
|
||
#include <opencensus/stats/stats.h> | ||
#include <string> | ||
|
||
namespace google { | ||
|
||
class Metrics { | ||
public: | ||
static const char kGceApiRequestErrors[]; | ||
|
||
// Record an API request error on a method, e.g. oauth2 is one of the methods. | ||
static void RecordGceApiRequestErrors(int64_t value, const std::string& method); | ||
|
||
// Serialize all the available metrics as prometheus text format. | ||
static std::string SerializeMetricsToPrometheusTextFormat(); | ||
|
||
// View Descriptor accessors. If the view descriptor variable is not | ||
// initialized, these methods will initialize the variable. | ||
static const ::opencensus::stats::ViewDescriptor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be private? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no. I tried to implement In order to create a view from the view descriptor, and run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also note that the standard trick for accessing private functions in tests is to mark them |
||
GceApiRequestErrorsCumulativeViewDescriptor(); | ||
|
||
private: | ||
static ::opencensus::stats::MeasureInt64 GceApiRequestErrorsInitialize(); | ||
static ::opencensus::stats::MeasureInt64 GceApiRequestErrors(); | ||
}; | ||
|
||
} // namespace google | ||
|
||
#endif /* METADATA_AGENT_METRICS_H_ */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,15 +34,15 @@ constexpr const char kDefaultTokenEndpoint[] = | |
class OAuth2 { | ||
public: | ||
OAuth2(const Environment& environment) | ||
: OAuth2(environment, ExpirationImpl<std::chrono::system_clock>::New()) {} | ||
: OAuth2(environment, ExpirationImpl<std::chrono::system_clock>::New()) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did we change the indentation here? Should we just revert this file altogether? |
||
|
||
std::string GetAuthHeaderValue(); | ||
|
||
protected: | ||
OAuth2(const Environment& environment, std::unique_ptr<Expiration> expiration) | ||
: environment_(environment), | ||
token_expiration_(std::move(expiration)), | ||
token_endpoint_(kDefaultTokenEndpoint) {} | ||
: environment_(environment), | ||
token_expiration_(std::move(expiration)), | ||
token_endpoint_(kDefaultTokenEndpoint) {} | ||
|
||
private: | ||
friend class OAuth2Test; | ||
|
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.
I'm always wary of targeting an untagged and unreleased commit. We can fix this later, but just wanted to note this as a concern.
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.
acknowledged. I just follow the pattern we used on other submodules. Once opencensus-cpp offically release their new version, we should check in that specific version. for now, I targeted on a commit which includes all the functionality we need, including prometheus-exporter.