Nov 12, 2024
pip install django-prometheus
INSTALLED_APPS = [
...
'django_prometheus',
...
]
MIDDLEWARE = [
'django_prometheus.middleware.PrometheusBeforeMiddleware',
# All your other middlewares go here, including the default
# middlewares like SessionMiddleware, CommonMiddleware,
# CsrfViewmiddleware, SecurityMiddleware, etc.
'django_prometheus.middleware.PrometheusAfterMiddleware',
]
urlpatterns = [
...
path('', include('django_prometheus.urls')),
]
DATABASES = {
'default': {
'ENGINE': 'django_prometheus.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
}
CACHES = {
'default': {
'BACKEND': 'django_prometheus.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
},
}
class Dog(models.Model):
name = models.CharField(max_length=100, unique=True)
breed = models.CharField(max_length=100, blank=True, null=True)
age = models.PositiveIntegerField(blank=True, null=True)
from django_prometheus.models import ExportModelOperationsMixin
class Dog(ExportModelOperationsMixin('dog'), models.Model):
name = models.CharField(max_length=100, unique=True)
breed = models.CharField(max_length=100, blank=True, null=True)
age = models.PositiveIntegerField(blank=True, null=True)
loki:
image: grafana/loki:2.9.2
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml
networks:
- loki
promtail:
image: grafana/promtail:2.9.2
volumes:
- /var/log:/var/log
- /var/run/docker.sock:/var/run/docker.sock
- ./promtail/config/promtail-config.yml:/etc/promtail/config.yml
command: -config.file=/etc/promtail/config.yml
networks:
- loki
server:
http_listen_port: 9080
grpc_listen_port: 0
positions:
filename: /tmp/positions.yaml
clients:
- url: http://loki:3100/loki/api/v1/push
scrape_configs:
- job_name: system
static_configs:
- targets:
- localhost
labels:
job: varlogs
__path__: /var/log/*log
- job_name: docker
docker_sd_configs:
- host: unix:///var/run/docker.sock
relabel_configs:
- source_labels: [__meta_docker_container_name]
target_label: container
- source_labels:
[__meta_docker_container_label_com_docker_compose_service]
target_label: service
- source_labels:
[__meta_docker_container_label_com_docker_compose_project]
target_label: compose_project
- source_labels: [__meta_docker_container_label_com_docker_compose_oneoff]
target_label: compose_oneoff
- source_labels:
[__meta_docker_container_label_com_docker_compose_config_hash]
target_label: compose_config_hash
- source_labels:
[__meta_docker_container_label_com_docker_compose_version]
target_label: compose_version
grafana:
environment:
- GF_PATHS_PROVISIONING=/etc/grafana/provisioning
entrypoint:
- sh
- -euc
- |
mkdir -p /etc/grafana/provisioning/datasources
cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
apiVersion: 1
datasources:
- name: Loki
type: loki
access: proxy
orgId: 1
url: http://loki:3100
basicAuth: false
isDefault: true
version: 1
editable: false
- name: Prometheus
type: prometheus
access: proxy
orgId: 1
url: http://prometheus:9090
basicAuth: false
isDefault: false
version: 1
editable: false
EOF
/run.sh
image: grafana/grafana:latest
volumes:
- grafana_data:/var/lib/grafana
ports:
- "3060:3000"
networks:
- loki
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
entrypoint:
- sh
- -c
- |
/bin/prometheus --config.file=/etc/prometheus/prometheus/config/prometheus.yml --web.enable-lifecycle
volumes:
- ./prometheus/config/prometheus.yml:/etc/prometheus/prometheus/config/prometheus.yml
- prometheus_data:/prometheus
networks:
- loki
global:
scrape_interval: 15s
scrape_timeout: 10s
scrape_protocols:
- OpenMetricsText1.0.0
- OpenMetricsText0.0.1
- PrometheusText0.0.4
evaluation_interval: 15s
runtime:
gogc: 75
scrape_configs:
- job_name: prometheus
honor_timestamps: true
track_timestamps_staleness: false
scrape_interval: 15s
scrape_timeout: 10s
scrape_protocols:
- OpenMetricsText1.0.0
- OpenMetricsText0.0.1
- PrometheusText0.0.4
metrics_path: /metrics
scheme: http
enable_compression: true
follow_redirects: true
enable_http2: true
static_configs:
- targets:
- <YOUR IP>:8000
networks:
loki:
volumes:
grafana_data:
prometheus_data: