Skip to content

Commit f5499ce

Browse files
committed
Made changes suggested in Diego's review
1 parent 55377e4 commit f5499ce

File tree

2 files changed

+44
-54
lines changed

2 files changed

+44
-54
lines changed

exporter/opentelemetry-exporter-prometheus-remote-write/src/opentelemetry/exporter/prometheus_remote_write/__init__.py

+16-20
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ def basic_auth(self, basic_auth: Dict):
9191
if basic_auth:
9292
if "username" not in basic_auth:
9393
raise ValueError("username required in basic_auth")
94-
if (
95-
"password" not in basic_auth
96-
and "password_file" not in basic_auth
97-
):
94+
if "password_file" in basic_auth:
95+
if "password" in basic_auth:
96+
raise ValueError(
97+
"basic_auth cannot contain password and password_file"
98+
)
99+
with open(basic_auth["password_file"]) as file:
100+
basic_auth["password"] = file.readline().strip()
101+
elif "password" not in basic_auth:
98102
raise ValueError("password required in basic_auth")
99-
if "password" in basic_auth and "password_file" in basic_auth:
100-
raise ValueError(
101-
"basic_auth cannot contain password and password_file"
102-
)
103103
self._basic_auth = basic_auth
104104

105105
@property
@@ -154,10 +154,10 @@ def headers(self, headers: Dict):
154154
def export(
155155
self, export_records: Sequence[ExportRecord]
156156
) -> MetricsExportResult:
157-
timeseries = self.convert_to_timeseries(export_records)
158-
message = self.build_message(timeseries)
159-
headers = self.get_headers()
160-
return self.send_message(message, headers)
157+
timeseries = self._convert_to_timeseries(export_records)
158+
message = self._build_message(timeseries)
159+
headers = self._build_headers()
160+
return self._send_message(message, headers)
161161

162162
def shutdown(self) -> None:
163163
pass
@@ -301,13 +301,13 @@ def add_label(label_name, label_value):
301301
timeseries.samples.append(sample)
302302
return timeseries
303303

304-
def build_message(self, timeseries: Sequence[TimeSeries]) -> bytes:
304+
def _build_message(self, timeseries: Sequence[TimeSeries]) -> bytes:
305305
write_request = WriteRequest()
306306
write_request.timeseries.extend(timeseries)
307307
serialized_message = write_request.SerializeToString()
308308
return snappy.compress(serialized_message)
309309

310-
def get_headers(self) -> Dict:
310+
def _build_headers(self) -> Dict:
311311
headers = {
312312
"Content-Encoding": "snappy",
313313
"Content-Type": "application/x-protobuf",
@@ -318,16 +318,12 @@ def get_headers(self) -> Dict:
318318
headers[header_name] = header_value
319319
return headers
320320

321-
def send_message(
321+
def _send_message(
322322
self, message: bytes, headers: Dict
323323
) -> MetricsExportResult:
324324
auth = None
325325
if self.basic_auth:
326-
if "password" in self.basic_auth:
327-
auth = (self.basic_auth["username"], self.basic_auth["password"])
328-
elif "password_file":
329-
with open(self.basic_auth["password_file"]) as file:
330-
auth = (self.basic_auth["username"], file.readline())
326+
auth = (self.basic_auth["username"], self.basic_auth["password"])
331327

332328
cert = None
333329
verify = True

exporter/opentelemetry-exporter-prometheus-remote-write/tests/test_prometheus_remote_write_exporter.py

+28-34
Original file line numberDiff line numberDiff line change
@@ -358,65 +358,59 @@ def create_label(name, value):
358358
self.assertEqual(timeseries, expected_timeseries)
359359

360360

361-
class ResponseStub:
362-
def __init__(self, status_code):
363-
self.status_code = status_code
364-
self.reason = "dummy_reason"
365-
self.content = "dummy_content"
366-
367-
368361
class TestExport(unittest.TestCase):
369362
# Initializes test data that is reused across tests
370363
def setUp(self):
371-
self._exporter = PrometheusRemoteWriteMetricsExporter(
364+
self.exporter = PrometheusRemoteWriteMetricsExporter(
372365
endpoint="/prom/test_endpoint"
373366
)
374367

375368
# Ensures export is successful with valid export_records and config
376-
@mock.patch("requests.post", return_value=ResponseStub(200))
369+
@patch("requests.post", return_value=Mock())
377370
def test_export(self, mock_post):
371+
mock_post.return_value.configure_mock(**{"status_code": 200})
378372
test_metric = Counter("testname", "testdesc", "testunit", int, None)
379-
labels = {"environment": "testing"}
373+
labels = get_dict_as_key({"environment": "testing"})
380374
record = ExportRecord(
381-
test_metric, labels, SumAggregator(), Resource({}),
375+
test_metric, labels, SumAggregator(), Resource({})
382376
)
383-
result = self._exporter.export([record])
377+
result = self.exporter.export([record])
384378
self.assertIs(result, MetricsExportResult.SUCCESS)
385379
self.assertEqual(mock_post.call_count, 1)
386380

387-
@mock.patch("requests.post", return_value=ResponseStub(200))
381+
@patch("requests.post", return_value=Mock())
388382
def test_valid_send_message(self, mock_post):
389-
result = self._exporter.send_message(bytes(), {})
383+
mock_post.return_value.configure_mock(**{"status_code": 200})
384+
result = self.exporter._send_message(bytes(), {})
390385
self.assertEqual(mock_post.call_count, 1)
391386
self.assertEqual(result, MetricsExportResult.SUCCESS)
392387

393-
@mock.patch("requests.post", return_value=ResponseStub(404))
388+
@patch("requests.post", return_value=Mock())
394389
def test_invalid_send_message(self, mock_post):
395-
result = self._exporter.send_message(bytes(), {})
390+
mock_post.return_value.configure_mock(
391+
**{
392+
"status_code": 404,
393+
"reason": "test_reason",
394+
"content": "test_content",
395+
}
396+
)
397+
result = self.exporter._send_message(bytes(), {})
396398
self.assertEqual(mock_post.call_count, 1)
397399
self.assertEqual(result, MetricsExportResult.FAILURE)
398400

399401
# Verifies that build_message calls snappy.compress and returns SerializedString
400-
@mock.patch("snappy.compress", return_value=bytes())
402+
@patch("snappy.compress", return_value=bytes())
401403
def test_build_message(self, mock_compress):
402-
test_timeseries = [
403-
TimeSeries(),
404-
TimeSeries(),
405-
]
406-
message = self._exporter.build_message(test_timeseries)
404+
message = self.exporter._build_message([TimeSeries()])
407405
self.assertEqual(mock_compress.call_count, 1)
408406
self.assertIsInstance(message, bytes)
409407

410408
# Ensure correct headers are added when valid config is provided
411-
def test_get_headers(self):
412-
self._exporter.headers = {"Custom Header": "test_header"}
413-
414-
headers = self._exporter.get_headers()
415-
self.assertEqual(headers.get("Content-Encoding", ""), "snappy")
416-
self.assertEqual(
417-
headers.get("Content-Type", ""), "application/x-protobuf"
418-
)
419-
self.assertEqual(
420-
headers.get("X-Prometheus-Remote-Write-Version", ""), "0.1.0"
421-
)
422-
self.assertEqual(headers.get("Custom Header", ""), "test_header")
409+
def test_build_headers(self):
410+
self.exporter.headers = {"Custom Header": "test_header"}
411+
412+
headers = self.exporter._build_headers()
413+
self.assertEqual(headers["Content-Encoding"], "snappy")
414+
self.assertEqual(headers["Content-Type"], "application/x-protobuf")
415+
self.assertEqual(headers["X-Prometheus-Remote-Write-Version"], "0.1.0")
416+
self.assertEqual(headers["Custom Header"], "test_header")

0 commit comments

Comments
 (0)