Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 1d75995

Browse files
author
Alex Amato
authored
Stackdriver stats exporter: add prepare_client_context callback. (#442)
1 parent 04ed021 commit 1d75995

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

opencensus/exporters/stats/stackdriver/internal/stackdriver_exporter.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
#include "opencensus/exporters/stats/stackdriver/stackdriver_exporter.h"
1616

17+
#include <grpcpp/grpcpp.h>
18+
1719
#include <algorithm>
1820
#include <cmath>
1921
#include <cstdint>
2022
#include <memory>
2123
#include <vector>
2224

23-
#include <grpcpp/grpcpp.h>
2425
#include "absl/memory/memory.h"
2526
#include "absl/strings/str_cat.h"
2627
#include "absl/strings/string_view.h"
@@ -156,6 +157,7 @@ void Handler::ExportViewData(
156157
}
157158
ctx[rpc_index].set_deadline(
158159
absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
160+
opts_.prepare_client_context(&ctx[rpc_index]);
159161
auto rpc(opts_.metric_service_stub->AsyncCreateTimeSeries(&ctx[rpc_index],
160162
request, &cq));
161163
rpc->Finish(&response, &status[rpc_index], (void*)(uintptr_t)rpc_index);
@@ -198,6 +200,7 @@ bool Handler::MaybeRegisterView(
198200
::grpc::ClientContext context;
199201
context.set_deadline(absl::ToChronoTime(absl::Now() + opts_.rpc_deadline));
200202
google::api::MetricDescriptor response;
203+
opts_.prepare_client_context(&context);
201204
::grpc::Status status = opts_.metric_service_stub->CreateMetricDescriptor(
202205
&context, request, &response);
203206
if (!status.ok()) {

opencensus/exporters/stats/stackdriver/stackdriver_exporter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ struct StackdriverOptions {
8080
// instead. Useful for testing.
8181
std::unique_ptr<google::monitoring::v3::MetricService::StubInterface>
8282
metric_service_stub;
83+
84+
// Optional: A function which can be used to modify the grpc::ClientContext
85+
// before making API requests. For example, to add custom credentials.
86+
// This function will be called by the exporter before every CreateTimeSeries
87+
// and CreateMetricDescriptor RPC.
88+
// This function will not be called in parallel.
89+
std::function<void(grpc::ClientContext*)> prepare_client_context =
90+
[](grpc::ClientContext*) {};
8391
};
8492

8593
// Exports stats for registered views (see opencensus/stats/stats_exporter.h) to

0 commit comments

Comments
 (0)