Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit e400b8b

Browse files
authored
Use default auth for StackdriverStatsExporter (#610)
1 parent d8709f1 commit e400b8b

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

contrib/opencensus-ext-stackdriver/opencensus/ext/stackdriver/stats_exporter/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from google.api_core.gapic_v1 import client_info
2424
from google.cloud import monitoring_v3
25+
import google.auth
2526

2627
from opencensus.common import utils
2728
from opencensus.common.monitored_resource import monitored_resource
@@ -365,12 +366,16 @@ def get_user_agent_slug():
365366
return "opencensus-python/{}".format(__version__)
366367

367368

368-
def new_stats_exporter(options, interval=None):
369+
def new_stats_exporter(options=None, interval=None):
369370
"""Get a stats exporter and running transport thread.
370371
371372
Create a new `StackdriverStatsExporter` with the given options and start
372373
periodically exporting stats to stackdriver in the background.
373374
375+
Fall back to default auth if `options` is null. This will raise
376+
`google.auth.exceptions.DefaultCredentialsError` if default credentials
377+
aren't configured.
378+
374379
See `opencensus.metrics.transport.get_exporter_thread` for details on the
375380
transport thread.
376381
@@ -380,9 +385,12 @@ def new_stats_exporter(options, interval=None):
380385
:type interval: int or float
381386
:param interval: Seconds between export calls.
382387
383-
:rtype: :class:`StackdriverStatsExporter` and :class:`PeriodicTask`
384-
:return: A tuple of the exporter and transport thread.
388+
:rtype: :class:`StackdriverStatsExporter`
389+
:return: The newly-created exporter.
385390
"""
391+
if options is None:
392+
_, project_id = google.auth.default()
393+
options = Options(project_id=project_id)
386394
if str(options.project_id).strip() == "":
387395
raise ValueError(ERROR_BLANK_PROJECT_ID)
388396

contrib/opencensus-ext-stackdriver/tests/test_stackdriver_stats.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import unittest
1818

1919
from google.cloud import monitoring_v3
20+
import google.auth
2021

2122
from opencensus.common import utils
2223
from opencensus.common.version import __version__
@@ -115,6 +116,32 @@ def test_constructor_param(self):
115116
default_monitoring_labels=default_labels))
116117
self.assertEqual(exporter.options.project_id, project_id)
117118

119+
def test_null_options(self):
120+
# Check that we don't suppress auth errors
121+
auth_error = google.auth.exceptions.DefaultCredentialsError
122+
mock_auth_error = mock.Mock()
123+
mock_auth_error.side_effect = auth_error
124+
with mock.patch('opencensus.ext.stackdriver.stats_exporter'
125+
'.google.auth.default', mock_auth_error):
126+
with self.assertRaises(auth_error):
127+
stackdriver.new_stats_exporter()
128+
129+
# Check that we get the default credentials' project ID
130+
mock_auth_ok = mock.Mock()
131+
mock_auth_ok.return_value = (None, 123)
132+
with mock.patch('opencensus.ext.stackdriver.stats_exporter'
133+
'.google.auth.default', mock_auth_ok):
134+
sdse = stackdriver.new_stats_exporter()
135+
self.assertEqual(sdse.options.project_id, 123)
136+
137+
# Check that we raise if auth works but the project is empty
138+
mock_auth_no_project = mock.Mock()
139+
mock_auth_no_project.return_value = (None, '')
140+
with mock.patch('opencensus.ext.stackdriver.stats_exporter'
141+
'.google.auth.default', mock_auth_no_project):
142+
with self.assertRaises(ValueError):
143+
stackdriver.new_stats_exporter()
144+
118145
def test_blank_project(self):
119146
self.assertRaises(ValueError, stackdriver.new_stats_exporter,
120147
stackdriver.Options(project_id=""))

0 commit comments

Comments
 (0)