本次用开源三件套:Prometheus+Grafana+Node Exporter(通用版),适配90%以上接入广告SDK/联盟的网站、App(后端改下Exporter也能用,但先从通用版学,后端补全见文末)
不管你是Windows/Mac/Linux,先装Docker Desktop(Mac/Windows)或Docker CE(Linux),Linux用这个一键脚本:
```bash curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh sudo usermod -aG docker $USER ```重启终端后验证:
```bash docker --version docker-compose --version ```找个空文件夹,新建docker-compose.yml,粘贴以下完整内容:

同文件夹新建prometheus.yml,粘贴以下内容,联盟/SDK数据我们后面用静态JSON或者HTTP接口对接(本次先讲静态模拟,HTTP接口见后续拓展):
http://你的服务器IP:9090/targets(本地localhost)、http://你的服务器IP:3000(本地localhost),Prometheus的targets里node_exporter必须显示UP真实数据用联盟/SDK的API(比如穿山甲、优量汇、AdSense都有),本次先写1个Python3模拟上报脚本(可直接复制运行,快速看效果):
mock_ad_metrics.pyad_revenue_total,点击Execute,能看到上升曲线就成功了http://prometheus:9090(因为是Docker网络,不用填服务器IP)点击左侧Dashboards → New → Import,粘贴Grafana官方现成的通用Exporter+自定义指标的ID?不对,本次用自定义指标,直接给个简单的JSON配置片段,粘贴到Import via panel json框里:
```json { "annotations": { "list": [] }, "editable": true, "fiscalYearStartMonth": 0, "graphTooltip": 0, "id": null, "links": [], "liveNow": false, "panels": [ { "datasource": { "type": "prometheus", "uid": "pbprometheus" }, "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "short" }, "overrides": [] }, "gridPos": { "h": 4, "w": 6, "x": 0, "y": 0 }, "id": 1, "options": { "colorMode": "value", "graphMode": "area", "justifyMode": "auto", "orientation": "auto", "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false }, "textMode": "auto" }, "targets": [ { "expr": "ad_daily_active_users", "refId": "A" } ], "title": "日活用户数", "type": "stat" }, { "datasource": { "type": "prometheus", "uid": "pbprometheus" }, "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "short" }, "overrides": [] }, "gridPos": { "h": 4, "w": 6, "x": 6, "y": 0 }, "id": 2, "options": { "colorMode": "value", "graphMode": "area", "justifyMode": "auto", "orientation": "auto", "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false }, "textMode": "auto" }, "targets": [ { "expr": "ad_impressions_total / ad_requests_total 100", "refId": "A" } ], "title": "广告填充率(%)", "type": "stat" }, { "datasource": { "type": "prometheus", "uid": "pbprometheus" }, "fieldConfig": { "defaults": { "color": { "mode": "thresholds" }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] }, "unit": "currencyCNY" }, "overrides": [] }, "gridPos": { "h": 4, "w": 6, "x": 12, "y": 0 }, "id": 3, "options": { "colorMode": "value", "graphMode": "area", "justifyMode": "auto", "orientation": "auto", "reduceOptions": { "calcs": ["lastNotNull"], "fields": "", "values": false }, "textMode": "auto" }, "targets": [ { "expr": "ad_revenue_total", "refId": "A" } ], "title": "今日累计收入(元)", "type": "stat" }, { "datasource": { "type": "prometheus", "uid": "pbprometheus" }, "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" }, "custom": { "axisBorderShow": false, "axisCenteredZero": false, "axisColorMode": "text", "axisLabel": "", "axisPlacement": "auto", "barAlignment": 0, "drawStyle": "line", "fillOpacity": 10, "gradientMode": "none", "hideFrom": { "legend": false, "tooltip": false, "viz": false }, "insertNulls": false, "lineInterpolation": "linear", "lineWidth": 1, "pointSize": 5, "scaleDistribution": { "type": "linear" }, "showPoints": "auto", "spanNulls": false, "stacking": { "group": "A", "mode": "none" }, "thresholdsStyle": { "mode": "off" } }, "mappings": [], "thresholds": { "mode": "absolute", "steps": [ { "color": "green", "value": null } ] } }, "overrides": [] }, "gridPos": { "h": 8, "w": 24, "x": 0, "y": 4 }, "id": 4, "options": { "legend": { "calcs": [], "displayMode": "list", "placement": "bottom", "showLegend": true }, "tooltip": { "mode": "single", "sort": "none" } }, "targets": [ { "expr": "rate(ad_revenue_total[1h])", "legendFormat": "每小时收入(元)", "refId": "A" } ], "title": "每小时收入趋势", "type": "timeseries" } ], "refresh": "5s", "schemaVersion": 39, "tags": [], "templating": { "list": [] }, "time": { "from": "now-24h", "to": "now" }, "timepicker": {}, "timezone": "", "title": "广告变现全链路监控看板", "uid": "admonboard", "version": 1, "weekStart": "" } ```ad_impressions_total / ad_requests_total 100,Condition选“Below”,Value填50以AdSense为例,用Google Ads Python SDK替换刚才的模拟脚本,步骤是:
ads易频IT社区是综合性互联网IT技术门户网站,专注分享网络技术、服务器运维、网络安全、编程开发、系统架构、云计算、大数据等行业干货,实时更新IT行业资讯、零基础教程、实战案例,为IT从业者、技术爱好者提供专业的学习交流平台。
Copyright © 2021-2026 易频IT社区. All Rights Reserved. 备案号:闽ICP备2023013482号 网站地图