Giter VIP home page Giter VIP logo

holiday_exporter's Introduction

Holiday Exporter

Version License Docker Downloads Docker Build Status

A simple python exporter that exports holidays to Prometheus to help with Alerting. This exporter uses the awesome python-holidays library

The Problem

I want to fire some alerts on workdays only, but Prometheus/Alertmanager doesnt support it!

You are not alone

Solution #1

Create a recording rule on Prometheus with all holidays. This can be really complicated with non fixed holidays, like Easter.

Implementation sugestion

Solution #2

Use this exporter and include it in your expression!

Here is a quick how to use this exporter:

Create an alert rule in Prometheus:

- alert: Is_Holiday
  expr: is_holiday > 0
  labels:
    severity: warning

And create an inhibit_rules in Alertmanager:

- source_match:
    alertname: Is_Holiday
  target_match:
    severity: warning
  equal:
  - severity

With this configuration, any alert with a label "severity = warning" will be inhibited by the holiday exporter. You can go beyond and put some work hours as well:

- alert: Is_Work_Hours
  expr:
    is_holiday > 0
    or
    hour() < 8 + (6 - scalar(is_daylight_saving_time))
    or
    hour() > 17 + (6 - scalar(is_daylight_saving_time))
    or
    day_of_week(vector(time() + (3600 * (6 - scalar(is_daylight_saving_time))))) == 0
    or
    day_of_week(vector(time() + (3600 * (6 - scalar(is_daylight_saving_time))))) == 6
  labels:
    severity: warning

In this rule, my timezone is -6h and it will be triggered if is a holiday, or current hour is > 17 and < 8, or day of week is Saturday or Sunday. When this alert fires, it will inhibit any rule with a label "severity = warning".

How to run:

git clone https://github.com/allangood/holiday_exporter.git
cd holiday_exporter
vi includes/holiday_exporter.yaml
docker build -t holiday_exporter .
docker run -d -p 9137:9137 --name holiday holiday_exporter

Or you can specify your own configuration file: Create your YAML file:

main:
 port: 9137

# Countries, states and provinces accordingly to https://pypi.org/project/holidays
holidays:
  - country: "CA"
    province: "ON"
  - country: "US"
    state: "CA"

Run the holiday_exporter container:

docker run -d -p 9137:9137 -v my_config_file.yaml:/etc/holiday_exporter.yaml -e TZ=America/New_York --restart unless-stopped --name holiday_exporter allangood/holiday_exporter

Version 1.1 and later of this exporter supports custom holidays. You just have to add a section like this to your configuration file:

custom_holidays:
  # Dates must be in ISO format: YYYY-MM-DD
  # Use temaplte {YYYY} for year and {MM} for month
  - date: "{YYYY}-01-01"
    description: "Some event that happens every year"
  - date: "{YYYY}-{MM}-01"
    description: "Some event that repeats every month at specific day"
  - date: "2019-12-17"
    description: "Some day this year only"

Then configure Prometheus to scrape your server:

- job_name: holiday_exporter
  scrape_interval: 10s
  scrape_timeout: 5s
  metrics_path: "/"
  static_configs:
  - targets:
    - <your_server_address>:9137

Metrics exposed:

Metric Possible Values
is_holiday 1 => True / 0 => False
is_daylight_saving 1 => True / 0 => False

Sample

# HELP is_holiday Boolean value if today is a statutory holiday
# TYPE is_holiday gauge
is_holiday{country="CA",province="ON",state="None"} 0.0
is_holiday{country="US",province="None",state="CA"} 0.0
is_holiday{country="Custom",province="Custom",state="Custom"} 1.0
# HELP is_daylightsavings Boolean value if today is local daylight saving time
# TYPE is_daylightsavings gauge
is_daylight_savings 0.0

Alternatives

This is another solution

Author:

Allan Good

holiday_exporter's People

Contributors

allangood avatar dependabot[bot] avatar stefancrain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

hiredscore nrgeil

holiday_exporter's Issues

Cannot change timezone when run as a container

When we run holiday-exporter as a container and the time defaults to UTC, so that is_daylight_saving_time is always evaluated to 0 even we're using DST.

/ $ python -c  "import time;print(time.localtime())"
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=27, tm_hour=15, tm_min=14, tm_sec=22, tm_wday=4, tm_yday=87, tm_isdst=0)

Simply starting the container with TZ set won't work either

docker run -e TZ=America/New_York holiday_exporter

as an alpine based image requires tzdata.

Could we have RUN apk add --no-cache tzdata added to Dockerfile please?

Thanks,
Map

Invalid isoformat string when Month < 10

Error

Traceback (most recent call last):
  File "/usr/bin/holiday_exporter.py", line 55, in <module>
    if date.fromisoformat(isodate) == date.today():
ValueError: Invalid isoformat string: '2020-1-01'

Using included yaml config (problem line)

...
  - date: "{YYYY}-{MM}-01"
    description: "Some event that repeats every month at specific day"

Isodate expects {MM} but now.month only gives us 1 causing the failure above.

 isodate = custom_holiday['date'].upper().format(YYYY=now.year, MM=now.month)

support for multiple sets of custom holidays

The use case is to accommodate the needs of different organizations, which follow different holiday schedules.

I would imagine the configs go like this:

custom_holidays:
  - name: test
    region: us
    organization: A
    schedule:
    - date: "2019-12-17"
      description: "Some day this year only"

Other than the mandatory schedule field, the others are free-form key-value pairs (containing at least 1 pair), which translate to the labels of the is_holiday metric. Perhaps we can make name mandatory as well.

@allangood Do you think this is can be something we add?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.