Skip to content

Commit 6ab3822

Browse files
Azfaar Qureshishovnik
Azfaar Qureshi
authored andcommitted
Adding config validation
1 parent 9c84064 commit 6ab3822

File tree

2 files changed

+136
-10
lines changed

2 files changed

+136
-10
lines changed

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

+73-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,79 @@ def __init__(
4343
bearer_token_file: str = None,
4444
headers: Dict = None,
4545
):
46-
pass
46+
self.endpoint = endpoint
47+
if basic_auth:
48+
self.basic_auth = basic_auth
49+
if bearer_token:
50+
self.bearer_token = bearer_token
51+
if bearer_token_file:
52+
self.bearer_token_file = bearer_token_file
53+
if headers:
54+
self.headers = headers
55+
56+
@property
57+
def endpoint(self):
58+
return self._endpoint
59+
60+
@endpoint.setter
61+
def endpoint(self, endpoint: str):
62+
if endpoint == "":
63+
raise ValueError("endpoint required")
64+
self._endpoint = endpoint
65+
66+
@property
67+
def basic_auth(self):
68+
return self._basic_auth
69+
70+
@basic_auth.setter
71+
def basic_auth(self, basic_auth: Dict):
72+
if hasattr(self, "bearer_token") or hasattr(self, "bearer_token_file"):
73+
raise ValueError("cannot contain basic_auth and bearer_token")
74+
if "username" not in basic_auth:
75+
raise ValueError("username required in basic_auth")
76+
if "password" not in basic_auth and "password_file" not in basic_auth:
77+
raise ValueError("password required in basic_auth")
78+
if "password" in basic_auth and "password_file" in basic_auth:
79+
raise ValueError(
80+
"basic_auth cannot contain password and password_file"
81+
)
82+
self._basic_auth = basic_auth
83+
84+
@property
85+
def bearer_token(self):
86+
return self._bearer_token
87+
88+
@bearer_token.setter
89+
def bearer_token(self, bearer_token: str):
90+
if hasattr(self, "basic_auth"):
91+
raise ValueError("cannot contain basic_auth and bearer_token")
92+
if hasattr(self, "bearer_token_file"):
93+
raise ValueError(
94+
"cannot contain bearer_token and bearer_token_file"
95+
)
96+
self._bearer_token = bearer_token
97+
98+
@property
99+
def bearer_token_file(self):
100+
return self._bearer_token_file
101+
102+
@bearer_token_file.setter
103+
def bearer_token_file(self, bearer_token_file: str):
104+
if hasattr(self, "basic_auth"):
105+
raise ValueError("cannot contain basic_auth and bearer_token")
106+
if hasattr(self, "bearer_token"):
107+
raise ValueError(
108+
"cannot contain bearer_token and bearer_token_file"
109+
)
110+
self._bearer_token_file = bearer_token_file
111+
112+
@property
113+
def headers(self):
114+
return self._headers
115+
116+
@headers.setter
117+
def headers(self, headers: Dict):
118+
self._headers = headers
47119

48120
def export(
49121
self, export_records: Sequence[ExportRecord]

Diff for: exporter/opentelemetry-exporter-prometheus-remote-write/tests/test_prometheus_remote_write_exporter.py

+63-9
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,89 @@
1414

1515
import unittest
1616

17+
from opentelemetry.exporter.prometheus_remote_write import (
18+
PrometheusRemoteWriteMetricsExporter,
19+
)
20+
1721

1822
class TestValidation(unittest.TestCase):
1923
# Test cases to ensure exporter parameter validation works as intended
2024
def test_valid_standard_param(self):
21-
pass
25+
try:
26+
PrometheusRemoteWriteMetricsExporter(
27+
endpoint="/prom/test_endpoint",
28+
)
29+
except ValueError:
30+
self.fail("failed to instantiate exporter with valid params)")
2231

2332
def test_valid_basic_auth_param(self):
24-
pass
33+
try:
34+
PrometheusRemoteWriteMetricsExporter(
35+
endpoint="/prom/test_endpoint",
36+
basic_auth={
37+
"username": "test_username",
38+
"password": "test_password",
39+
},
40+
)
41+
except ValueError:
42+
self.fail("failed to instantiate exporter with valid params)")
2543

2644
def test_valid_bearer_token_param(self):
27-
pass
45+
try:
46+
PrometheusRemoteWriteMetricsExporter(
47+
endpoint="/prom/test_endpoint",
48+
bearer_token="test_bearer_token",
49+
)
50+
except ValueError:
51+
self.fail("failed to instantiate exporter with valid params)")
2852

2953
def test_invalid_no_endpoint_param(self):
30-
pass
54+
with self.assertRaises(ValueError):
55+
PrometheusRemoteWriteMetricsExporter("")
3156

3257
def test_invalid_no_username_param(self):
33-
pass
58+
with self.assertRaises(ValueError):
59+
PrometheusRemoteWriteMetricsExporter(
60+
endpoint="/prom/test_endpoint",
61+
basic_auth={"password": "test_password"},
62+
)
3463

3564
def test_invalid_no_password_param(self):
36-
pass
65+
with self.assertRaises(ValueError):
66+
PrometheusRemoteWriteMetricsExporter(
67+
endpoint="/prom/test_endpoint",
68+
basic_auth={"username": "test_username"},
69+
)
3770

3871
def test_invalid_conflicting_passwords_param(self):
39-
pass
72+
with self.assertRaises(ValueError):
73+
PrometheusRemoteWriteMetricsExporter(
74+
endpoint="/prom/test_endpoint",
75+
basic_auth={
76+
"username": "test_username",
77+
"password": "test_password",
78+
"password_file": "test_file",
79+
},
80+
)
4081

4182
def test_invalid_conflicting_bearer_tokens_param(self):
42-
pass
83+
with self.assertRaises(ValueError):
84+
PrometheusRemoteWriteMetricsExporter(
85+
endpoint="/prom/test_endpoint",
86+
bearer_token="test_bearer_token",
87+
bearer_token_file="test_file",
88+
)
4389

4490
def test_invalid_conflicting_auth_param(self):
45-
pass
91+
with self.assertRaises(ValueError):
92+
PrometheusRemoteWriteMetricsExporter(
93+
endpoint="/prom/test_endpoint",
94+
basic_auth={
95+
"username": "test_username",
96+
"password": "test_password",
97+
},
98+
bearer_token="test_bearer_token",
99+
)
46100

47101

48102
class TestConversion(unittest.TestCase):

0 commit comments

Comments
 (0)