Skip to content

Commit 05208d3

Browse files
Adds Correlated Activity Id (Azure#27820)
* correlated act id * Update _base.py * Create test_correlated_activity_id.py * Update CHANGELOG.md * Update container.py * Update _container.py * Update sdk/cosmos/azure-cosmos/CHANGELOG.md Co-authored-by: Simon Moreno <30335873+simorenoh@users.noreply.github.com> * Update container.py * Update test_correlated_activity_id.py * cspell * Update http_constants.py * Update container.py * Update test_correlated_activity_id.py * Update container.py * Update _container.py * Update _base.py * minor changes * Update _container.py Co-authored-by: Simon Moreno <30335873+simorenoh@users.noreply.github.com>
1 parent fd21c0c commit 05208d3

File tree

6 files changed

+77
-6
lines changed

6 files changed

+77
-6
lines changed

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
## Release History
22

33
### 4.3.1b2 (Unreleased)
4-
54
#### Features Added
6-
5+
- Added `correlated_activity_id` for query operations
76
#### Breaking Changes
87

98
#### Bugs Fixed

sdk/cosmos/azure-cosmos/azure/cosmos/_base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ def GetHeaders( # pylint: disable=too-many-statements,too-many-branches
299299
if options.get("autoUpgradePolicy"):
300300
headers[http_constants.HttpHeaders.AutoscaleSettings] = options["autoUpgradePolicy"]
301301

302+
if options.get("correlatedActivityId"):
303+
headers[http_constants.HttpHeaders.CorrelatedActivityId] = options["correlatedActivityId"]
304+
302305
return headers
303306

304307

sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from ._cosmos_client_connection_async import CosmosClientConnection
3232
from .._base import build_options as _build_options, validate_cache_staleness_value, _deserialize_throughput, \
33-
_replace_throughput
33+
_replace_throughput, GenerateGuidId
3434
from ..exceptions import CosmosResourceNotFoundError
3535
from ..http_constants import StatusCodes
3636
from ..offer import ThroughputProperties
@@ -364,7 +364,8 @@ def query_items(
364364
if max_integrated_cache_staleness_in_ms:
365365
validate_cache_staleness_value(max_integrated_cache_staleness_in_ms)
366366
feed_options["maxIntegratedCacheStaleness"] = max_integrated_cache_staleness_in_ms
367-
367+
correlated_activity_id = GenerateGuidId()
368+
feed_options["correlatedActivityId"] = correlated_activity_id
368369
if hasattr(response_hook, "clear"):
369370
response_hook.clear()
370371

sdk/cosmos/azure-cosmos/azure/cosmos/container.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
from azure.core.tracing.decorator import distributed_trace # type: ignore
3030

3131
from ._cosmos_client_connection import CosmosClientConnection
32-
from ._base import build_options, validate_cache_staleness_value, _deserialize_throughput, _replace_throughput
32+
from ._base import build_options, validate_cache_staleness_value, _deserialize_throughput, _replace_throughput, \
33+
GenerateGuidId
3334
from .exceptions import CosmosResourceNotFoundError
3435
from .http_constants import StatusCodes
3536
from .offer import ThroughputProperties
@@ -396,7 +397,8 @@ def query_items(
396397
if max_integrated_cache_staleness_in_ms:
397398
validate_cache_staleness_value(max_integrated_cache_staleness_in_ms)
398399
feed_options["maxIntegratedCacheStaleness"] = max_integrated_cache_staleness_in_ms
399-
400+
correlated_activity_id = GenerateGuidId()
401+
feed_options["correlatedActivityId"] = correlated_activity_id
400402
if hasattr(response_hook, "clear"):
401403
response_hook.clear()
402404

sdk/cosmos/azure-cosmos/azure/cosmos/http_constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class HttpHeaders(object):
9999

100100
# Request sender generated. Simply echoed by backend.
101101
ActivityId = "x-ms-activity-id"
102+
CorrelatedActivityId = "x-ms-cosmos-correlated-activityid" # cspell:disable-line
102103
PreTriggerInclude = "x-ms-documentdb-pre-trigger-include"
103104
PreTriggerExclude = "x-ms-documentdb-pre-trigger-exclude"
104105
PostTriggerInclude = "x-ms-documentdb-post-trigger-include"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# The MIT License (MIT)
2+
# Copyright (c) 2022 Microsoft Corporation
3+
4+
# Permission is hereby granted, free of charge, to any person obtaining a copy
5+
# of this software and associated documentation files (the "Software"), to deal
6+
# in the Software without restriction, including without limitation the rights
7+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
# copies of the Software, and to permit persons to whom the Software is
9+
# furnished to do so, subject to the following conditions:
10+
11+
# The above copyright notice and this permission notice shall be included in all
12+
# copies or substantial portions of the Software.
13+
14+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
# SOFTWARE.
21+
22+
import unittest
23+
from unittest.mock import MagicMock
24+
25+
import pytest
26+
27+
import azure.cosmos.cosmos_client as cosmos_client
28+
import test_config
29+
from azure.cosmos.partition_key import PartitionKey
30+
31+
pytestmark = pytest.mark.cosmosEmulator
32+
33+
34+
@pytest.mark.usefixtures("teardown")
35+
class CorrelatedActivityIdTest(unittest.TestCase):
36+
configs = test_config._test_config
37+
host = configs.host
38+
masterKey = configs.masterKey
39+
40+
@classmethod
41+
def setUpClass(cls):
42+
cls.client = cosmos_client.CosmosClient(cls.host, cls.masterKey)
43+
cls.database = cls.client.create_database_if_not_exists(test_config._test_config.TEST_DATABASE_ID)
44+
cls.container = cls.database.create_container(id=test_config._test_config.TEST_COLLECTION_MULTI_PARTITION_ID,
45+
partition_key=PartitionKey(path="/id"))
46+
47+
def side_effect_correlated_activity_id(self, *args):
48+
# Extract request headers from args
49+
assert args[2]["x-ms-cosmos-correlated-activityid"] # cspell:disable-line
50+
raise StopIteration
51+
52+
def test_correlated_activity_id(self):
53+
query = 'SELECT * from c ORDER BY c._ts'
54+
55+
cosmos_client_connection = self.container.client_connection
56+
cosmos_client_connection._CosmosClientConnection__Get = MagicMock(
57+
side_effect=self.side_effect_correlated_activity_id)
58+
try:
59+
self.container.query_items(query=query, partition_key="pk-1")
60+
except StopIteration:
61+
pass
62+
63+
64+
if __name__ == "__main__":
65+
unittest.main()

0 commit comments

Comments
 (0)