Skip to content

Commit

Permalink
Merge pull request #85 from rg2011/master
Browse files Browse the repository at this point in the history
Fixes #82 - Support for custom HTTP headers
  • Loading branch information
braedon authored Sep 29, 2020
2 parents d3c865c + 22cd35f commit 563e8b5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions prometheus_es_exporter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,23 @@ def indices_stats_fields_parser(ctx, param, value):
return value.split(',')


def split_http_header(header_string):
"""Splits a colon-separated string into header and value"""
parts = tuple(part.strip() for part in header_string.split(":", 1))
if len(parts) != 2 or any(part == '' for part in parts):
msg = "Invalid header '{}'. Use colon to separate name and value".format(header_string)
raise click.BadParameter(msg)
return parts


def http_headers_parser(ctx, param, headers):
"""Turn header list into a dict {header: value, ...}"""
if headers is None:
return None

return dict(split_http_header(header_string) for header_string in headers)


def configparser_enum_conv(enum):
lower_enums = tuple(e.lower() for e in enum)

Expand Down Expand Up @@ -412,6 +429,13 @@ def conv(value):
@click.option('--basic-password',
help='Password for basic authentication with nodes. '
'Must be specified if "--basic-user" is provided.')
@click.option('--header', '-H',
multiple=True,
callback=http_headers_parser,
help='HTTP header to include in requests to ElasticSearch cluster. '
'Header name and value should be separated by colon, e.g. '
'"Authorization: Bearer xxxxx". Several headers can be added '
'by repeating the -H parameter.')
@click.option('--port', '-p', default=9206,
help='Port to serve the metrics endpoint on. (default: 9206)')
@click.option('--query-disable', default=False, is_flag=True,
Expand Down Expand Up @@ -539,10 +563,12 @@ def cli(**options):
ca_certs=options['ca_certs'],
client_cert=options['client_cert'],
client_key=options['client_key'],
headers=options['header'],
http_auth=http_auth)
else:
es_client = Elasticsearch(es_cluster,
verify_certs=False,
headers=options['header'],
http_auth=http_auth)

scheduler = None
Expand Down

0 comments on commit 563e8b5

Please sign in to comment.