Skip to content

Commit

Permalink
Merge pull request #1608 from tgxworld/support_prometheus_addr
Browse files Browse the repository at this point in the history
Add `--prometheus_addr` command line parameter
  • Loading branch information
jertel authored Jan 24, 2025
2 parents 866fc7a + 9003ed7 commit c1ca69e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
## New features
- [Helm] Add optional liveness and readiness probe - [#1604](https://github.com/jertel/elastalert2/pull/1604) - @aizerin
- Add `include_rule_params_in_matches` rule parameter to enable copying of specific rule params into match data - [#1605](https://github.com/jertel/elastalert2/pull/1605) - @jertel
- [Helm] Add `--prometheus_addr` command line parameter to allow binding the Prometheus metrics server on a different host address - [#1608](https://github.com/jertel/elastalert2/pull/1608) - @tgxworld

## Other changes
- [Docs] Add missing documentation of the `aggregation_alert_time_compared_with_timestamp_field` option. - [#1588](https://github.com/jertel/elastalert2/pull/1588) - @nicolasnovelli
Expand Down
2 changes: 2 additions & 0 deletions docs/source/flags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ based on changes to their config files.
``--prometheus_port`` exposes ElastAlert 2 `Prometheus metrics <https://elastalert2.readthedocs.io/en/latest/recipes/exposing_rule_metrics.html>`_ on the specified
port. Prometheus metrics disabled by default.

``--prometheus_addr`` allows you to specify the host address that the Prometheus metrics server will bind to.

``--rule <rule.yaml>`` will only run the given rule. The rule file may be a
complete file path or a filename in ``rules_folder`` or its subdirectories.

Expand Down
6 changes: 6 additions & 0 deletions docs/source/recipes/exposing_rule_metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ To expose ElastAlert rule metrics on port ``9979`` run the following command:
$ elastalert --config config.yaml --prometheus_port 9979
The ``--prometheus_addr`` configuration flag can also be used to bind the Prometheus metrics server to a different host address.

.. code-block:: console
$ elastalert --config config.yaml --prometheus_port 9979 --prometheus_addr "::"
Rule Metrics
------------

Expand Down
9 changes: 9 additions & 0 deletions elastalert/elastalert.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def parse_args(self, args):
help='Enable logging from Elasticsearch queries as curl command. Queries will be logged to file. Note that '
'this will incorrectly display localhost:9200 as the host/port')
parser.add_argument('--prometheus_port', type=int, dest='prometheus_port', help='Enables Prometheus metrics on specified port.')

parser.add_argument(
'--prometheus_addr',
type=str,
dest='prometheus_addr',
default=None,
help='Address to bind the Prometheus metrics server on.')

self.args = parser.parse_args(args)

def __init__(self, args):
Expand Down Expand Up @@ -175,6 +183,7 @@ def __init__(self, args):
self.statsd = None
self.add_metadata_alert = self.conf.get('add_metadata_alert', False)
self.prometheus_port = self.args.prometheus_port
self.prometheus_addr = self.args.prometheus_addr
self.show_disabled_rules = self.conf.get('show_disabled_rules', True)
self.pretty_ts_format = self.conf.get('custom_pretty_ts_format')

Expand Down
3 changes: 2 additions & 1 deletion elastalert/prometheus_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class PrometheusWrapper:

def __init__(self, client):
self.prometheus_port = client.prometheus_port
self.prometheus_addr = client.prometheus_addr
self.run_rule = client.run_rule
self.writeback = client.writeback

Expand All @@ -24,7 +25,7 @@ def __init__(self, client):
self.prom_alerts_silenced = prometheus_client.Counter('elastalert_alerts_silenced', 'Number of silenced alerts', ['rule_name'])

def start(self):
prometheus_client.start_http_server(self.prometheus_port)
prometheus_client.start_http_server(port=self.prometheus_port, addr=self.prometheus_addr)

def metrics_run_rule(self, rule, endtime, starttime=None):
""" Increment counter every time rule is run """
Expand Down

0 comments on commit c1ca69e

Please sign in to comment.