Skip to content

Commit

Permalink
add fancy logs
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel González Lopes <[email protected]>
  • Loading branch information
dgzlopes committed Sep 12, 2021
1 parent 9d1953b commit 262725b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 20 additions & 8 deletions github-traffic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger
from prometheus_client import start_http_server, Gauge
from logfmt_logger import getLogger

ORG_NAME = config("ORG_NAME")
REPO_TYPE = config("REPO_TYPE", default="public")
Expand All @@ -14,6 +15,8 @@

github = Github(GITHUB_TOKEN)

logger = getLogger("github_traffic")

gh_traffic_views = Gauge(
"github_traffic_views",
"Number of views",
Expand Down Expand Up @@ -63,30 +66,39 @@ def job_function():
for repo in repositories:
if REPO_NAME_CONTAINS in repo.name:
repo_name = repo.name
print(repo.name)
toShow = dict()
toShow['repo'] = repo_name

try:
# Views stats
data_views = repo.get_views_traffic(per="day")
print(data_views["views"][-1].count)
gh_traffic_views.labels(repo_name).set(data_views["views"][-1].count)
print(data_views["views"][-1].uniques)
gh_traffic_unique_views.labels(repo_name).set(data_views["views"][-1].uniques)

toShow['views'] = data_views["views"][-1].count
toShow['unique_views'] = data_views["views"][-1].uniques
except Exception as e:
logger.error(f"Failed to extract views on {repo_name}: {e}")
try:
# Clones stats
data_clones = repo.get_clones_traffic(per="day")
print(data_clones["clones"][-1].count)
gh_traffic_clones.labels(repo_name).set(data_clones["clones"][-1].count)
print(data_clones["clones"][-1].uniques)
gh_traffic_unique_clones.labels(repo_name).set(data_clones["clones"][-1].uniques)

toShow['clones'] = data_clones["clones"][-1].count
toShow['unique_clones'] = data_clones["clones"][-1].uniques
except Exception as e:
logger.error(f"Failed to extract clones on {repo_name}: {e}")
try:
# Star stats
gh_traffic_stars.labels(repo_name).set(repo.stargazers_count)
toShow['stars'] = repo.stargazers_count
except Exception as e:
print(f"Failed to extract stats on {repo_name}: {e}")
logger.error(f"Failed to get stars on {repo_name}: {e}")

logger.info('Gather insights', extra={"context": toShow})


if __name__ == "__main__":
logger.info('Github traffic is running!')
# Start up the server to expose the metrics.
start_http_server(8001)
# Schedule run the job on startup.
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ apscheduler==3.7.0
requests==2.25.0
python-decouple==3.3
prometheus-client==0.9.0
pygithub==1.51
pygithub==1.51
logfmt-logger==0.1.2

0 comments on commit 262725b

Please sign in to comment.