-
-
Save adripo/469d6cabf1a25f37dfd799d766ff24ca to your computer and use it in GitHub Desktop.
Export Jellyfin playback statistics to Prometheus and Grafana. See https://sno.ws/jellyfin-stats for more info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
modules: | |
jellyfin: | |
headers: | |
# The Token value here needs to be an API key generated from the | |
# Jellyfin admin panel. It's hard-coded here but I'm sure there's | |
# a better way | |
Authorization: MediaBrowser Token=ADD_TOKEN_HERE | |
Content-Type: application/json | |
accept: application/json | |
# This is the custom SQL query from live-stats.sql, but condensed to a single line | |
# NOTE: The string escaping/lack of newlines is required for the exporter to work | |
body: | |
content: | | |
{"CustomQueryString": "SELECT unixepoch(DATETIME(DateCreated, \"+\" || PlayDuration || \" seconds\")) AS Timestamp, UserId, ItemType, ItemName, PlaybackMethod, ClientName, DeviceName, PlayDuration, TRUE AS IsActive FROM PlaybackActivity WHERE DATETIME(DateCreated, \"+\" || PlayDuration || \" seconds\") >= DATETIME(\"now\", \"localtime\", \"-60 seconds\")", "ReplaceUserId": true} | |
metrics: | |
- name: jellyfin | |
type: object | |
help: User playback metrics from Jellyfin | |
path: '{ .results[*] }' | |
# Optionally use the SQL-generated timestamp instead of | |
# the Prometheus "query collected at" timestamp | |
epochTimestamp: '{ [0] }' | |
# The JSON parsing here is necessary to get a clean and labelled output | |
labels: | |
user_name: '{ [1] }' | |
item_type: '{ [2] }' | |
item_name: '{ [3] }' | |
playback_method: '{ [4] }' | |
client_name: '{ [5] }' | |
device_name: '{ [6] }' | |
values: | |
play_duration: '{ [7] }' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
-- Return the timestamp of the record/stream to | |
-- replace the default Prometheus timestamp | |
unixepoch(DATETIME( | |
DateCreated, "+" || PlayDuration || " seconds" | |
)) AS Timestamp, | |
UserId, | |
ItemType, | |
ItemName, | |
PlaybackMethod, | |
ClientName, | |
DeviceName, | |
PlayDuration, | |
TRUE AS IsActive | |
FROM PlaybackActivity | |
-- Only return "active" streams, i.e. ones playing | |
-- in the last 60 seconds | |
WHERE DATETIME(DateCreated, "+" || PlayDuration || " seconds") >= | |
DATETIME("now", "localtime", "-60 seconds") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scrape_configs: | |
- job_name: json | |
metrics_path: /probe | |
params: | |
# The name of the module defined by json-exporter-config.yaml | |
module: [jellyfin] | |
static_configs: | |
- targets: | |
# The Jellyfin Playback Reporting Plugin custom query API endpoint | |
- https://jellyfin.example.com/user_usage_stats/submit_custom_query | |
relabel_configs: | |
- source_labels: [__address__] | |
target_label: __param_target | |
- source_labels: [__param_target] | |
target_label: instance | |
- target_label: __address__ | |
replacement: HOSTNAME:9115 # The exporter's hostname:port |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment