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

configure prometheus url #702

Merged
merged 1 commit into from
Jul 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion charts/platform-reports/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
- name: thanos
version: "0.3.23"
repository: "https://kubernetes-charts.banzaicloud.com"
condition: prometheusRemoteStorageEnabled
condition: prometheus.remoteStorageEnabled
- name: grafana
version: "7.0.11"
repository: "https://grafana.github.io/helm-charts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec:
- name: CLUSTER_NAME
value: {{ .Values.platform.clusterName | quote }}
- name: PROMETHEUS_URL
value: http://thanos-query-http:10902
value: {{ .Values.prometheus.url }}
- name: PLATFORM_AUTH__URL
value: {{ .Values.platform.authUrl | quote }}
- name: PLATFORM_AUTH__TOKEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ spec:
- name: NP_CLUSTER_NAME
value: {{ .Values.platform.clusterName | quote }}
- name: PROMETHEUS_URL
value: http://thanos-query-http:10902
value: {{ .Values.prometheus.url }}
- name: NP_AUTH_URL
value: {{ .Values.platform.authUrl | quote }}
- name: NP_API_URL
Expand Down
6 changes: 5 additions & 1 deletion charts/platform-reports/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ grafanaProxy:
requests:
cpu: 100m
memory: 256Mi

ingress:
enabled: false
ingressClassName: ""
hosts: []
annotations: {}

prometheusRemoteStorageEnabled: true
prometheus:
url: http://thanos-query-http:10902

remoteStorageEnabled: true

kube-prometheus-stack:
nameOverride: prometheus
Expand Down
21 changes: 12 additions & 9 deletions src/platform_reports/metrics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,25 @@ def create_for_compute(cls, metric: Metric) -> CreditsUsage | None:
return None

@classmethod
def _create_for_job(cls, metric: Metric, *, job_id: str) -> CreditsUsage:
def _create_for_job(cls, metric: Metric, *, job_id: str) -> CreditsUsage | None:
if not (project_name := cls._get_project_name(metric)):
return None
return CreditsUsage(
category_name=CategoryName.JOBS,
org_name=cls._get_org_name(metric),
project_name=cls._get_project_name(metric),
project_name=project_name,
resource_id=job_id,
credits=metric.values[-1].value - metric.values[0].value,
)

@classmethod
def _create_for_app(cls, metric: Metric, *, app_id: str) -> CreditsUsage:
def _create_for_app(cls, metric: Metric, *, app_id: str) -> CreditsUsage | None:
if not (project_name := cls._get_project_name(metric)):
return None
return CreditsUsage(
category_name=CategoryName.APPS,
org_name=cls._get_org_name(metric),
project_name=cls._get_project_name(metric),
project_name=project_name,
resource_id=app_id,
credits=metric.values[-1].value - metric.values[0].value,
)
Expand All @@ -131,11 +135,10 @@ def _get_org_name(cls, metric: Metric) -> str | None:
return None if org_name == "no_org" else org_name

@classmethod
def _get_project_name(cls, metric: Metric) -> str:
return (
metric.labels.get(PrometheusLabel.APOLO_PROJECT_KEY)
or metric.labels[PrometheusLabel.NEURO_PROJECT_KEY]
)
def _get_project_name(cls, metric: Metric) -> str | None:
return metric.labels.get(
PrometheusLabel.APOLO_PROJECT_KEY
) or metric.labels.get(PrometheusLabel.NEURO_PROJECT_KEY)


class MetricsService:
Expand Down
36 changes: 34 additions & 2 deletions tests/unit/test_metrics_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_create_for_compute__job(self) -> None:
credits=Decimal(2),
)

def test_create_for_compute__job__with_org(self) -> None:
def test_create_for_compute__job__with_org_label(self) -> None:
metric = Metric(
labels={
"label_platform_neuromation_io_org": "test-org",
Expand All @@ -80,6 +80,22 @@ def test_create_for_compute__job__with_org(self) -> None:
credits=Decimal(2),
)

def test_create_for_compute__job__no_project_label(self) -> None:
metric = Metric(
labels={
"label_platform_neuromation_io_job": "test-job",
},
values=[
Metric.Value(datetime.now(), Decimal(1)),
Metric.Value(datetime.now(), Decimal(2)),
Metric.Value(datetime.now(), Decimal(3)),
],
)

usage = CreditsUsageFactory().create_for_compute(metric)

assert usage is None

def test_create_for_compute__app(self) -> None:
metric = Metric(
labels={
Expand All @@ -104,7 +120,23 @@ def test_create_for_compute__app(self) -> None:
credits=Decimal(2),
)

def test_create_for_compute__none(self) -> None:
def test_create_for_compute__app__no_project_label(self) -> None:
metric = Metric(
labels={
"label_platform_apolo_us_app": "test-app",
},
values=[
Metric.Value(datetime.now(), Decimal(1)),
Metric.Value(datetime.now(), Decimal(2)),
Metric.Value(datetime.now(), Decimal(3)),
],
)

usage = CreditsUsageFactory().create_for_compute(metric)

assert usage is None

def test_create_for_compute__not_enough_metrics(self) -> None:
metric = Metric(
labels={
"label_platform_neuromation_io_project": "test-project",
Expand Down
Loading