Skip to content

Commit fc8b0f9

Browse files
authored
feat: set primary user agent (#15)
* feat: set primary user agent Signed-off-by: Michal Fiedorowicz <[email protected]> --------- Signed-off-by: Michal Fiedorowicz <[email protected]>
1 parent d0a9729 commit fc8b0f9

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @leoparente @mfiedorowicz @natm
1+
* @leoparente @mfiedorowicz

netboxlabs/diode/sdk/client.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,24 @@ def __init__(
102102
("python-version", self._python_version),
103103
)
104104

105+
channel_opts = (
106+
("grpc.primary_user_agent", f"{self._name}/{self._version} {self._app_name}/{self._app_version}"),
107+
)
108+
105109
if self._tls_verify:
106110
_LOGGER.debug("Setting up gRPC secure channel")
107111
self._channel = grpc.secure_channel(
108112
self._target,
109113
grpc.ssl_channel_credentials(
110114
root_certificates=_load_certs(),
111115
),
116+
options=channel_opts,
112117
)
113118
else:
114119
_LOGGER.debug("Setting up gRPC insecure channel")
115120
self._channel = grpc.insecure_channel(
116121
target=self._target,
122+
options=channel_opts,
117123
)
118124

119125
channel = self._channel

tests/test_client.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,46 @@ def test_client_sets_up_insecure_channel_when_grpc_scheme_is_found_in_target():
231231
mock_insecure_channel.assert_called_once()
232232

233233

234+
def test_insecure_channel_options_with_primary_user_agent():
235+
"""Check that DiodeClient.__init__() sets the gRPC primary_user_agent option for insecure channel."""
236+
with mock.patch("grpc.insecure_channel") as mock_insecure_channel:
237+
client = DiodeClient(
238+
target="grpc://localhost:8081",
239+
app_name="my-producer",
240+
app_version="0.0.1",
241+
api_key="abcde",
242+
)
243+
244+
mock_insecure_channel.assert_called_once()
245+
_, kwargs = mock_insecure_channel.call_args
246+
assert kwargs["options"] == (
247+
(
248+
"grpc.primary_user_agent",
249+
f"{client.name}/{client.version} {client.app_name}/{client.app_version}",
250+
),
251+
)
252+
253+
254+
def test_secure_channel_options_with_primary_user_agent():
255+
"""Check that DiodeClient.__init__() sets the gRPC primary_user_agent option for secure channel."""
256+
with mock.patch("grpc.secure_channel") as mock_secure_channel:
257+
client = DiodeClient(
258+
target="grpcs://localhost:8081",
259+
app_name="my-producer",
260+
app_version="0.0.1",
261+
api_key="abcde",
262+
)
263+
264+
mock_secure_channel.assert_called_once()
265+
_, kwargs = mock_secure_channel.call_args
266+
assert kwargs["options"] == (
267+
(
268+
"grpc.primary_user_agent",
269+
f"{client.name}/{client.version} {client.app_name}/{client.app_version}",
270+
),
271+
)
272+
273+
234274
def test_client_interceptor_setup_with_path():
235275
"""Check that DiodeClient.__init__() sets up the gRPC interceptor when a path is provided."""
236276
client = DiodeClient(

0 commit comments

Comments
 (0)