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

Commit 8a81b1b

Browse files
authored
Stackdriver exporters: set user agent on gRPC channel. (#234)
1 parent bd876f6 commit 8a81b1b

File tree

7 files changed

+85
-5
lines changed

7 files changed

+85
-5
lines changed

opencensus/common/internal/grpc/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ cc_library(
3131
],
3232
)
3333

34+
cc_library(
35+
name = "with_user_agent",
36+
srcs = ["with_user_agent.cc"],
37+
hdrs = ["with_user_agent.h"],
38+
copts = DEFAULT_COPTS,
39+
deps = [
40+
"//opencensus/common:version",
41+
"@com_github_grpc_grpc//:grpc++",
42+
],
43+
)
44+
3445
# Tests
3546
# ========================================================================= #
3647

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "opencensus/common/internal/grpc/with_user_agent.h"
16+
17+
#include <string>
18+
19+
#include <grpcpp/support/channel_arguments.h>
20+
#include "opencensus/common/version.h"
21+
22+
namespace opencensus {
23+
namespace common {
24+
25+
grpc::ChannelArguments WithUserAgent() {
26+
grpc::ChannelArguments args;
27+
args.SetUserAgentPrefix("opencensus-cpp/" OPENCENSUS_VERSION);
28+
return args;
29+
}
30+
31+
} // namespace common
32+
} // namespace opencensus
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#ifndef OPENCENSUS_COMMON_INTERNAL_GRPC_WITH_USER_AGENT_H_
17+
#define OPENCENSUS_COMMON_INTERNAL_GRPC_WITH_USER_AGENT_H_
18+
19+
#include <grpcpp/support/channel_arguments.h>
20+
21+
namespace opencensus {
22+
namespace common {
23+
24+
// Returns grpc::ChannelArguments to prepend the OpenCensus user agent.
25+
grpc::ChannelArguments WithUserAgent();
26+
27+
} // namespace common
28+
} // namespace opencensus
29+
30+
#endif // OPENCENSUS_COMMON_INTERNAL_GRPC_WITH_USER_AGENT_H_

opencensus/exporters/stats/stackdriver/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cc_library(
2828
":stackdriver_utils",
2929
"//google/monitoring/v3:metric_service",
3030
"//opencensus/common/internal/grpc:status",
31+
"//opencensus/common/internal/grpc:with_user_agent",
3132
"//opencensus/stats",
3233
"@com_github_grpc_grpc//:grpc++",
3334
"@com_google_absl//absl/base",

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "google/monitoring/v3/metric_service.grpc.pb.h"
3030
#include "google/protobuf/empty.pb.h"
3131
#include "opencensus/common/internal/grpc/status.h"
32+
#include "opencensus/common/internal/grpc/with_user_agent.h"
3233
#include "opencensus/exporters/stats/stackdriver/internal/stackdriver_utils.h"
3334
#include "opencensus/stats/stats.h"
3435

@@ -73,8 +74,10 @@ Handler::Handler(const StackdriverOptions& opts)
7374
: opts_(opts),
7475
project_id_(absl::StrCat(kProjectIdPrefix, opts.project_id)),
7576
stub_(google::monitoring::v3::MetricService::NewStub(
76-
::grpc::CreateChannel(kGoogleStackdriverStatsAddress,
77-
::grpc::GoogleDefaultCredentials()))) {}
77+
::grpc::CreateCustomChannel(kGoogleStackdriverStatsAddress,
78+
::grpc::GoogleDefaultCredentials(),
79+
::opencensus::common::WithUserAgent()))) {
80+
}
7881

7982
void Handler::ExportViewData(
8083
const std::vector<std::pair<opencensus::stats::ViewDescriptor,

opencensus/exporters/trace/stackdriver/BUILD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("//opencensus:copts.bzl", "DEFAULT_COPTS", "TEST_COPTS")
15+
load("//opencensus:copts.bzl", "DEFAULT_COPTS")
1616

1717
licenses(["notice"]) # Apache License 2.0
1818

@@ -31,6 +31,7 @@ cc_library(
3131
"//google/devtools/cloudtrace/v2:tracing_proto",
3232
"//opencensus/common:version",
3333
"//opencensus/common/internal/grpc:status",
34+
"//opencensus/common/internal/grpc:with_user_agent",
3435
"//opencensus/trace",
3536
"@com_github_grpc_grpc//:grpc++",
3637
"@com_google_absl//absl/base:core_headers",

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "absl/time/clock.h"
2525
#include "google/devtools/cloudtrace/v2/tracing.grpc.pb.h"
2626
#include "opencensus/common/internal/grpc/status.h"
27+
#include "opencensus/common/internal/grpc/with_user_agent.h"
2728
#include "opencensus/common/version.h"
2829
#include "opencensus/trace/exporter/span_data.h"
2930
#include "opencensus/trace/exporter/span_exporter.h"
@@ -271,8 +272,9 @@ void Handler::Export(
271272

272273
// static
273274
void StackdriverExporter::Register(const StackdriverOptions& opts) {
274-
auto creds = grpc::GoogleDefaultCredentials();
275-
auto channel = ::grpc::CreateChannel(kGoogleStackdriverTraceAddress, creds);
275+
auto channel = ::grpc::CreateCustomChannel(
276+
kGoogleStackdriverTraceAddress, ::grpc::GoogleDefaultCredentials(),
277+
::opencensus::common::WithUserAgent());
276278
::opencensus::trace::exporter::SpanExporter::RegisterHandler(
277279
absl::make_unique<Handler>(opts, channel));
278280
}

0 commit comments

Comments
 (0)