Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus Remote Write Exporter (2/7) #9

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,79 @@ def __init__(
bearer_token_file: str = None,
headers: Dict = None,
):
raise NotImplementedError()
self.endpoint = endpoint
if basic_auth:
self.basic_auth = basic_auth
if bearer_token:
self.bearer_token = bearer_token
if bearer_token_file:
self.bearer_token_file = bearer_token_file
if headers:
self.headers = headers

@property
def endpoint(self):
return self._endpoint

@endpoint.setter
def endpoint(self, endpoint: str):
if endpoint == "":
raise ValueError("endpoint required")
self._endpoint = endpoint

@property
def basic_auth(self):
return self._basic_auth

@basic_auth.setter
def basic_auth(self, basic_auth: Dict):
if hasattr(self, "bearer_token") or hasattr(self, "bearer_token_file"):
raise ValueError("cannot contain basic_auth and bearer_token")
if "username" not in basic_auth:
raise ValueError("username required in basic_auth")
if "password" not in basic_auth and "password_file" not in basic_auth:
raise ValueError("password required in basic_auth")
if "password" in basic_auth and "password_file" in basic_auth:
raise ValueError(
"basic_auth cannot contain password and password_file"
)
self._basic_auth = basic_auth

@property
def bearer_token(self):
return self._bearer_token

@bearer_token.setter
def bearer_token(self, bearer_token: str):
if hasattr(self, "basic_auth"):
raise ValueError("cannot contain basic_auth and bearer_token")
if hasattr(self, "bearer_token_file"):
raise ValueError(
"cannot contain bearer_token and bearer_token_file"
)
self._bearer_token = bearer_token

@property
def bearer_token_file(self):
return self._bearer_token_file

@bearer_token_file.setter
def bearer_token_file(self, bearer_token_file: str):
if hasattr(self, "basic_auth"):
raise ValueError("cannot contain basic_auth and bearer_token")
if hasattr(self, "bearer_token"):
raise ValueError(
"cannot contain bearer_token and bearer_token_file"
)
self._bearer_token_file = bearer_token_file

@property
def headers(self):
return self._headers

@headers.setter
def headers(self, headers: Dict):
self._headers = headers

def export(
self, export_records: Sequence[ExportRecord]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,77 @@

import unittest

from opentelemetry.exporter.prometheus_remote_write import (
PrometheusRemoteWriteMetricsExporter,
)


class TestValidation(unittest.TestCase):
# Test cases to ensure exporter parameter validation works as intended
def test_valid_standard_param(self):
pass
PrometheusRemoteWriteMetricsExporter(endpoint="/prom/test_endpoint")

def test_valid_basic_auth_param(self):
pass
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={
"username": "test_username",
"password": "test_password",
},
)

def test_valid_bearer_token_param(self):
pass
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint", bearer_token="test_bearer_token",
)

def test_invalid_no_endpoint_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter("")

def test_invalid_no_username_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={"password": "test_password"},
)

def test_invalid_no_password_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={"username": "test_username"},
)

def test_invalid_conflicting_passwords_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={
"username": "test_username",
"password": "test_password",
"password_file": "test_file",
},
)

def test_invalid_conflicting_bearer_tokens_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
bearer_token="test_bearer_token",
bearer_token_file="test_file",
)

def test_invalid_conflicting_auth_param(self):
pass
with self.assertRaises(ValueError):
PrometheusRemoteWriteMetricsExporter(
endpoint="/prom/test_endpoint",
basic_auth={
"username": "test_username",
"password": "test_password",
},
bearer_token="test_bearer_token",
)


class TestConversion(unittest.TestCase):
Expand Down