Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
connection_options.cc
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
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 
16 #include "google/cloud/spanner/internal/compiler_info.h"
17 #include "google/cloud/internal/getenv.h"
18 
19 namespace google {
20 namespace cloud {
21 namespace spanner {
22 inline namespace SPANNER_CLIENT_NS {
23 
24 std::string ConnectionOptionsTraits::default_endpoint() {
25  auto default_endpoint = google::cloud::internal::GetEnv(
26  "GOOGLE_CLOUD_CPP_SPANNER_DEFAULT_ENDPOINT");
27  return default_endpoint.has_value() ? *default_endpoint
28  : "spanner.googleapis.com";
29 }
30 
31 std::string ConnectionOptionsTraits::user_agent_prefix() {
32  return "gcloud-cpp/" + google::cloud::spanner::VersionString() + " (" +
33  internal::CompilerId() + "-" + internal::CompilerVersion() + "; " +
34  internal::CompilerFeatures() + ")";
35 }
36 
37 int ConnectionOptionsTraits::default_num_channels() { return 4; }
38 
39 namespace internal {
40 
41 // Override connection endpoint and credentials with values appropriate for
42 // an emulated backend. This should be done after any user code that could
43 // also override the default values (i.e., immediately before establishing
44 // the connection).
45 ConnectionOptions EmulatorOverrides(ConnectionOptions options) {
46  auto emulator_addr = google::cloud::internal::GetEnv("SPANNER_EMULATOR_HOST");
47  if (emulator_addr.has_value()) {
48  options.set_endpoint(*emulator_addr)
49  .set_credentials(grpc::InsecureChannelCredentials());
50  }
51  return options;
52 }
53 
54 } // namespace internal
55 
56 } // namespace SPANNER_CLIENT_NS
57 } // namespace spanner
58 } // namespace cloud
59 } // namespace google
std::string VersionString()
The version as a string, in MAJOR.MINOR.PATCH+gitrev format.
Definition: version.cc:24
Contains all the Cloud Spanner C++ client types and functions.
google::cloud::ConnectionOptions< ConnectionOptionsTraits > ConnectionOptions
The options for Cloud Spanner connections.
#define SPANNER_CLIENT_NS
Definition: version.h:22