Skip to content

Commit f139a0f

Browse files
committed
Permit to pass a custom transport to client
And a custom session to RequestsTransport
1 parent a746292 commit f139a0f

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

opamp/opentelemetry-opamp-client/src/opentelemetry/_opamp/client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from opentelemetry._opamp import messages
2323
from opentelemetry._opamp.proto import opamp_pb2
24+
from opentelemetry._opamp.transport.base import HttpTransport
2425
from opentelemetry._opamp.transport.requests import RequestsTransport
2526
from opentelemetry._opamp.version import __version__
2627
from opentelemetry.context import (
@@ -61,9 +62,12 @@ def __init__(
6162
timeout_millis: int = _DEFAULT_OPAMP_TIMEOUT_MS,
6263
agent_identifying_attributes: Mapping[str, AnyValue],
6364
agent_non_identifying_attributes: Mapping[str, AnyValue] | None = None,
65+
transport: HttpTransport = None,
6466
):
6567
self._timeout_millis = timeout_millis
66-
self._transport = RequestsTransport()
68+
self._transport = (
69+
RequestsTransport() if transport is None else transport
70+
)
6771

6872
self._endpoint = endpoint
6973
headers = headers or {}

opamp/opentelemetry-opamp-client/src/opentelemetry/_opamp/transport/requests.py

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

15+
from __future__ import annotations
16+
1517
from typing import Mapping
1618

1719
import requests
@@ -22,8 +24,8 @@
2224

2325

2426
class RequestsTransport(HttpTransport):
25-
def __init__(self):
26-
self.session = requests.Session()
27+
def __init__(self, session: requests.Session | None = None):
28+
self.session = requests.Session() if session is None else session
2729

2830
def send(
2931
self,

opamp/opentelemetry-opamp-client/tests/opamp/test_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from opentelemetry._opamp.proto.anyvalue_pb2 import (
3131
KeyValue as PB2KeyValue, # pylint: disable=no-name-in-module
3232
)
33+
from opentelemetry._opamp.transport.requests import RequestsTransport
3334
from opentelemetry._opamp.version import __version__
3435

3536

@@ -61,12 +62,14 @@ def test_can_instantiate_opamp_client_with_defaults():
6162

6263

6364
def test_can_instantiate_opamp_client_all_params():
65+
transport = RequestsTransport()
6466
client = OpAMPClient(
6567
endpoint="url",
6668
headers={"an": "header"},
6769
timeout_millis=2_000,
6870
agent_identifying_attributes={"foo": "bar"},
6971
agent_non_identifying_attributes={"bar": "baz"},
72+
transport=transport,
7073
)
7174

7275
assert client
@@ -85,6 +88,7 @@ def test_can_instantiate_opamp_client_all_params():
8588
assert client._agent_description.non_identifying_attributes == [
8689
PB2KeyValue(key="bar", value=PB2AnyValue(string_value="baz")),
8790
]
91+
assert client._transport is transport
8892

8993

9094
def test_client_headers_override_defaults():

opamp/opentelemetry-opamp-client/tests/opamp/transport/test_requests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from unittest import mock
1616

1717
import pytest
18+
import requests
1819

1920
from opentelemetry._opamp.proto import opamp_pb2
2021
from opentelemetry._opamp.transport.base import base_headers
@@ -28,6 +29,14 @@ def test_can_instantiate_requests_transport():
2829
assert transport
2930

3031

32+
def test_can_instantiate_requests_transport_with_own_session():
33+
session = requests.Session()
34+
transport = RequestsTransport(session=session)
35+
36+
assert transport
37+
assert transport.session is session
38+
39+
3140
def test_can_send():
3241
transport = RequestsTransport()
3342
serialized_message = opamp_pb2.ServerToAgent().SerializeToString()

0 commit comments

Comments
 (0)