diff --git a/README.md b/README.md index 439a394..4607253 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,34 @@ -# 👷 `worker-template` Hello World +# Cloudflare metrics worker -A template for kick starting a Cloudflare worker project. +Send your page views from [Cloudflare worker](https://developers.cloudflare.com/workers/) to InfluxDB. -[`index.js`](https://github.com/cloudflare/worker-template/blob/master/index.js) is the content of the Workers script. +![Dashboard views](static/dashboard.png) -#### Wrangler +## Requirements -To generate using [wrangler](https://github.com/cloudflare/wrangler) +1. Your site need to be setup behind Cloudflare CDN. +2. You need to setup InfluxDB with external access (make sure you have set [authentication](https://docs.influxdata.com/influxdb/v1.7/administration/authentication_and_authorization/#set-up-authentication)) + 1. Make sure InfluxDB is hosted under [supported port](https://blog.cloudflare.com/cloudflare-now-supporting-more-ports/) for Workers. Best option is 80 or 443. + +## How to use + +1. Install wrangler package ``` -wrangler generate projectname https://github.com/cloudflare/worker-template +npm i @cloudflare/wrangler -g ``` -#### Serverless +2. Copy example files + +``` +cp .env.example .env +cp wrangler.toml.example wrangler.toml +``` + +3. Deploy your worker to a site with wrangler + +``` +wrangler publish --env production +``` -To deploy using serverless add a [`serverless.yml`](https://serverless.com/framework/docs/providers/cloudflare/) file. +4. (Optional) If you're using Grafana with InfluxDB, then you can import [example Dashboard](static/dashboard.json) from first screen. diff --git a/static/dashboard.json b/static/dashboard.json new file mode 100644 index 0000000..2846e12 --- /dev/null +++ b/static/dashboard.json @@ -0,0 +1,1094 @@ +{ + "__inputs": [ + { + "name": "DS_REQUESTS", + "label": "requests", + "description": "", + "type": "datasource", + "pluginId": "influxdb", + "pluginName": "InfluxDB" + } + ], + "__requires": [ + { + "type": "grafana", + "id": "grafana", + "name": "Grafana", + "version": "6.6.0" + }, + { + "type": "panel", + "id": "grafana-clock-panel", + "name": "Clock", + "version": "1.0.3" + }, + { + "type": "panel", + "id": "grafana-piechart-panel", + "name": "Pie Chart", + "version": "1.4.0" + }, + { + "type": "panel", + "id": "grafana-worldmap-panel", + "name": "Worldmap Panel", + "version": "0.2.1" + }, + { + "type": "panel", + "id": "graph", + "name": "Graph", + "version": "" + }, + { + "type": "datasource", + "id": "influxdb", + "name": "InfluxDB", + "version": "1.0.0" + }, + { + "type": "panel", + "id": "table", + "name": "Table", + "version": "" + } + ], + "annotations": { + "list": [ + { + "builtIn": 1, + "datasource": "-- Grafana --", + "enable": true, + "hide": true, + "iconColor": "rgba(0, 211, 255, 1)", + "name": "Annotations & Alerts", + "type": "dashboard" + } + ] + }, + "editable": true, + "gnetId": null, + "graphTooltip": 0, + "id": null, + "iteration": 1581572429214, + "links": [], + "panels": [ + { + "aliasColors": {}, + "bars": true, + "dashLength": 10, + "dashes": false, + "datasource": "${DS_REQUESTS}", + "fill": 1, + "fillGradient": 0, + "gridPos": { + "h": 11, + "w": 12, + "x": 0, + "y": 0 + }, + "hiddenSeries": false, + "id": 2, + "interval": "1h", + "legend": { + "alignAsTable": true, + "avg": false, + "current": true, + "max": false, + "min": false, + "rightSide": true, + "show": true, + "sort": "total", + "sortDesc": true, + "total": true, + "values": true + }, + "lines": false, + "linewidth": 1, + "nullPointMode": "null", + "options": { + "dataLinks": [] + }, + "percentage": false, + "pointradius": 2, + "points": false, + "renderer": "flot", + "seriesOverrides": [], + "spaceLength": 10, + "stack": true, + "steppedLine": false, + "targets": [ + { + "alias": "$tag_url", + "groupBy": [ + { + "params": [ + "$Interval" + ], + "type": "time" + }, + { + "params": [ + "url" + ], + "type": "tag" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "views", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "duration" + ], + "type": "field" + }, + { + "params": [], + "type": "count" + } + ] + ], + "tags": [ + { + "key": "hostname", + "operator": "=~", + "value": "/^$Hostname$/" + }, + { + "condition": "AND", + "key": "pathname", + "operator": "=~", + "value": "/\\/$/" + }, + { + "condition": "AND", + "key": "country", + "operator": "=~", + "value": "/^$Country$/" + } + ] + } + ], + "thresholds": [], + "timeFrom": null, + "timeRegions": [], + "timeShift": null, + "title": "Page views", + "tooltip": { + "shared": true, + "sort": 0, + "value_type": "individual" + }, + "type": "graph", + "xaxis": { + "buckets": null, + "mode": "time", + "name": null, + "show": true, + "values": [] + }, + "yaxes": [ + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + }, + { + "format": "short", + "label": null, + "logBase": 1, + "max": null, + "min": null, + "show": true + } + ], + "yaxis": { + "align": false, + "alignLevel": null + } + }, + { + "circleMaxSize": 30, + "circleMinSize": 2, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "datasource": "${DS_REQUESTS}", + "decimals": 0, + "esMetric": "Count", + "gridPos": { + "h": 11, + "w": 6, + "x": 12, + "y": 0 + }, + "hideEmpty": false, + "hideZero": false, + "id": 10, + "initialZoom": 1, + "locationData": "countries", + "mapCenter": "(0°, 0°)", + "mapCenterLatitude": 0, + "mapCenterLongitude": 0, + "maxDataPoints": 1, + "mouseWheelZoom": false, + "options": {}, + "showLegend": true, + "stickyLabels": false, + "tableQueryOptions": { + "geohashField": "geohash", + "latitudeField": "latitude", + "longitudeField": "longitude", + "metricField": "metric", + "queryType": "geohash" + }, + "targets": [ + { + "alias": "$tag_country", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "country" + ], + "type": "tag" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "views", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "duration" + ], + "type": "field" + }, + { + "params": [], + "type": "count" + } + ] + ], + "tags": [ + { + "key": "hostname", + "operator": "=~", + "value": "/^$Hostname$/" + }, + { + "condition": "AND", + "key": "pathname", + "operator": "=~", + "value": "/\\/$/" + } + ] + } + ], + "thresholds": "0,10", + "timeFrom": null, + "timeShift": null, + "title": "World views", + "type": "grafana-worldmap-panel", + "unitPlural": "", + "unitSingle": "", + "valueName": "total" + }, + { + "columns": [ + { + "text": "Total", + "value": "total" + } + ], + "datasource": "${DS_REQUESTS}", + "fontSize": "100%", + "gridPos": { + "h": 11, + "w": 6, + "x": 18, + "y": 0 + }, + "id": 6, + "options": {}, + "pageSize": null, + "showHeader": true, + "sort": { + "col": 1, + "desc": true + }, + "styles": [ + { + "alias": "Time", + "align": "auto", + "dateFormat": "YYYY-MM-DD HH:mm:ss", + "link": false, + "pattern": "Time", + "type": "date" + }, + { + "alias": "", + "align": "right", + "colorMode": null, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "decimals": 0, + "pattern": "/.*/", + "thresholds": [], + "type": "number", + "unit": "short" + } + ], + "targets": [ + { + "alias": "$tag_pathname", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "pathname" + ], + "type": "tag" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "views", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "duration" + ], + "type": "field" + }, + { + "params": [], + "type": "count" + } + ] + ], + "tags": [ + { + "key": "hostname", + "operator": "=~", + "value": "/^$Hostname$/" + }, + { + "condition": "AND", + "key": "pathname", + "operator": "=~", + "value": "/\\/$/" + } + ] + } + ], + "timeFrom": "30d", + "timeShift": null, + "title": "Popular pages", + "transform": "timeseries_aggregations", + "type": "table" + }, + { + "columns": [ + { + "text": "Total", + "value": "total" + } + ], + "datasource": "${DS_REQUESTS}", + "fontSize": "100%", + "gridPos": { + "h": 12, + "w": 4, + "x": 0, + "y": 11 + }, + "id": 5, + "options": {}, + "pageSize": null, + "showHeader": true, + "sort": { + "col": 1, + "desc": true + }, + "styles": [ + { + "alias": "Time", + "align": "auto", + "dateFormat": "YYYY-MM-DD HH:mm:ss", + "link": false, + "pattern": "Time", + "type": "date" + }, + { + "alias": "", + "align": "right", + "colorMode": null, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "decimals": 0, + "pattern": "/.*/", + "thresholds": [], + "type": "number", + "unit": "short" + } + ], + "targets": [ + { + "alias": "$tag_pathname", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "pathname" + ], + "type": "tag" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "views", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "duration" + ], + "type": "field" + }, + { + "params": [], + "type": "count" + } + ] + ], + "tags": [ + { + "key": "hostname", + "operator": "=~", + "value": "/^$Hostname$/" + }, + { + "condition": "AND", + "key": "pathname", + "operator": "=~", + "value": "/\\/$/" + } + ] + } + ], + "timeFrom": "7d", + "timeShift": null, + "title": "Popular pages", + "transform": "timeseries_aggregations", + "type": "table" + }, + { + "columns": [ + { + "text": "Total", + "value": "total" + } + ], + "datasource": "${DS_REQUESTS}", + "fontSize": "100%", + "gridPos": { + "h": 12, + "w": 4, + "x": 4, + "y": 11 + }, + "id": 7, + "options": {}, + "pageSize": null, + "showHeader": true, + "sort": { + "col": 1, + "desc": true + }, + "styles": [ + { + "alias": "Time", + "align": "auto", + "dateFormat": "YYYY-MM-DD HH:mm:ss", + "link": false, + "pattern": "Time", + "type": "date" + }, + { + "alias": "", + "align": "right", + "colorMode": null, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "decimals": 0, + "pattern": "/.*/", + "thresholds": [], + "type": "number", + "unit": "short" + } + ], + "targets": [ + { + "alias": "$tag_referer", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "referer" + ], + "type": "tag" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "views", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "duration" + ], + "type": "field" + }, + { + "params": [], + "type": "count" + } + ] + ], + "tags": [ + { + "key": "hostname", + "operator": "=~", + "value": "/^$Hostname$/" + }, + { + "condition": "AND", + "key": "pathname", + "operator": "=~", + "value": "/\\/$/" + }, + { + "condition": "AND", + "key": "referer", + "operator": "!=", + "value": "vanadium23.me" + }, + { + "condition": "AND", + "key": "referer", + "operator": "!=", + "value": "empty" + } + ] + } + ], + "timeFrom": "7d", + "timeShift": null, + "title": "Referer tops", + "transform": "timeseries_aggregations", + "type": "table" + }, + { + "columns": [ + { + "text": "Total", + "value": "total" + } + ], + "datasource": "${DS_REQUESTS}", + "fontSize": "100%", + "gridPos": { + "h": 12, + "w": 4, + "x": 8, + "y": 11 + }, + "id": 8, + "options": {}, + "pageSize": null, + "showHeader": true, + "sort": { + "col": 1, + "desc": true + }, + "styles": [ + { + "alias": "Time", + "align": "auto", + "dateFormat": "YYYY-MM-DD HH:mm:ss", + "link": false, + "pattern": "Time", + "type": "date" + }, + { + "alias": "", + "align": "right", + "colorMode": null, + "colors": [ + "rgba(245, 54, 54, 0.9)", + "rgba(237, 129, 40, 0.89)", + "rgba(50, 172, 45, 0.97)" + ], + "decimals": 0, + "pattern": "/.*/", + "thresholds": [], + "type": "number", + "unit": "short" + } + ], + "targets": [ + { + "alias": "$tag_utm_source", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "utm_source" + ], + "type": "tag" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "views", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "duration" + ], + "type": "field" + }, + { + "params": [], + "type": "count" + } + ] + ], + "tags": [ + { + "key": "hostname", + "operator": "=~", + "value": "/^$Hostname$/" + }, + { + "condition": "AND", + "key": "pathname", + "operator": "=~", + "value": "/\\/$/" + }, + { + "condition": "AND", + "key": "utm_source", + "operator": "!=", + "value": "empty" + } + ] + } + ], + "timeFrom": "7d", + "timeShift": null, + "title": "utm_sources tops", + "transform": "timeseries_aggregations", + "type": "table" + }, + { + "aliasColors": {}, + "breakPoint": "50%", + "cacheTimeout": null, + "combine": { + "label": "Others", + "threshold": 0 + }, + "datasource": "${DS_REQUESTS}", + "fontSize": "80%", + "format": "short", + "gridPos": { + "h": 12, + "w": 4, + "x": 12, + "y": 11 + }, + "id": 12, + "interval": null, + "legend": { + "show": true, + "values": true + }, + "legendType": "On graph", + "links": [], + "maxDataPoints": 3, + "nullPointMode": "connected", + "options": {}, + "pieType": "pie", + "strokeWidth": "2", + "targets": [ + { + "alias": "$tag_cf_cache", + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "cf_cache" + ], + "type": "tag" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "measurement": "views", + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "duration" + ], + "type": "field" + }, + { + "params": [], + "type": "count" + } + ] + ], + "tags": [ + { + "key": "hostname", + "operator": "=~", + "value": "/^$Hostname$/" + }, + { + "condition": "AND", + "key": "country", + "operator": "=~", + "value": "/^$Country$/" + } + ] + } + ], + "timeFrom": null, + "timeShift": null, + "title": "Cloudflare caches", + "type": "grafana-piechart-panel", + "valueName": "current" + }, + { + "bgColor": null, + "cacheTimeout": null, + "clockType": "24 hour", + "countdownSettings": { + "customFormat": null, + "endCountdownTime": "2020-02-10T11:31:00.000Z", + "endText": "00:00:00" + }, + "datasource": "${DS_REQUESTS}", + "dateSettings": { + "dateFormat": "YYYY-MM-DD", + "fontSize": "20px", + "fontWeight": "normal", + "showDate": true + }, + "gridPos": { + "h": 12, + "w": 8, + "x": 16, + "y": 11 + }, + "id": 14, + "links": [], + "mode": "time", + "options": {}, + "refreshSettings": { + "syncWithDashboard": false + }, + "targets": [ + { + "groupBy": [ + { + "params": [ + "$__interval" + ], + "type": "time" + }, + { + "params": [ + "null" + ], + "type": "fill" + } + ], + "orderByTime": "ASC", + "policy": "default", + "refId": "A", + "resultFormat": "time_series", + "select": [ + [ + { + "params": [ + "value" + ], + "type": "field" + }, + { + "params": [], + "type": "mean" + } + ] + ], + "tags": [] + } + ], + "timeFrom": null, + "timeSettings": { + "customFormat": "HH:mm:ss", + "fontSize": "60px", + "fontWeight": "normal" + }, + "timeShift": null, + "timezone": null, + "timezoneSettings": { + "fontSize": "12px", + "fontWeight": "normal", + "showTimezone": false, + "zoneFormat": "offsetAbbv" + }, + "title": "Time passes", + "type": "grafana-clock-panel" + } + ], + "refresh": "5m", + "schemaVersion": 22, + "style": "dark", + "tags": [], + "templating": { + "list": [ + { + "allValue": null, + "current": {}, + "datasource": "${DS_REQUESTS}", + "definition": "SHOW TAG VALUES ON \"requests\" WITH KEY = \"hostname\"", + "hide": 0, + "includeAll": false, + "label": null, + "multi": false, + "name": "Hostname", + "options": [], + "query": "SHOW TAG VALUES ON \"requests\" WITH KEY = \"hostname\"", + "refresh": 1, + "regex": "", + "skipUrlSync": false, + "sort": 1, + "tagValuesQuery": "", + "tags": [], + "tagsQuery": "", + "type": "query", + "useTags": false + }, + { + "allValue": null, + "current": {}, + "datasource": "${DS_REQUESTS}", + "definition": "SHOW TAG VALUES ON \"requests\" WITH KEY = \"country\"", + "hide": 0, + "includeAll": true, + "label": null, + "multi": false, + "name": "Country", + "options": [], + "query": "SHOW TAG VALUES ON \"requests\" WITH KEY = \"country\"", + "refresh": 1, + "regex": "", + "skipUrlSync": false, + "sort": 1, + "tagValuesQuery": "", + "tags": [], + "tagsQuery": "", + "type": "query", + "useTags": false + }, + { + "auto": false, + "auto_count": 30, + "auto_min": "10s", + "current": { + "selected": true, + "text": "6h", + "value": "6h" + }, + "hide": 0, + "label": null, + "name": "Interval", + "options": [ + { + "selected": false, + "text": "1h", + "value": "1h" + }, + { + "selected": true, + "text": "6h", + "value": "6h" + }, + { + "selected": false, + "text": "12h", + "value": "12h" + }, + { + "selected": false, + "text": "1d", + "value": "1d" + }, + { + "selected": false, + "text": "7d", + "value": "7d" + }, + { + "selected": false, + "text": "14d", + "value": "14d" + }, + { + "selected": false, + "text": "30d", + "value": "30d" + } + ], + "query": "1h,6h,12h,1d,7d,14d,30d", + "refresh": 2, + "skipUrlSync": false, + "type": "interval" + } + ] + }, + "time": { + "from": "now-2d", + "to": "now" + }, + "timepicker": { + "refresh_intervals": [ + "5s", + "10s", + "30s", + "1m", + "5m", + "15m", + "30m", + "1h", + "2h", + "1d" + ] + }, + "timezone": "", + "title": "Blog", + "uid": "CEu7c7UWk", + "version": 16 +} \ No newline at end of file diff --git a/static/dashboard.png b/static/dashboard.png new file mode 100644 index 0000000..3845302 Binary files /dev/null and b/static/dashboard.png differ