This repository was archived by the owner on Aug 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Add instrumentation and expose measurement in prometheus format #210
Open
StevenYCChou
wants to merge
36
commits into
Stackdriver:master
Choose a base branch
from
StevenYCChou:ycchou-prometheus-integration-for-server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 25 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
8ab1570
Expose API server with metrics in prometheus format.
StevenYCChou 2427bce
Fix indentation and line wrapping.
StevenYCChou 7b3f07e
Remove a redundant test.
StevenYCChou 955a061
Make indentation consistent in Makefile.
StevenYCChou 12b4a86
Fix typo.
StevenYCChou 5c065da
Instrument first metric with opencensus-cpp.
StevenYCChou c667388
Makefile change
StevenYCChou cf5f833
remove unused variable.
StevenYCChou b788df1
expand comments on functions in measures.h
StevenYCChou e49c64d
remove declaration on internal variable
StevenYCChou ec5a859
make string declaration consistent
StevenYCChou bbbd851
add comments on why accessing measure in OAuth2 constructor helps
StevenYCChou acac889
Change the place to register view.
StevenYCChou ac7c9ea
Use the right header for prometheus compatible format.
StevenYCChou 5ac289e
Address comments on measurement organization.
StevenYCChou d8b3e37
fix format in test file.
StevenYCChou 93790ed
revert header/constructor because no need anymore.
StevenYCChou 786245f
Wrap opencensus related code into a class.
StevenYCChou c22faac
Move util function into Metrics Class.
StevenYCChou cdfe9c1
remove explicit dependency on prometheus-cpp.
StevenYCChou 7514b25
Clean up prometheus-cpp dependency on gitmodules as well.
StevenYCChou 4f37882
Revert submodules on prometheus-cpp.
StevenYCChou 96b268a
Revert chnage for prometheus-cpp.
StevenYCChou 64929c8
Flush metrics only within one test, and add comments.
StevenYCChou 88830d0
Avoid using string_view for constant string.
StevenYCChou 6ee8ab8
Use constexpr consistently.
StevenYCChou bee8a32
Refactor code, fix tests which is not idempotent.
StevenYCChou 2687728
initialize export related object every time to be safe - because we d…
StevenYCChou 5c6526e
reorder variable definition.
StevenYCChou 546958f
Use constexpr const.
StevenYCChou 775737c
Add metrics tests on GCE Api Request Errors.
StevenYCChou fc935e6
fix format.
StevenYCChou 1fe4649
Fix format on namespace.
StevenYCChou 1099767
Fix typo.
StevenYCChou 1e56d36
Address feedbacks.
StevenYCChou d6c6ac3
Add const.
StevenYCChou 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 hidden or 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
Submodule opencensus-cpp
added at
f44923
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
init-submodules | ||
build-cpp-netlib | ||
build-yaml-cpp | ||
build-opencensus-cpp | ||
metadatad |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,88 @@ | ||
/* | ||
* 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 <absl/strings/string_view.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
|
||
|
||
} // namespace | ||
|
||
ABSL_CONST_INIT const absl::string_view | ||
StevenYCChou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
kGceApiRequestErrors = | ||
"container.googleapis.com/internal/metadata_agent/gce_api_request_errors"; | ||
|
||
void Metrics::RegisterAllViewsForExport() { | ||
// Access metrics used by views to ensure metrics are initalized. | ||
GceApiRequestErrors(); | ||
|
||
// Register all the views for export. | ||
// | ||
// To avoid view registration throwing error, when adding a new view, register | ||
// measures used by this view in the section above. | ||
GceApiRequestErrorsCumulative().RegisterForExport(); | ||
} | ||
|
||
void Metrics::RecordGceApiRequestErrors(int64_t value, | ||
const std::string& method) { | ||
::opencensus::stats::Record( | ||
{{GceApiRequestErrors(), value}}, | ||
{{MethodTagKey(), method}}); | ||
}; | ||
|
||
const ::opencensus::stats::ViewDescriptor& | ||
Metrics::GceApiRequestErrorsCumulative() { | ||
const static ::opencensus::stats::ViewDescriptor descriptor = | ||
::opencensus::stats::ViewDescriptor() | ||
.set_name("gce_api_request_errors") | ||
.set_measure(kGceApiRequestErrors) | ||
.set_aggregation(::opencensus::stats::Aggregation::Count()) | ||
.add_column(MethodTagKey()); | ||
return descriptor; | ||
} | ||
|
||
std::string Metrics::SerializeMetricsToPrometheusTextFormat() { | ||
static const auto* const text_serializer = | ||
new ::prometheus::TextSerializer(); | ||
static auto* const exporter = | ||
new ::opencensus::exporters::stats::PrometheusExporter(); | ||
return text_serializer->Serialize(exporter->Collect()); | ||
} | ||
|
||
::opencensus::stats::MeasureInt64 Metrics::GceApiRequestErrors() { | ||
static const auto measure = | ||
::opencensus::stats::MeasureInt64::Register( | ||
kGceApiRequestErrors, | ||
"Number of API request errors encountered.", | ||
kCount); | ||
return measure; | ||
} | ||
|
||
::opencensus::stats::TagKey Metrics::MethodTagKey() { | ||
static const auto method_tag_key = | ||
::opencensus::stats::TagKey::Register("method"); | ||
return method_tag_key; | ||
} | ||
|
||
} // namespace google |
This file contains hidden or 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,54 @@ | ||
/* | ||
* 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: | ||
// Register all the view descriptors declared above as view for export. | ||
static void RegisterAllViewsForExport(); | ||
|
||
static void RecordGceApiRequestErrors(int64_t value, const std::string& method); | ||
|
||
// View Descriptor accessors. If the view descriptor variable is not | ||
// initialized, these methods will initialize the variable. | ||
static const ::opencensus::stats::ViewDescriptor& GceApiRequestErrorsCumulative(); | ||
|
||
static std::string SerializeMetricsToPrometheusTextFormat(); | ||
|
||
private: | ||
// Measure accessors. If the measure variable is not initialized, these methods | ||
// will initialize the variable. | ||
// | ||
// Reference of measure: https://opencensus.io/stats/measure/ | ||
static ::opencensus::stats::MeasureInt64 GceApiRequestErrors(); | ||
|
||
// Tag key accessors. If the tag key variable is not initialized, these methods | ||
// will initialize the variable. | ||
// | ||
// Reference of measure: https://opencensus.io/tag/key/ | ||
static ::opencensus::stats::TagKey MethodTagKey(); | ||
}; | ||
|
||
} // namespace google | ||
|
||
#endif /* METADATA_AGENT_METRICS_H_ */ |
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.
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.