Skip to content

Commit

Permalink
feat(spanner): allow custom lib name and version for telemetry purpose (
Browse files Browse the repository at this point in the history
#4762)

* allow custom lib name and version for telemetry purpose

* format docs

* added custom lib name and version prefix

- updated doc and tests

* update test cases for lib name and version
  • Loading branch information
jiren committed Feb 13, 2020
1 parent 9fbc94c commit ade2581
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 17 deletions.
43 changes: 39 additions & 4 deletions google-cloud-spanner/lib/google-cloud-spanner.rb
Expand Up @@ -45,6 +45,20 @@ module Cloud
# @param [Integer] timeout Default timeout to use in requests. Optional.
# @param [Hash] client_config A hash of values to override the default
# behavior of the API client. Optional.
# @param [String] lib_name Library name. This will be added as a prefix
# to the API call tracking header `x-goog-api-client` with provided
# lib version for telemetry. Optional. For example prefix looks like
# `spanner-activerecord/0.0.1 gccl/1.13.1`. Here,
# `spanner-activerecord/0.0.1` is provided custom library name and
# version and `gccl/1.13.1` represents the Cloud Spanner Ruby library
# with version.
# @param [String] lib_version Library version. This will be added as a
# prefix to the API call tracking header `x-goog-api-client` with
# provided lib name for telemetry. Optional. For example prefix look like
# `spanner-activerecord/0.0.1 gccl/1.13.1`. Here,
# `spanner-activerecord/0.0.1` is provided custom library name and
# version and `gccl/1.13.1` represents the Cloud Spanner Ruby library
# with version.
#
# @return [Google::Cloud::Spanner::Project]
#
Expand All @@ -61,10 +75,13 @@ module Cloud
# platform_scope = "https://www.googleapis.com/auth/cloud-platform"
# spanner = gcloud.spanner scope: platform_scope
#
def spanner scope: nil, timeout: nil, client_config: nil
def spanner scope: nil, timeout: nil, client_config: nil, lib_name: nil,
lib_version: nil
Google::Cloud.spanner @project, @keyfile, scope: scope,
timeout: (timeout || @timeout),
client_config: client_config
client_config: client_config,
lib_name: lib_name,
lib_version: lib_version
end

##
Expand Down Expand Up @@ -92,6 +109,20 @@ def spanner scope: nil, timeout: nil, client_config: nil
# @param [Integer] timeout Default timeout to use in requests. Optional.
# @param [Hash] client_config A hash of values to override the default
# behavior of the API client. Optional.
# @param [String] lib_name Library name. This will be added as a prefix
# to the API call tracking header `x-goog-api-client` with provided
# lib version for telemetry. Optional. For example prefix looks like
# `spanner-activerecord/0.0.1 gccl/1.13.1`. Here,
# `spanner-activerecord/0.0.1` is provided custom library name and
# version and `gccl/1.13.1` represents the Cloud Spanner Ruby library
# with version.
# @param [String] lib_version Library version. This will be added as a
# prefix to the API call tracking header `x-goog-api-client` with
# provided lib name for telemetry. Optional. For example prefix look like
# `spanner-activerecord/0.0.1 gccl/1.13.1`. Here,
# `spanner-activerecord/0.0.1` is provided custom library name and
# version and `gccl/1.13.1` represents the Cloud Spanner Ruby library
# with version.
#
# @return [Google::Cloud::Spanner::Project]
#
Expand All @@ -101,12 +132,14 @@ def spanner scope: nil, timeout: nil, client_config: nil
# spanner = Google::Cloud.spanner
#
def self.spanner project_id = nil, credentials = nil, scope: nil,
timeout: nil, client_config: nil
timeout: nil, client_config: nil, lib_name: nil,
lib_version: nil
require "google/cloud/spanner"
Google::Cloud::Spanner.new project_id: project_id,
credentials: credentials,
scope: scope, timeout: timeout,
client_config: client_config
client_config: client_config,
lib_name: lib_name, lib_version: lib_version
end
end
end
Expand Down Expand Up @@ -137,4 +170,6 @@ def self.spanner project_id = nil, credentials = nil, scope: nil,
config.add_field! :client_config, nil, match: Hash
config.add_field! :endpoint, nil, match: String
config.add_field! :emulator_host, default_emulator, match: String, allow_nil: true
config.add_field! :lib_name, nil, match: String, allow_nil: true
config.add_field! :lib_version, nil, match: String, allow_nil: true
end
31 changes: 27 additions & 4 deletions google-cloud-spanner/lib/google/cloud/spanner.rb
Expand Up @@ -34,7 +34,7 @@ module Cloud
# See {file:OVERVIEW.md Spanner Overview}.
#
module Spanner
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/MethodLength,Metrics/AbcSize

##
# Creates a new object for connecting to the Spanner service.
Expand Down Expand Up @@ -68,6 +68,20 @@ module Spanner
# Deprecated.
# @param [String] emulator_host Spanner emulator host. Optional.
# If the param is nil, uses the value of the `emulator_host` config.
# @param [String] lib_name Library name. This will be added as a prefix
# to the API call tracking header `x-goog-api-client` with provided
# lib version for telemetry. Optional. For example prefix looks like
# `spanner-activerecord/0.0.1 gccl/1.13.1`. Here,
# `spanner-activerecord/0.0.1` is provided custom library name and
# version and `gccl/1.13.1` represents the Cloud Spanner Ruby library
# with version.
# @param [String] lib_version Library version. This will be added as a
# prefix to the API call tracking header `x-goog-api-client` with
# provided lib name for telemetry. Optional. For example prefix look like
# `spanner-activerecord/0.0.1 gccl/1.13.1`. Here,
# `spanner-activerecord/0.0.1` is provided custom library name and
# version and `gccl/1.13.1` represents the Cloud Spanner Ruby library
# with version.
#
# @return [Google::Cloud::Spanner::Project]
#
Expand All @@ -78,14 +92,16 @@ module Spanner
#
def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
client_config: nil, endpoint: nil, project: nil, keyfile: nil,
emulator_host: nil
emulator_host: nil, lib_name: nil, lib_version: nil
project_id ||= (project || default_project_id)
scope ||= configure.scope
timeout ||= configure.timeout
client_config ||= configure.client_config
endpoint ||= configure.endpoint
credentials ||= (keyfile || default_credentials(scope: scope))
emulator_host ||= configure.emulator_host
lib_name ||= configure.lib_name
lib_version ||= configure.lib_version

if emulator_host
credentials = :this_channel_is_insecure
Expand All @@ -106,12 +122,13 @@ def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
Spanner::Project.new(
Spanner::Service.new(
project_id, credentials,
host: endpoint, timeout: timeout, client_config: client_config
host: endpoint, timeout: timeout, client_config: client_config,
lib_name: lib_name, lib_version: lib_version
)
)
end

# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize

##
# Configure the Google Cloud Spanner library.
Expand All @@ -133,6 +150,12 @@ def self.new project_id: nil, credentials: nil, scope: nil, timeout: nil,
# to use the default endpoint.
# * `emulator_host` - (String) Host name of the emulator. Defaults to
# `ENV["SPANNER_EMULATOR_HOST"]`.
# * `lib_name` - (String) Override the lib name , or `nil`
# to use the default lib name without prefix in agent tracking
# header.
# * `lib_version` - (String) Override the lib version , or `nil`
# to use the default version lib name without prefix in agent
# tracking header.
#
# @return [Google::Cloud::Config] The configuration object the
# Google::Cloud::Spanner library uses.
Expand Down
21 changes: 16 additions & 5 deletions google-cloud-spanner/lib/google/cloud/spanner/service.rb
Expand Up @@ -29,17 +29,20 @@ module Spanner
# @private Represents the gRPC Spanner service, including all the API
# methods.
class Service
attr_accessor :project, :credentials, :timeout, :client_config, :host
attr_accessor :project, :credentials, :timeout, :client_config, :host,
:lib_name, :lib_version

##
# Creates a new Service instance.
def initialize project, credentials, host: nil, timeout: nil,
client_config: nil
client_config: nil, lib_name: nil, lib_version: nil
@project = project
@credentials = credentials
@host = host || V1::SpannerClient::SERVICE_ADDRESS
@timeout = timeout
@client_config = client_config || {}
@lib_name = lib_name
@lib_version = lib_version
end

def channel
Expand Down Expand Up @@ -67,7 +70,7 @@ def service
client_config: client_config,
service_address: service_address,
service_port: service_port,
lib_name: "gccl",
lib_name: lib_name_with_prefix,
lib_version: Google::Cloud::Spanner::VERSION
)
end
Expand All @@ -82,7 +85,7 @@ def instances
client_config: client_config,
service_address: service_address,
service_port: service_port,
lib_name: "gccl",
lib_name: lib_name_with_prefix,
lib_version: Google::Cloud::Spanner::VERSION
)
end
Expand All @@ -97,7 +100,7 @@ def databases
client_config: client_config,
service_address: service_address,
service_port: service_port,
lib_name: "gccl",
lib_name: lib_name_with_prefix,
lib_version: Google::Cloud::Spanner::VERSION
)
end
Expand Down Expand Up @@ -459,6 +462,14 @@ def service_port
URI.parse("//#{host}").port
end

def lib_name_with_prefix
return "gccl" if [nil, "gccl"].include? lib_name

value = lib_name.dup
value << "/#{lib_version}" if lib_version
value << " gccl"
end

def default_options_from_session session_name
default_prefix = session_name.split("/sessions/").first
Google::Gax::CallOptions.new kwargs: \
Expand Down

0 comments on commit ade2581

Please sign in to comment.