|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +from datetime import datetime |
15 | 16 | import unittest |
| 17 | + |
16 | 18 | import mock |
17 | | -from datetime import datetime |
| 19 | + |
| 20 | +from opencensus.__version__ import __version__ |
18 | 21 | from opencensus.stats import aggregation as aggregation_module |
19 | | -from opencensus.stats.exporters import stackdriver_exporter as stackdriver |
20 | 22 | from opencensus.stats import measure as measure_module |
21 | 23 | from opencensus.stats import stats as stats_module |
22 | 24 | from opencensus.stats import view as view_module |
23 | 25 | from opencensus.stats import view_data as view_data_module |
| 26 | +from opencensus.stats.exporters import stackdriver_exporter as stackdriver |
24 | 27 | from opencensus.tags import tag_key as tag_key_module |
25 | 28 | from opencensus.tags import tag_map as tag_map_module |
26 | 29 | from opencensus.tags import tag_value as tag_value_module |
|
49 | 52 |
|
50 | 53 |
|
51 | 54 | 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 |
57 | 57 |
|
58 | 58 |
|
59 | 59 | class TestOptions(unittest.TestCase): |
@@ -108,6 +108,28 @@ def test_not_blank_project(self): |
108 | 108 |
|
109 | 109 | self.assertIsInstance(exporter_created, stackdriver.StackdriverStatsExporter) |
110 | 110 |
|
| 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 | + |
111 | 133 | def test_as_float(self): |
112 | 134 | value = 1.5 |
113 | 135 | result = stackdriver.as_float(value) |
|
0 commit comments