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

Commit ee3b7c6

Browse files
authored
Set stackdriver stats exporter user agent (#370)
* Set stackdriver exporter's user agent by passing the OC client library version to the cloud monitoring client. * Include "opencensus" in user agent
1 parent fc42d70 commit ee3b7c6

2 files changed

Lines changed: 46 additions & 14 deletions

File tree

opencensus/stats/exporters/stackdriver_exporter.py

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

15-
import re
1615
import os
1716
import platform
18-
from . import base
19-
from google.cloud import monitoring_v3
20-
from opencensus.stats import aggregation
21-
from opencensus.stats import measure
17+
import re
18+
2219
from datetime import datetime
23-
from opencensus.common.transports import async_
20+
from google.api_core.gapic_v1 import client_info
21+
from google.cloud import monitoring_v3
22+
23+
from opencensus.__version__ import __version__
2424
from opencensus.common.monitored_resource_util.monitored_resource_util \
2525
import MonitoredResourceUtil
26+
from opencensus.common.transports import async_
27+
from opencensus.stats import aggregation
28+
from opencensus.stats import measure
29+
from opencensus.stats.exporters import base
2630

2731
MAX_TIME_SERIES_PER_UPLOAD = 200
2832
OPENCENSUS_TASK_DESCRIPTION = "Opencensus task identifier"
@@ -378,14 +382,20 @@ def set_attribute_label(series, resource_labels, attribute_key,
378382
label_value_prefix + resource_labels[attribute_key]
379383

380384

385+
def get_user_agent_slug():
386+
"""Get the UA fragment to identify this library version."""
387+
return "opencensus-{}".format(__version__)
388+
389+
381390
def new_stats_exporter(options):
382391
""" new_stats_exporter returns an exporter that
383392
uploads stats data to Stackdriver Monitoring.
384393
"""
385394
if str(options.project_id).strip() == "":
386395
raise Exception(ERROR_BLANK_PROJECT_ID)
387396

388-
client = monitoring_v3.MetricServiceClient()
397+
ci = client_info.ClientInfo(client_library_version=get_user_agent_slug())
398+
client = monitoring_v3.MetricServiceClient(client_info=ci)
389399

390400
exporter = StackdriverStatsExporter(client=client, options=options)
391401

tests/unit/stats/exporter/test_stackdriver_stats.py

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

15+
from datetime import datetime
1516
import unittest
17+
1618
import mock
17-
from datetime import datetime
19+
20+
from opencensus.__version__ import __version__
1821
from opencensus.stats import aggregation as aggregation_module
19-
from opencensus.stats.exporters import stackdriver_exporter as stackdriver
2022
from opencensus.stats import measure as measure_module
2123
from opencensus.stats import stats as stats_module
2224
from opencensus.stats import view as view_module
2325
from opencensus.stats import view_data as view_data_module
26+
from opencensus.stats.exporters import stackdriver_exporter as stackdriver
2427
from opencensus.tags import tag_key as tag_key_module
2528
from opencensus.tags import tag_map as tag_map_module
2629
from opencensus.tags import tag_value as tag_value_module
@@ -49,11 +52,8 @@
4952

5053

5154
class _Client(object):
52-
def __init__(self, project=None):
53-
if project is None:
54-
project = 'PROJECT'
55-
56-
self.project = project
55+
def __init__(self, client_info=None):
56+
self.client_info = client_info
5757

5858

5959
class TestOptions(unittest.TestCase):
@@ -108,6 +108,28 @@ def test_not_blank_project(self):
108108

109109
self.assertIsInstance(exporter_created, stackdriver.StackdriverStatsExporter)
110110

111+
def test_get_user_agent_slug(self):
112+
self.assertIn(__version__, stackdriver.get_user_agent_slug())
113+
114+
def test_client_info_user_agent(self):
115+
"""Check that the monitoring client sets a user agent.
116+
117+
The user agent should include the library version. Note that this
118+
assumes MetricServiceClient calls ClientInfo.to_user_agent to attach
119+
the user agent as metadata to metric service API calls.
120+
"""
121+
patch_client = mock.patch(
122+
'opencensus.stats.exporters.stackdriver_exporter.monitoring_v3'
123+
'.MetricServiceClient',
124+
_Client)
125+
126+
with patch_client:
127+
exporter = stackdriver.new_stats_exporter(
128+
stackdriver.Options(project_id=1))
129+
130+
self.assertIn(stackdriver.get_user_agent_slug(),
131+
exporter.client.client_info.to_user_agent())
132+
111133
def test_as_float(self):
112134
value = 1.5
113135
result = stackdriver.as_float(value)

0 commit comments

Comments
 (0)