This repository was archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathstackdriver_exporter.cc
More file actions
187 lines (165 loc) · 6.8 KB
/
stackdriver_exporter.cc
File metadata and controls
187 lines (165 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2018, OpenCensus 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 "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <memory>
#include <vector>
#include <grpcpp/grpcpp.h>
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "google/monitoring/v3/metric_service.grpc.pb.h"
#include "google/protobuf/empty.pb.h"
#include "opencensus/common/internal/grpc/status.h"
#include "opencensus/common/internal/grpc/with_user_agent.h"
#include "opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.h"
#include "opencensus/stats/stats.h"
namespace opencensus {
namespace exporters {
namespace stats {
namespace {
constexpr char kGoogleStackdriverStatsAddress[] = "monitoring.googleapis.com";
constexpr char kProjectIdPrefix[] = "projects/";
// Stackdriver limits a single CreateTimeSeries request to 200 series.
constexpr int kTimeSeriesBatchSize = 200;
class Handler : public ::opencensus::stats::StatsExporter::Handler {
public:
explicit Handler(const StackdriverOptions& opts);
void ExportViewData(
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
opencensus::stats::ViewData>>& data)
LOCKS_EXCLUDED(mu_) override;
private:
// Registers 'descriptor' with Stackdriver if no view by that name has been
// registered by this, and adds it to registered_descriptors_ if successful.
// Returns true if the view has already been registered or registration is
// successful, and false if the registration fails or the name has already
// been registered with different parameters.
bool MaybeRegisterView(const opencensus::stats::ViewDescriptor& descriptor)
EXCLUSIVE_LOCKS_REQUIRED(mu_);
const StackdriverOptions opts_;
const std::string project_id_;
const std::unique_ptr<google::monitoring::v3::MetricService::Stub> stub_;
mutable absl::Mutex mu_;
std::unordered_map<std::string, opencensus::stats::ViewDescriptor>
registered_descriptors_ GUARDED_BY(mu_);
};
Handler::Handler(const StackdriverOptions& opts)
: opts_(opts),
project_id_(absl::StrCat(kProjectIdPrefix, opts.project_id)),
stub_(google::monitoring::v3::MetricService::NewStub(
::grpc::CreateCustomChannel(kGoogleStackdriverStatsAddress,
::grpc::GoogleDefaultCredentials(),
::opencensus::common::WithUserAgent()))) {
}
void Handler::ExportViewData(
const std::vector<std::pair<opencensus::stats::ViewDescriptor,
opencensus::stats::ViewData>>& data) {
// TODO: refactor to avoid copying the timeseries.
absl::MutexLock l(&mu_);
std::vector<google::monitoring::v3::TimeSeries> time_series;
for (const auto& datum : data) {
if (!MaybeRegisterView(datum.first)) {
continue;
}
const auto view_time_series =
MakeTimeSeries(datum.first, datum.second, opts_.opencensus_task);
time_series.insert(time_series.end(), view_time_series.begin(),
view_time_series.end());
}
const int num_rpcs =
ceil(static_cast<double>(time_series.size()) / kTimeSeriesBatchSize);
std::vector<grpc::Status> status(num_rpcs);
std::vector<grpc::ClientContext> ctx(num_rpcs);
// We can safely re-use an empty response--it is never updated.
google::protobuf::Empty response;
grpc::CompletionQueue cq;
for (int rpc_index = 0; rpc_index < num_rpcs; ++rpc_index) {
auto request = google::monitoring::v3::CreateTimeSeriesRequest();
request.set_name(project_id_);
const int batch_end = std::min(static_cast<int>(time_series.size()),
(rpc_index + 1) * kTimeSeriesBatchSize);
for (int i = rpc_index * kTimeSeriesBatchSize; i < batch_end; ++i) {
*request.add_time_series() = time_series[i];
};
ctx[rpc_index].set_deadline(
absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
auto rpc(stub_->AsyncCreateTimeSeries(&ctx[rpc_index], request, &cq));
rpc->Finish(&response, &status[rpc_index], (void*)(uintptr_t)rpc_index);
}
cq.Shutdown();
void* tag;
bool ok;
while (cq.Next(&tag, &ok)) {
if (ok) {
const auto& s = status[(uintptr_t)tag];
if (!s.ok()) {
std::cerr << "CreateTimeSeries request failed: "
<< opencensus::common::ToString(s) << "\n";
}
}
}
}
bool Handler::MaybeRegisterView(
const opencensus::stats::ViewDescriptor& descriptor) {
const auto& it = registered_descriptors_.find(descriptor.name());
if (it != registered_descriptors_.end()) {
if (it->second != descriptor) {
std::cerr << "Not exporting altered view: " << descriptor.DebugString()
<< "\nAlready registered as: " << it->second.DebugString()
<< "\n";
return false;
}
return true;
}
auto request = google::monitoring::v3::CreateMetricDescriptorRequest();
request.set_name(project_id_);
SetMetricDescriptor(project_id_, descriptor,
request.mutable_metric_descriptor());
::grpc::ClientContext context;
context.set_deadline(absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
google::api::MetricDescriptor response;
::grpc::Status status =
stub_->CreateMetricDescriptor(&context, request, &response);
if (!status.ok()) {
std::cerr << "CreateMetricDescriptor request failed: "
<< opencensus::common::ToString(status) << "\n";
return false;
}
registered_descriptors_.emplace_hint(it, descriptor.name(), descriptor);
return true;
}
} // namespace
// static
grpc::Status StackdriverExporter::Register(const StackdriverOptions& opts) {
opencensus::stats::StatsExporter::RegisterPushHandler(
absl::WrapUnique(new Handler(opts)));
// TODO: Propagate errors to caller. For now we just return OK.
return grpc::Status::OK;
}
// static, DEPRECATED
void StackdriverExporter::Register(absl::string_view project_id,
absl::string_view opencensus_task) {
StackdriverOptions opts;
opts.project_id = std::string(project_id);
opts.opencensus_task = std::string(opencensus_task);
Register(opts);
}
} // namespace stats
} // namespace exporters
} // namespace opencensus