Skip to content

Instantly share code, notes, and snippets.

@adripo
Forked from dfsnow/json-exporter-config.yaml
Created December 6, 2023 03:40
Show Gist options
  • Save adripo/469d6cabf1a25f37dfd799d766ff24ca to your computer and use it in GitHub Desktop.
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
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] }'
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")
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