-
Notifications
You must be signed in to change notification settings - Fork 16
/
config_builder.py
199 lines (175 loc) · 6.52 KB
/
config_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# Copyright 2023 Canonical Ltd.
# See LICENSE file for licensing details.
"""Config builder for Loki Charmed Operator."""
import os
# Paths in workload container
HTTP_LISTEN_PORT = 3100
LOKI_CONFIG_DIR = "/etc/loki"
LOKI_CONFIG = os.path.join(LOKI_CONFIG_DIR, "loki-local-config.yaml")
LOKI_CERTS_DIR = os.path.join(LOKI_CONFIG_DIR, "certs")
CERT_FILE = os.path.join(LOKI_CERTS_DIR, "loki.cert.pem")
KEY_FILE = os.path.join(LOKI_CERTS_DIR, "loki.key.pem")
LOKI_DIR = "/loki"
CHUNKS_DIR = os.path.join(LOKI_DIR, "chunks")
BOLTDB_DIR = os.path.join(LOKI_DIR, "boltdb-shipper-active")
RULES_DIR = os.path.join(LOKI_DIR, "rules")
class ConfigBuilder:
"""Loki configuration builder class.
Some minimal configuration is required for Loki to start, including: storage paths, schema,
ring.
Reference: https://grafana.com/docs/loki/latest/configuration/
"""
_target: str = "all"
_auth_enabled: bool = False
def __init__(
self,
*,
instance_addr: str,
alertmanager_url: str,
external_url: str,
ingestion_rate_mb: int,
ingestion_burst_size_mb: int,
http_tls: bool = False,
):
"""Init method."""
self.instance_addr = instance_addr
self.alertmanager_url = alertmanager_url
self.external_url = external_url
self.ingestion_rate_mb = ingestion_rate_mb
self.ingestion_burst_size_mb = ingestion_burst_size_mb
self.http_tls = http_tls
def build(self) -> dict:
"""Build Loki config dictionary."""
return {
"target": self._target,
"auth_enabled": self._auth_enabled,
"common": self._common,
"ingester": self._ingester,
"ruler": self._ruler,
"schema_config": self._schema_config,
"server": self._server,
"storage_config": self._storage_config,
"limits_config": self._limits_config,
"query_range": self._query_range,
"chunk_store_config": self._chunk_store_config,
"frontend": self._frontend,
"querier": self._querier,
}
@property
def _common(self) -> dict:
return {
"path_prefix": LOKI_DIR,
"replication_factor": 1,
"ring": {"instance_addr": self.instance_addr, "kvstore": {"store": "inmemory"}},
"storage": {
"filesystem": {
"chunks_directory": CHUNKS_DIR,
"rules_directory": RULES_DIR,
}
},
}
@property
def _ingester(self) -> dict:
return {
"wal": {
"dir": os.path.join(CHUNKS_DIR, "wal"),
"enabled": True,
"flush_on_shutdown": True,
}
}
@property
def _ruler(self) -> dict:
# Reference: https://grafana.com/docs/loki/latest/configure/#ruler
return {
"alertmanager_url": self.alertmanager_url,
"external_url": self.external_url,
}
@property
def _schema_config(self) -> dict:
return {
"configs": [
{
"from": "2020-10-24",
"index": {"period": "24h", "prefix": "index_"},
"object_store": "filesystem",
"schema": "v11",
"store": "boltdb",
}
]
}
@property
def _server(self) -> dict:
_server = {
"http_listen_address": "0.0.0.0",
"http_listen_port": HTTP_LISTEN_PORT,
}
if self.http_tls:
_server["http_tls_config"] = {
"cert_file": CERT_FILE, # HTTP server cert path.
"key_file": KEY_FILE, # HTTP server key path.
}
return _server
@property
def _storage_config(self) -> dict:
return {
"boltdb": {"directory": BOLTDB_DIR},
"filesystem": {"directory": CHUNKS_DIR},
}
@property
def _limits_config(self) -> dict:
# Ref: https://grafana.com/docs/loki/latest/configure/#limits_config
return {
# For convenience, we use an integer but Loki takes a float
"ingestion_rate_mb": float(self.ingestion_rate_mb),
"ingestion_burst_size_mb": float(self.ingestion_burst_size_mb),
# The per-stream limits are intentionally set to match the per-user limits, to simplify UX and address the
# case of one stream per user.
"per_stream_rate_limit": f"{self.ingestion_rate_mb}MB",
"per_stream_rate_limit_burst": f"{self.ingestion_burst_size_mb}MB",
# This charmed operator is intended for running a single loki instances, so we don't need to split queries
# https://community.grafana.com/t/too-many-outstanding-requests-on-loki-2-7-1/78249/9
"split_queries_by_interval": "0",
}
@property
def _query_range(self) -> dict:
# Ref: https://grafana.com/docs/loki/latest/configure/#query_range
return {
"parallelise_shardable_queries": False,
"results_cache": {
"cache": {
"embedded_cache": {
# https://community.grafana.com/t/too-many-outstanding-requests-on-loki-2-7-1/78249/11
"enabled": True
}
}
},
}
@property
def _chunk_store_config(self) -> dict:
# Ref: https://grafana.com/docs/loki/latest/configure/#chunk_store_config
return {
"chunk_cache_config": {
"embedded_cache": {
# https://community.grafana.com/t/too-many-outstanding-requests-on-loki-2-7-1/78249/11
"enabled": True
}
}
}
@property
def _frontend(self) -> dict:
# Ref: https://grafana.com/docs/loki/latest/configure/#frontend
return {
# Maximum number of outstanding requests per tenant per frontend; requests beyond this error with HTTP 429.
# Default is 2048, but 8cpu16gb can ingest ~3 times more, so set to 4x.
"max_outstanding_per_tenant": 8192,
# Compress HTTP responses.
"compress_responses": True,
}
@property
def _querier(self) -> dict:
# Ref: https://grafana.com/docs/loki/latest/configure/#querier
return {
# The maximum number of concurrent queries allowed. Default is 10.
"max_concurrent": 20,
}