Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ def self.new project_id: nil,
scope ||= configure.scope
timeout ||= configure.timeout
endpoint ||= configure.endpoint
quota_project = configure.quota_project
credentials ||= keyfile || default_credentials(scope: scope)

credentials = resolve_credentials credentials, scope
project_id = resolve_project_id project_id, credentials

service = ErrorReporting::Service.new project_id, credentials, host: endpoint, timeout: timeout
service = ErrorReporting::Service.new project_id, credentials,
host: endpoint, timeout: timeout,
quota_project: quota_project
ErrorReporting::Project.new service
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def project_id
# error_event = error_reporting.error_event "Error with Backtrace"
# error_reporting.report error_event
#
def report *args, &block
service.report(*args, &block)
def report(*args, &)
service.report(*args, &)
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ class Service
attr_accessor :credentials
attr_accessor :timeout
attr_accessor :host
attr_accessor :quota_project

##
# Creates a new Service instance.
def initialize project, credentials, timeout: nil, host: nil
def initialize project, credentials, timeout: nil, host: nil, quota_project: nil
@project = project
@credentials = credentials
@timeout = timeout
@host = host
@quota_project = quota_project || (credentials.quota_project_id if credentials.respond_to? :quota_project_id)
end

def error_reporting
Expand All @@ -46,6 +48,7 @@ def error_reporting
config.credentials = credentials if credentials
config.timeout = timeout if timeout
config.endpoint = host if host
config.quota_project = quota_project if quota_project
config.lib_name = "gccl"
config.lib_version = Google::Cloud::ErrorReporting::VERSION
end
Expand Down
11 changes: 6 additions & 5 deletions google-cloud-error_reporting/support/doctest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

require "minitest/focus"
require "ostruct"

require "google/cloud/error_reporting"

Expand Down Expand Up @@ -73,13 +74,13 @@ def mock_error_reporting

doctest.before "Google::Cloud#error_reporting" do
mock_error_reporting do |mock|
mock.expect :report_error_event, nil, [Hash]
mock.expect :report_error_event, nil, [], project_name: Object, event: Object
end
end

doctest.before "Google::Cloud.error_reporting" do
mock_error_reporting do |mock|
mock.expect :report_error_event, nil, [Hash]
mock.expect :report_error_event, nil, [], project_name: Object, event: Object
end
end

Expand All @@ -89,21 +90,21 @@ def mock_error_reporting

doctest.before "Google::Cloud::ErrorReporting::ErrorEvent" do
mock_error_reporting do |mock|
mock.expect :report_error_event, nil, [Hash]
mock.expect :report_error_event, nil, [], project_name: Object, event: Object
end
end

doctest.before "Google::Cloud::ErrorReporting::Project" do
mock_error_reporting do |mock|
mock.expect :report_error_event, nil, [Hash]
mock.expect :report_error_event, nil, [], project_name: Object, event: Object
end
end

doctest.skip "Google::Cloud::ErrorReporting::Credentials" # occasionally getting "This code example is not yet mocked"

doctest.before "Google::Cloud::ErrorReporting::Service" do
mock_error_reporting do |mock|
mock.expect :report_error_event, nil, [Hash]
mock.expect :report_error_event, nil, [], project_name: Object, event: Object
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright 2026 Google LLC
#
# 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
#
# https://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.

require "helper"
require "signet/oauth_2/client"

describe Google::Cloud::ErrorReporting::Service do
let(:project) { "test-project" }
let(:credentials) do
creds = Signet::OAuth2::Client.new
def creds.quota_project_id
"credentials-quota-project"
end
def creds.disable_universe_domain_check
true
end
creds
end

describe ".new" do
it "sets project and credentials" do
service = Google::Cloud::ErrorReporting::Service.new project, credentials
_(service.project).must_equal project
_(service.credentials).must_equal credentials
end

it "accepts quota_project" do
quota_project = "test-quota-project"
service = Google::Cloud::ErrorReporting::Service.new project, credentials, quota_project: quota_project
_(service.quota_project).must_equal quota_project
end

it "falls back to credentials quota_project_id if not explicitly passed" do
service = Google::Cloud::ErrorReporting::Service.new project, credentials
_(service.quota_project).must_equal "credentials-quota-project"
end
end

describe "#error_reporting" do
it "configures the gRPC client with quota_project" do
quota_project = "test-quota-project"
service = Google::Cloud::ErrorReporting::Service.new project, credentials, quota_project: quota_project

client = service.error_reporting
_(client.configure.quota_project).must_equal quota_project
end

it "configures the gRPC client with credentials quota_project" do
service = Google::Cloud::ErrorReporting::Service.new project, credentials

client = service.error_reporting
_(client.configure.quota_project).must_equal "credentials-quota-project"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def creds.is_a? target

it "uses provided endpoint" do
endpoint = "errorreporting-endpoint2.example.com"
stubbed_service = ->(project, credentials, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal default_credentials
_(timeout).must_be :nil?
Expand All @@ -85,6 +85,28 @@ def creds.is_a? target
end
end

it "uses configuration quota_project" do
quota_project = "configure-quota-project"
Google::Cloud::ErrorReporting.configure do |config|
config.quota_project = quota_project
end

stubbed_service = ->(project, credentials, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(quota_project).must_equal "configure-quota-project"
OpenStruct.new project: project
}

ENV.stub :[], nil do
Google::Cloud::ErrorReporting::Service.stub :new, stubbed_service do
error_reporting = Google::Cloud::ErrorReporting.new project_id: "project-id",
credentials: default_credentials
_(error_reporting).must_be_kind_of Google::Cloud::ErrorReporting::Project
end
end
Google::Cloud::ErrorReporting.configure.reset!
end

it "uses provided project (alias), keyfile (alias), service, and version" do
stubbed_credentials = ->(keyfile, scope: nil) {
_(keyfile).must_equal "/path/to/a/keyfile"
Expand Down Expand Up @@ -315,7 +337,7 @@ def creds.is_a? target
_(scope).must_equal default_scopes
"error_reporting-credentials"
}
stubbed_service = ->(project, credentials, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "error_reporting-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -352,7 +374,7 @@ def creds.is_a? target
_(scope).must_equal default_scopes
"error_reporting-credentials"
}
stubbed_service = ->(project, credentials, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "error_reporting-credentials"
_(timeout).must_be :nil?
Expand Down Expand Up @@ -389,7 +411,7 @@ def creds.is_a? target
_(scope).must_equal default_scopes
"error_reporting-credentials"
}
stubbed_service = ->(project, credentials, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "error_reporting-credentials"
_(timeout).must_equal 42
Expand Down Expand Up @@ -423,7 +445,7 @@ def creds.is_a? target

it "uses error_reporting config for endpoint" do
endpoint = "errorreporting-endpoint2.example.com"
stubbed_service = ->(project, credentials, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal default_credentials
_(timeout).must_be :nil?
Expand Down Expand Up @@ -454,7 +476,7 @@ def creds.is_a? target
_(scope).must_equal default_scopes
"error_reporting-credentials"
}
stubbed_service = ->(project, credentials, timeout: nil, host: nil) {
stubbed_service = ->(project, credentials, timeout: nil, host: nil, quota_project: nil) {
_(project).must_equal "project-id"
_(credentials).must_equal "error_reporting-credentials"
_(timeout).must_equal 42
Expand Down
Loading