Skip to content

Commit d68daa7

Browse files
authored
Added top referrers and top paths to metrics. (#4)
1 parent 5270ae8 commit d68daa7

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

github-traffic.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@
4242
["repository"],
4343
)
4444

45+
gh_traffic_top_paths = Gauge(
46+
"github_traffic_top_paths",
47+
"Number of visits to top paths",
48+
["repository", "path", "title"],
49+
)
50+
51+
gh_traffic_top_unique_paths = Gauge(
52+
"github_traffic_top_unique_paths",
53+
"Number of unique visits to top paths",
54+
["repository", "path", "title"],
55+
)
56+
57+
gh_traffic_top_referrers = Gauge(
58+
"github_traffic_top_referrers",
59+
"Number of visits from top referrers",
60+
["repository", "referrer"],
61+
)
62+
63+
gh_traffic_top_unique_referrers = Gauge(
64+
"github_traffic_top_unique_referrers",
65+
"Number of unique visits from top referrers",
66+
["repository", "referrer"],
67+
)
68+
4569
gh_traffic_stars = Gauge(
4670
"github_traffic_stars",
4771
"Number of stars",
@@ -98,7 +122,33 @@ def job_function():
98122
toShow['stars'] = repo.stargazers_count
99123
except Exception as e:
100124
logger.error(f"Failed to get stars on {repo_name}: {e}")
101-
125+
try:
126+
# Top Paths
127+
total_count = 0
128+
total_unique_count = 0
129+
for path in repo.get_top_paths():
130+
gh_traffic_top_paths.labels(repo_name, path.path, path.title).set(path.count)
131+
total_count = total_count + path.count
132+
gh_traffic_top_unique_paths.labels(repo_name, path.path, path.title).set(path.uniques)
133+
total_unique_count = total_unique_count + path.uniques
134+
toShow['sum(top_paths)'] = total_count
135+
toShow['sum(top_unique_paths)'] = total_unique_count
136+
except Exception as e:
137+
logger.error(f"Failed to get top paths on {repo_name}: {e}")
138+
try:
139+
# Top Referrers
140+
total_count = 0
141+
total_unique_count = 0
142+
for referrer in repo.get_top_referrers():
143+
gh_traffic_top_referrers.labels(repo_name, referrer.referrer).set(referrer.count)
144+
total_count = total_count + referrer.count
145+
gh_traffic_top_unique_referrers.labels(repo_name, referrer.referrer).set(referrer.uniques)
146+
total_unique_count = total_unique_count + referrer.uniques
147+
toShow['sum(top_referrers)'] = total_count
148+
toShow['sum(top_unique_referrers)'] = total_unique_count
149+
except Exception as e:
150+
logger.error(f"Failed to get top referrers on {repo_name}: {e}")
151+
102152
logger.info('Gather insights', extra={"context": toShow})
103153

104154

0 commit comments

Comments
 (0)