Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize docker readings behavior #16

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import requests
import json
import os
import jwt
from prometheus_client.core import GaugeMetricFamily, REGISTRY
from prometheus_client import start_http_server

Expand All @@ -29,6 +30,9 @@ def __init__(self, verbose, username, password):
self.password = password

self.verbose = verbose

self.previous_limit = None
self.previous_remaining = None

def do_verbose(self, text):
if self.verbose:
Expand Down Expand Up @@ -67,6 +71,12 @@ def get_token(self):
if not token:
raise Exception('Cannot obtain token from Docker Hub. Please try again!')

# extract content from jwt token
jwtContent = jwt.decode(token, options={"verify_signature": False})
if not self.previous_limit:
self.previous_limit = jwtContent.get('access')[0].get('parameters').get('pull_limit')
self.previous_remaining = self.previous_limit

return token

## Test against registry
Expand Down Expand Up @@ -102,11 +112,21 @@ def get_registry_limits(self):
def collect(self):
(limit, remaining, reset) = self.get_registry_limits()

if len(str(remaining)) == 0:
remaining = self.previous_remaining
else:
self.previous_remaining = remaining

gr = GaugeMetricFamily("dockerhub_limit_remaining_requests_total", 'Docker Hub Rate Limit Remaining Requests', labels=['limit'])
gr.add_metric(["remaining_requests_total"], remaining)

yield gr

if len(str(limit)) == 0:
limit = self.previous_limit
else:
self.previous_limit = limit

gl = GaugeMetricFamily("dockerhub_limit_max_requests_total", 'Docker Hub Rate Limit Maximum Requests', labels=['limit'])
gl.add_metric(["max_requests_total"], limit)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests
prometheus_client
pyjwt