Skip to content

Commit

Permalink
Add Prometheus exporter to ROCK (#67)
Browse files Browse the repository at this point in the history
* adding exporter to rock

* address comments
  • Loading branch information
gtrkiller authored Jan 5, 2024
1 parent 764e827 commit bca3a66
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 54 deletions.
2 changes: 2 additions & 0 deletions content-cache_rock/rockcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ parts:
- nginx-light
- bash
- coreutils
stage-snaps:
- rocks-nginx-prometheus-exporter/latest/edge
copy-config:
plugin: dump
source: .
Expand Down
7 changes: 0 additions & 7 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,11 @@ source: https://github.com/canonical/content-cache-k8s-operator
containers:
content-cache:
resource: content-cache-image
nginx-prometheus-exporter:
resource: nginx-prometheus-exporter-image

resources:
content-cache-image:
type: oci-image
description: Docker image for content-cache to run
nginx-prometheus-exporter-image:
type: oci-image
description: Prometheus exporter for nginx
auto-fetch: true
upstream-source: nginx/nginx-prometheus-exporter:0.11.0

provides:
metrics-endpoint:
Expand Down
44 changes: 5 additions & 39 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
IngressRequires,
)
from charms.prometheus_k8s.v0.prometheus_scrape import MetricsEndpointProvider
from ops.charm import (
ActionEvent,
CharmBase,
ConfigChangedEvent,
PebbleReadyEvent,
UpgradeCharmEvent,
)
from ops.charm import ActionEvent, CharmBase, ConfigChangedEvent, UpgradeCharmEvent
from ops.main import main
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
from tabulate import tabulate
Expand Down Expand Up @@ -79,10 +73,6 @@ def __init__(self, *args):
self.framework.observe(
self.on.content_cache_pebble_ready, self._on_content_cache_pebble_ready
)
self.framework.observe(
self.on.nginx_prometheus_exporter_pebble_ready,
self._on_nginx_prometheus_exporter_pebble_ready,
)
# Provide ability for Content-cache to be scraped by Prometheus using prometheus_scrape
self._metrics_endpoint = MetricsEndpointProvider(
self, jobs=[{"static_configs": [{"targets": ["*:9113"]}]}]
Expand Down Expand Up @@ -117,17 +107,6 @@ def _on_content_cache_pebble_ready(self, event) -> None:
self.model.unit.status = MaintenanceStatus(msg)
self.on.config_changed.emit()

def _on_nginx_prometheus_exporter_pebble_ready(self, event: PebbleReadyEvent) -> None:
"""Handle content_cache_pebble_ready event and configure workload container.
Args:
event: Event triggering the pebble ready hook for the nginx prometheus exporter.
"""
msg = "Configuring workload container (nginx-prometheus-exporter-pebble-ready)"
logger.info(msg)
self.model.unit.status = MaintenanceStatus(msg)
self.on.config_changed.emit()

def _on_start(self, event) -> None:
"""Handle workload container started.
Expand Down Expand Up @@ -257,23 +236,10 @@ def configure_workload_container(self, event: ConfigChangedEvent) -> None:
logger.info(msg)
self.unit.status = MaintenanceStatus(msg)
container.add_layer(CONTAINER_NAME, pebble_config, combine=True)
container.add_layer(EXPORTER_CONTAINER_NAME, exporter_config, combine=True)
container.pebble.replan_services()
else:
self.unit.status = WaitingStatus("Waiting for Pebble to start (content-cache)")
event.defer()
return

exporter_container = self.unit.get_container(EXPORTER_CONTAINER_NAME)
if exporter_container.can_connect():
msg = "Updating exporter pebble layer config"
logger.info(msg)
self.unit.status = MaintenanceStatus(msg)
exporter_container.add_layer(EXPORTER_CONTAINER_NAME, exporter_config, combine=True)
exporter_container.pebble.replan_services()
else:
self.unit.status = WaitingStatus(
"Waiting for Pebble to start (nginx-prometheus-exporter)"
)
self.unit.status = WaitingStatus("Waiting for Pebble to start")
event.defer()
return

Expand All @@ -298,13 +264,13 @@ def _get_nginx_prometheus_exporter_pebble_config(self):
"""Generate pebble config for the nginx-prometheus-exporter container.
Returns:
Pebble layer config for the nginx-prometheus-exporter container.
Pebble layer config for the nginx-prometheus-exporter layer.
"""
return {
"summary": "Nginx prometheus exporter",
"description": "Prometheus exporter for nginx",
"services": {
"nginx-exporter": {
"nginx-prometheus-exporter": {
"override": "replace",
"summary": "Nginx Prometheus Exporter",
"command": (
Expand Down
8 changes: 0 additions & 8 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ async def get_unit_ip_list_action():
yield get_unit_ip_list_action


@fixture(scope="module")
def nginx_prometheus_exporter_image(metadata):
"""Provide the statsd prometheus exporter image from the metadata."""
yield metadata["resources"]["nginx-prometheus-exporter-image"]["upstream-source"]


@pytest_asyncio.fixture(scope="function")
async def unit_ip_list(get_unit_ip_list):
"""Yield ip addresses of current units."""
Expand Down Expand Up @@ -131,7 +125,6 @@ async def app(
app_name: str,
charm_file: str,
content_cache_image: str,
nginx_prometheus_exporter_image: str,
nginx_integrator_app: Application,
run_action,
):
Expand Down Expand Up @@ -162,7 +155,6 @@ async def app(
application_name=app_name,
resources={
"content-cache-image": content_cache_image,
"nginx-prometheus-exporter-image": nginx_prometheus_exporter_image,
},
series="jammy",
)
Expand Down

0 comments on commit bca3a66

Please sign in to comment.