Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

Use better labels when exporting to Grafana cloud & Keep constant table names (#38)

patcher99dd0b97b84f51
diff --git a/.gitignore b/.gitignore
index 610c8034..46e4f065 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
node_modules
test.py
-test.go
\ No newline at end of file
+test.go
+.DS_Store
\ No newline at end of file
diff --git a/docs/0.0.1/integrations/grafanacloud.mdx b/docs/0.0.1/integrations/grafanacloud.mdx
index 4d76f39c..044bba27 100644
--- a/docs/0.0.1/integrations/grafanacloud.mdx
+++ b/docs/0.0.1/integrations/grafanacloud.mdx
@@ -3,6 +3,11 @@ title: 'Grafana Cloud'
description: 'Connect Doku to Grafana Cloud'
---
+<Frame>
+ <img src="https://github.com/dokulabs/doku/blob/main/docs/images/grafana-dashboard-1.jpg?raw=true" />
+ <img src="https://github.com/dokulabs/doku/blob/main/docs/images/grafana-dashboard-2.jpg?raw=true" />
+</Frame>
+
Doku can automatically send LLM usage data from Doku to Grafana Cloud using the [Grafana Cloud Influx Telegraf](https://grafana.com/docs/grafana-cloud/send-data/metrics/metrics-influxdb/push-from-telegraf/) and [Loki Push HTTP API](https://grafana.com/docs/loki/latest/reference/api/#push-log-entries-to-loki)
Add the following to your doku configuration file(`doku.yml`).
@@ -16,4 +21,2331 @@ observabilityPlatform:
lokiUrl: <your-loki-push-url> # URL to the Loki Push URL of the Grafana Cloud Loki Instance
lokiUsername: <your-loki-username> # Loki Username of the Grafana Cloud Loki Instance
accessToken: <your-grafana-cloud-access-token> # Access Token of the Grafana Cloud Instance
-```
\ No newline at end of file
+```
+
+## Grafana Resources
+
+<Accordion title="Dashboard">
+ ```json
+ {
+ "annotations": {
+ "list": [
+ {
+ "builtIn": 1,
+ "datasource": {
+ "type": "grafana",
+ "uid": "-- Grafana --"
+ },
+ "enable": true,
+ "hide": true,
+ "iconColor": "rgba(0, 211, 255, 1)",
+ "name": "Annotations & Alerts",
+ "type": "dashboard"
+ }
+ ]
+ },
+ "editable": true,
+ "fiscalYearStartMonth": 0,
+ "graphTooltip": 0,
+ "id": 14,
+ "links": [
+ {
+ "icon": "info",
+ "tags": [],
+ "targetBlank": true,
+ "title": "Doku Documentation",
+ "tooltip": "",
+ "type": "link",
+ "url": "https://docs.dokulabs.com/"
+ },
+ {
+ "icon": "info",
+ "tags": [],
+ "targetBlank": true,
+ "title": "Doku Community",
+ "tooltip": "",
+ "type": "link",
+ "url": "https://dokulabs.slack.com/"
+ },
+ {
+ "icon": "info",
+ "tags": [],
+ "targetBlank": true,
+ "title": "Dokulabs GitHub",
+ "tooltip": "",
+ "type": "link",
+ "url": "https://github.com/dokulabs/"
+ }
+ ],
+ "liveNow": false,
+ "panels": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "gridPos": {
+ "h": 4,
+ "w": 11,
+ "x": 0,
+ "y": 0
+ },
+ "id": 23,
+ "options": {
+ "code": {
+ "language": "plaintext",
+ "showLineNumbers": false,
+ "showMiniMap": false
+ },
+ "content": "---\n---\n---\n---\n---\n---",
+ "mode": "markdown"
+ },
+ "pluginVersion": "10.4.0-65875",
+ "transparent": true,
+ "type": "text"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "gridPos": {
+ "h": 4,
+ "w": 2,
+ "x": 11,
+ "y": 0
+ },
+ "id": 2,
+ "options": {
+ "code": {
+ "language": "plaintext",
+ "showLineNumbers": false,
+ "showMiniMap": false
+ },
+ "content": "![Doku Logo](https://github.com/dokulabs/.github/blob/main/profile/assets/favicon.png?raw=true)",
+ "mode": "markdown"
+ },
+ "pluginVersion": "10.4.0-65875",
+ "transparent": true,
+ "type": "text"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "gridPos": {
+ "h": 4,
+ "w": 11,
+ "x": 13,
+ "y": 0
+ },
+ "id": 22,
+ "options": {
+ "code": {
+ "language": "plaintext",
+ "showLineNumbers": false,
+ "showMiniMap": false
+ },
+ "content": "---\n---\n---\n---\n---\n---",
+ "mode": "markdown"
+ },
+ "pluginVersion": "10.4.0-65875",
+ "transparent": true,
+ "type": "text"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the total number of requests sent to Large Language Models (LLM) Platforms in the selected time interval.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "thresholds"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": null
+ },
+ {
+ "color": "red",
+ "value": 80
+ }
+ ]
+ },
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 3,
+ "w": 6,
+ "x": 0,
+ "y": 4
+ },
+ "id": 3,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "area",
+ "justifyMode": "auto",
+ "orientation": "auto",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "textMode": "auto",
+ "wideLayout": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "count_over_time(doku_llm_requestDuration{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}[$__interval])",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT count(*) FROM DOKU\nWHERE $__timeFilter(time)",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "name": "COUNT",
+ "parameters": [
+ {
+ "name": "environment",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Total Requests",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "sum"
+ ],
+ "operation": "aggregate"
+ },
+ "job": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the average time taken to process each request by the Large Language Models (LLM), offering a snapshot of system performance and efficiency.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "thresholds"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": null
+ },
+ {
+ "color": "red",
+ "value": 5
+ }
+ ]
+ },
+ "unit": "s",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 3,
+ "w": 4,
+ "x": 6,
+ "y": 4
+ },
+ "id": 4,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "area",
+ "justifyMode": "auto",
+ "orientation": "auto",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "textMode": "auto",
+ "wideLayout": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_requestDuration{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT AVG(requestduration) FROM doku\nWHERE $__timeFilter(time)",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "name": "AVG",
+ "parameters": [
+ {
+ "name": "requestduration",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Avg Request Duration",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "mean"
+ ],
+ "operation": "aggregate"
+ },
+ "job": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the average count of Total tokens used per LLM request",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "thresholds"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": null
+ },
+ {
+ "color": "red",
+ "value": 1000
+ }
+ ]
+ },
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 3,
+ "w": 4,
+ "x": 10,
+ "y": 4
+ },
+ "id": 5,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "area",
+ "justifyMode": "auto",
+ "orientation": "auto",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "textMode": "auto",
+ "wideLayout": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_totalTokens{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT AVG(totaltokens) FROM doku\nWHERE $__timeFilter(time)",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "name": "AVG",
+ "parameters": [
+ {
+ "name": "totaltokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Avg Tokens per Request",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "mean"
+ ],
+ "operation": "aggregate"
+ },
+ "job": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the average cost incurred per request to the LLM, helping monitor and optimize budget allocation for LLM usage.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "thresholds"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": null
+ },
+ {
+ "color": "red",
+ "value": 5
+ }
+ ]
+ },
+ "unit": "currencyUSD",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 3,
+ "w": 4,
+ "x": 14,
+ "y": 4
+ },
+ "id": 6,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "area",
+ "justifyMode": "auto",
+ "orientation": "auto",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "textMode": "auto",
+ "wideLayout": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_usageCost{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT AVG(usagecost) FROM doku \nWHERE $__timeFilter(time)",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "name": "AVG",
+ "parameters": [
+ {
+ "name": "usagecost",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Avg Cost / Request",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "mean"
+ ],
+ "operation": "aggregate"
+ },
+ "job": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the total cost on LLM requests over the selected period.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "continuous-BlPu"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": null
+ }
+ ]
+ },
+ "unit": "currencyUSD",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 3,
+ "w": 6,
+ "x": 18,
+ "y": 4
+ },
+ "id": 10,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "area",
+ "justifyMode": "auto",
+ "orientation": "auto",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "textMode": "auto",
+ "wideLayout": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_usageCost{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT SUM(usagecost) FROM doku\nWHERE $__timeFilter(time)",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "name": "SUM",
+ "parameters": [
+ {
+ "name": "usagecost",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Total Cost",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "sum"
+ ],
+ "operation": "aggregate"
+ },
+ "job": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the count of API requests made to LLMs over the selected time.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "continuous-BlPu"
+ },
+ "custom": {
+ "axisBorderShow": false,
+ "axisCenteredZero": false,
+ "axisColorMode": "series",
+ "axisGridShow": true,
+ "axisLabel": "",
+ "axisPlacement": "auto",
+ "barAlignment": 0,
+ "drawStyle": "line",
+ "fillOpacity": 54,
+ "gradientMode": "hue",
+ "hideFrom": {
+ "legend": false,
+ "tooltip": false,
+ "viz": false
+ },
+ "insertNulls": false,
+ "lineInterpolation": "smooth",
+ "lineStyle": {
+ "dash": [
+ 10,
+ 10
+ ],
+ "fill": "dash"
+ },
+ "lineWidth": 2,
+ "pointSize": 5,
+ "scaleDistribution": {
+ "type": "linear"
+ },
+ "showPoints": "always",
+ "spanNulls": true,
+ "stacking": {
+ "group": "A",
+ "mode": "none"
+ },
+ "thresholdsStyle": {
+ "mode": "off"
+ }
+ },
+ "fieldMinMax": false,
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green",
+ "value": null
+ }
+ ]
+ },
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 5,
+ "w": 24,
+ "x": 0,
+ "y": 7
+ },
+ "id": 17,
+ "options": {
+ "legend": {
+ "calcs": [],
+ "displayMode": "list",
+ "placement": "bottom",
+ "showLegend": false
+ },
+ "tooltip": {
+ "mode": "single",
+ "sort": "none"
+ }
+ },
+ "pluginVersion": "10.2.3",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "count(count_over_time(doku_llm_requestDuration{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}[$__interval]))",
+ "format": "time_series",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "Requests",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT\n DATE_TRUNC('day', \"time\") AS \"time\",\n COUNT(endpoint) AS total_requests\nFROM \n doku\nWHERE\n $__timeFilter(\"time\")\nGROUP BY \n DATE_TRUNC('day', \"time\")\nORDER BY \n \"time\";",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "alias": "\"Prompt Tokens\"",
+ "parameters": [
+ {
+ "name": "prompttokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"time\"",
+ "parameters": [
+ {
+ "name": "\"time\"",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"Completion Tokens\"",
+ "parameters": [
+ {
+ "name": "completiontokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"Total Tokens\"",
+ "parameters": [
+ {
+ "name": "totaltokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Total Requests overtime",
+ "transformations": [],
+ "type": "timeseries"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel illustrates the distribution of LLM requests across different usage categories: Chat Generations, Embeddings, Image Generations, Audio Generations, and Fine Tuning. It visualizes the relative volume of requests, helping to identify which types of operations are most frequently performed and providing insights into usage patterns by category.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "fixedColor": "blue",
+ "mode": "palette-classic"
+ },
+ "custom": {
+ "hideFrom": {
+ "legend": false,
+ "tooltip": false,
+ "viz": false
+ }
+ },
+ "mappings": [],
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": [
+ {
+ "__systemRef": "hideSeriesFrom",
+ "matcher": {
+ "id": "byNames",
+ "options": {
+ "mode": "exclude",
+ "names": [
+ "Value (sum)",
+ "Chat"
+ ],
+ "prefix": "All except:",
+ "readOnly": true
+ }
+ },
+ "properties": [
+ {
+ "id": "custom.hideFrom",
+ "value": {
+ "legend": false,
+ "tooltip": false,
+ "viz": true
+ }
+ }
+ ]
+ }
+ ]
+ },
+ "gridPos": {
+ "h": 9,
+ "w": 6,
+ "x": 0,
+ "y": 12
+ },
+ "id": 12,
+ "options": {
+ "displayLabels": [
+ "name",
+ "value"
+ ],
+ "legend": {
+ "displayMode": "list",
+ "placement": "bottom",
+ "showLegend": true,
+ "values": []
+ },
+ "pieType": "pie",
+ "reduceOptions": {
+ "calcs": [
+ "allValues"
+ ],
+ "fields": "",
+ "values": true
+ },
+ "tooltip": {
+ "mode": "single",
+ "sort": "asc"
+ }
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "count_over_time(doku_llm_requestDuration{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}[$__interval])",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT \n category,\n CAST(COUNT(*) AS INTEGER) AS Requests,\n ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER(), 2) AS percentage\n FROM \n (\n SELECT \n CASE\n WHEN endpoint LIKE 'openai.chat%' THEN 'Chat'\n WHEN endpoint LIKE 'openai.completions%' THEN 'Chat'\n WHEN endpoint LIKE 'anthropic.completions%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.summarize%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.generate%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.chat%' THEN 'Chat'\n WHEN endpoint LIKE '%embed%' THEN 'Embeddings'\n WHEN endpoint LIKE 'openai.images%' THEN 'Image'\n WHEN endpoint LIKE 'openai.audio%' THEN 'Audio'\n WHEN endpoint LIKE 'openai.fine_tuning%' THEN 'Fine Tuning'\n END AS category,\n endpoint\n FROM doku\n WHERE $__timeFilter(time)\n ) AS subquery\n GROUP BY category",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "parameters": [],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "useBackend": false
+ }
+ ],
+ "title": "Usage by Category",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "sum"
+ ],
+ "operation": "aggregate"
+ },
+ "generation": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "piechart"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "Displays the cost breakdown by provider (OpenAI, Cohere, Anthropic) for LLM requests, offering insights into financial deployment across different services.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "palette-classic"
+ },
+ "custom": {
+ "hideFrom": {
+ "legend": false,
+ "tooltip": false,
+ "viz": false
+ }
+ },
+ "mappings": [],
+ "unit": "currencyUSD",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 9,
+ "w": 6,
+ "x": 6,
+ "y": 12
+ },
+ "id": 16,
+ "options": {
+ "displayLabels": [
+ "name",
+ "value"
+ ],
+ "legend": {
+ "displayMode": "list",
+ "placement": "bottom",
+ "showLegend": true,
+ "values": []
+ },
+ "pieType": "pie",
+ "reduceOptions": {
+ "calcs": [
+ "allValues"
+ ],
+ "fields": "",
+ "values": true
+ },
+ "tooltip": {
+ "mode": "single",
+ "sort": "asc"
+ }
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_usageCost{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT\n CASE\n WHEN endpoint LIKE 'openai.%' THEN 'OpenAI'\n WHEN endpoint LIKE 'cohere.%' THEN 'Cohere'\n WHEN endpoint LIKE 'anthropic.%' THEN 'Anthropic'\n END AS provider,\n SUM(usageCost) AS total_cost\nFROM\n doku\nWHERE $__timeFilter(time)\nGROUP BY\n provider\nORDER BY\n total_cost DESC;",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "parameters": [],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "useBackend": false
+ }
+ ],
+ "title": "Usage cost by Provider",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "sum"
+ ],
+ "operation": "aggregate"
+ },
+ "generation": {
+ "aggregations": []
+ },
+ "platform": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "piechart"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel presents a pie chart detailing the distribution of expenses for LLM requests by various applications. It shows how budget allocations are consumed by different applications, enabling a deeper understanding of cost drivers and facilitating cost optimization efforts.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "palette-classic"
+ },
+ "custom": {
+ "hideFrom": {
+ "legend": false,
+ "tooltip": false,
+ "viz": false
+ }
+ },
+ "mappings": [],
+ "unit": "currencyUSD",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 9,
+ "w": 6,
+ "x": 12,
+ "y": 12
+ },
+ "id": 24,
+ "options": {
+ "displayLabels": [
+ "name",
+ "value"
+ ],
+ "legend": {
+ "displayMode": "list",
+ "placement": "bottom",
+ "showLegend": true,
+ "values": []
+ },
+ "pieType": "pie",
+ "reduceOptions": {
+ "calcs": [
+ "allValues"
+ ],
+ "fields": "/.*/",
+ "values": true
+ },
+ "tooltip": {
+ "mode": "single",
+ "sort": "asc"
+ }
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_usageCost{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT\n CASE\n WHEN endpoint LIKE 'openai.%' THEN 'OpenAI'\n WHEN endpoint LIKE 'cohere.%' THEN 'Cohere'\n WHEN endpoint LIKE 'anthropic.%' THEN 'Anthropic'\n END AS provider,\n SUM(usageCost) AS total_cost\nFROM\n doku\nWHERE $__timeFilter(time)\nGROUP BY\n provider\nORDER BY\n total_cost DESC;",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "parameters": [],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "useBackend": false
+ }
+ ],
+ "title": "Usage cost by Application",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "sum"
+ ],
+ "operation": "aggregate"
+ },
+ "applicationName": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "piechart"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "Visualizes the allocation of LLM request expenses across different environments, such as development, staging, and production. This breakdown helps in tracking and managing costs with respect to operational environments.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "palette-classic"
+ },
+ "custom": {
+ "hideFrom": {
+ "legend": false,
+ "tooltip": false,
+ "viz": false
+ }
+ },
+ "mappings": [],
+ "unit": "currencyUSD",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 9,
+ "w": 6,
+ "x": 18,
+ "y": 12
+ },
+ "id": 25,
+ "options": {
+ "displayLabels": [
+ "name",
+ "value"
+ ],
+ "legend": {
+ "displayMode": "list",
+ "placement": "bottom",
+ "showLegend": true,
+ "values": []
+ },
+ "pieType": "pie",
+ "reduceOptions": {
+ "calcs": [
+ "allValues"
+ ],
+ "fields": "/.*/",
+ "values": true
+ },
+ "tooltip": {
+ "mode": "single",
+ "sort": "asc"
+ }
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_usageCost{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT\n CASE\n WHEN endpoint LIKE 'openai.%' THEN 'OpenAI'\n WHEN endpoint LIKE 'cohere.%' THEN 'Cohere'\n WHEN endpoint LIKE 'anthropic.%' THEN 'Anthropic'\n END AS provider,\n SUM(usageCost) AS total_cost\nFROM\n doku\nWHERE $__timeFilter(time)\nGROUP BY\n provider\nORDER BY\n total_cost DESC;",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "parameters": [],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "useBackend": false
+ }
+ ],
+ "title": "Usage cost by Environment",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "sum"
+ ],
+ "operation": "aggregate"
+ },
+ "applicationName": {
+ "aggregations": []
+ },
+ "environment": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "piechart"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the average count of Prompt tokens used per LLM request",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "continuous-YlBl"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green"
+ },
+ {
+ "color": "red",
+ "value": 5
+ }
+ ]
+ },
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 3,
+ "w": 6,
+ "x": 0,
+ "y": 21
+ },
+ "id": 9,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "area",
+ "justifyMode": "auto",
+ "orientation": "auto",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "textMode": "auto",
+ "wideLayout": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_promptTokens{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT AVG(prompttokens) FROM doku WHERE \n $__timeFilter(\"time\")",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "name": "AVG",
+ "parameters": [
+ {
+ "name": "prompttokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Avg Prompt Tokens / Request",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "mean"
+ ],
+ "operation": "aggregate"
+ },
+ "job": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "Provides a time-series analysis of token consumption for LLM requests, highlighting trends in usage intensity and complexity over time.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "continuous-BlYlRd"
+ },
+ "custom": {
+ "axisBorderShow": false,
+ "axisCenteredZero": false,
+ "axisColorMode": "series",
+ "axisGridShow": true,
+ "axisLabel": "",
+ "axisPlacement": "auto",
+ "barAlignment": 0,
+ "drawStyle": "line",
+ "fillOpacity": 50,
+ "gradientMode": "hue",
+ "hideFrom": {
+ "legend": false,
+ "tooltip": false,
+ "viz": false
+ },
+ "insertNulls": 86400000,
+ "lineInterpolation": "smooth",
+ "lineStyle": {
+ "dash": [
+ 10,
+ 10
+ ],
+ "fill": "dash"
+ },
+ "lineWidth": 1,
+ "pointSize": 6,
+ "scaleDistribution": {
+ "type": "linear"
+ },
+ "showPoints": "always",
+ "spanNulls": true,
+ "stacking": {
+ "group": "A",
+ "mode": "none"
+ },
+ "thresholdsStyle": {
+ "mode": "off"
+ }
+ },
+ "fieldMinMax": false,
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green"
+ },
+ {
+ "color": "red",
+ "value": 80
+ }
+ ]
+ },
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 6,
+ "w": 18,
+ "x": 6,
+ "y": 21
+ },
+ "id": 11,
+ "options": {
+ "legend": {
+ "calcs": [],
+ "displayMode": "list",
+ "placement": "bottom",
+ "showLegend": true
+ },
+ "tooltip": {
+ "mode": "single",
+ "sort": "none"
+ }
+ },
+ "pluginVersion": "10.2.3",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "avg(doku_llm_completionTokens{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"})",
+ "format": "time_series",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "Completion Tokens",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT prompttokens AS \"Prompt Tokens\", \"time\" AS \"time\", completiontokens AS \"Completion Tokens\", totaltokens AS \"Total Tokens\" FROM doku",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "alias": "\"Prompt Tokens\"",
+ "parameters": [
+ {
+ "name": "prompttokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"time\"",
+ "parameters": [
+ {
+ "name": "\"time\"",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"Completion Tokens\"",
+ "parameters": [
+ {
+ "name": "completiontokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"Total Tokens\"",
+ "parameters": [
+ {
+ "name": "totaltokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "builder",
+ "expr": "avg(doku_llm_promptTokens)",
+ "format": "time_series",
+ "fullMetaSearch": false,
+ "hide": false,
+ "includeNullMetadata": true,
+ "legendFormat": "Prompt Tokens",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT prompttokens AS \"Prompt Tokens\", \"time\" AS \"time\", completiontokens AS \"Completion Tokens\", totaltokens AS \"Total Tokens\" FROM doku",
+ "refId": "B",
+ "sql": {
+ "columns": [
+ {
+ "alias": "\"Prompt Tokens\"",
+ "parameters": [
+ {
+ "name": "prompttokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"time\"",
+ "parameters": [
+ {
+ "name": "\"time\"",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"Completion Tokens\"",
+ "parameters": [
+ {
+ "name": "completiontokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ },
+ {
+ "alias": "\"Total Tokens\"",
+ "parameters": [
+ {
+ "name": "totaltokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Token usage overtime",
+ "type": "timeseries"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "This panel shows the average count of Completion tokens used per LLM request",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "continuous-BlPu"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green"
+ }
+ ]
+ },
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 3,
+ "w": 6,
+ "x": 0,
+ "y": 24
+ },
+ "id": 7,
+ "options": {
+ "colorMode": "background",
+ "graphMode": "area",
+ "justifyMode": "auto",
+ "orientation": "auto",
+ "reduceOptions": {
+ "calcs": [
+ "lastNotNull"
+ ],
+ "fields": "",
+ "values": false
+ },
+ "showPercentChange": false,
+ "textMode": "auto",
+ "wideLayout": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "doku_llm_completionTokens{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT AVG(completiontokens) FROM doku WHERE \n $__timeFilter(\"time\")",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "name": "AVG",
+ "parameters": [
+ {
+ "name": "completiontokens",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Avg Completion Tokens / Request",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "mean"
+ ],
+ "operation": "aggregate"
+ },
+ "job": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ }
+ ],
+ "type": "stat"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "Displays the frequency of model usage over time, identifying the most utilised models and showcasing usage trends across different periods",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "fixedColor": "purple",
+ "mode": "continuous-YlBl"
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green"
+ },
+ {
+ "color": "red",
+ "value": 80
+ }
+ ]
+ },
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 6,
+ "w": 12,
+ "x": 0,
+ "y": 27
+ },
+ "id": 13,
+ "options": {
+ "displayMode": "lcd",
+ "maxVizHeight": 165,
+ "minVizHeight": 0,
+ "minVizWidth": 75,
+ "namePlacement": "left",
+ "orientation": "horizontal",
+ "reduceOptions": {
+ "calcs": [
+ "allValues"
+ ],
+ "fields": "",
+ "values": true
+ },
+ "showUnfilled": true,
+ "sizing": "auto",
+ "valueMode": "color"
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "topk(5,count by(model) (sum_over_time(doku_llm_requestDuration{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}[$__interval])))",
+ "format": "table",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "__auto",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT \n model,\n total_count,\n percentage_usage\nFROM \n (\n SELECT \n model,\n COUNT(*) AS total_count,\n ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER(), 2) AS percentage_usage\n FROM \n doku\n WHERE $__timeFilter(time)\n GROUP BY \n model\n ORDER BY \n percentage_usage DESC\n ) sub\nLIMIT 5;",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "parameters": [
+ {
+ "name": "model",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Top models by usage",
+ "transformations": [
+ {
+ "id": "groupBy",
+ "options": {
+ "fields": {
+ "Value": {
+ "aggregations": [
+ "sum"
+ ],
+ "operation": "aggregate"
+ },
+ "model": {
+ "aggregations": [],
+ "operation": "groupby"
+ }
+ }
+ }
+ },
+ {
+ "id": "sortBy",
+ "options": {
+ "fields": {},
+ "sort": [
+ {
+ "desc": true,
+ "field": "Value (sum)"
+ }
+ ]
+ }
+ }
+ ],
+ "type": "bargauge"
+ },
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "description": "Breaks down token usage across different LLM models, offering insights into which models are driving the highest token consumption.",
+ "fieldConfig": {
+ "defaults": {
+ "color": {
+ "mode": "palette-classic"
+ },
+ "custom": {
+ "axisBorderShow": false,
+ "axisCenteredZero": false,
+ "axisColorMode": "text",
+ "axisLabel": "",
+ "axisPlacement": "auto",
+ "barAlignment": 0,
+ "drawStyle": "line",
+ "fillOpacity": 23,
+ "gradientMode": "hue",
+ "hideFrom": {
+ "legend": false,
+ "tooltip": false,
+ "viz": false
+ },
+ "insertNulls": false,
+ "lineInterpolation": "linear",
+ "lineStyle": {
+ "dash": [
+ 10,
+ 10
+ ],
+ "fill": "dash"
+ },
+ "lineWidth": 1,
+ "pointSize": 9,
+ "scaleDistribution": {
+ "type": "linear"
+ },
+ "showPoints": "always",
+ "spanNulls": true,
+ "stacking": {
+ "group": "A",
+ "mode": "none"
+ },
+ "thresholdsStyle": {
+ "mode": "off"
+ }
+ },
+ "mappings": [],
+ "thresholds": {
+ "mode": "absolute",
+ "steps": [
+ {
+ "color": "green"
+ },
+ {
+ "color": "red",
+ "value": 80
+ }
+ ]
+ },
+ "unit": "none",
+ "unitScale": true
+ },
+ "overrides": []
+ },
+ "gridPos": {
+ "h": 6,
+ "w": 12,
+ "x": 12,
+ "y": 27
+ },
+ "id": 14,
+ "options": {
+ "legend": {
+ "calcs": [],
+ "displayMode": "list",
+ "placement": "bottom",
+ "showLegend": true
+ },
+ "tooltip": {
+ "mode": "single",
+ "sort": "none"
+ }
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "disableTextWrap": false,
+ "editorMode": "code",
+ "expr": "sum_over_time(doku_llm_totalTokens{job=\"doku\", applicationName=~\"$Application\", environment=~\"$Environment\"}[$__interval])",
+ "format": "time_series",
+ "fullMetaSearch": false,
+ "includeNullMetadata": true,
+ "legendFormat": "{{model}}",
+ "range": true,
+ "rawQuery": true,
+ "rawSql": "SELECT \n DATE_TRUNC('day', \"time\") AS \"time\",\n model,\n COUNT(*) AS \"Model:\"\nFROM \n doku\nWHERE \n $__timeFilter(\"time\")\nGROUP BY \n 1, model\nORDER BY \n \"time\" ASC, model;",
+ "refId": "A",
+ "sql": {
+ "columns": [
+ {
+ "parameters": [
+ {
+ "name": "model",
+ "type": "functionParameter"
+ }
+ ],
+ "type": "function"
+ }
+ ],
+ "groupBy": [
+ {
+ "property": {
+ "type": "string"
+ },
+ "type": "groupBy"
+ }
+ ],
+ "limit": 50
+ },
+ "table": "doku",
+ "useBackend": false
+ }
+ ],
+ "title": "Token usage by model",
+ "transformations": [],
+ "type": "timeseries"
+ },
+ {
+ "datasource": {
+ "type": "loki",
+ "uid": "grafanacloud-logs"
+ },
+ "description": "Displays a log of all prompt messages sent to the LLM, providing insights into the inputs and interactions leading up to LLM responses.",
+ "gridPos": {
+ "h": 8,
+ "w": 12,
+ "x": 0,
+ "y": 33
+ },
+ "id": 26,
+ "options": {
+ "dedupStrategy": "none",
+ "enableLogDetails": true,
+ "prettifyLogMessage": true,
+ "showCommonLabels": false,
+ "showLabels": false,
+ "showTime": true,
+ "sortOrder": "Descending",
+ "wrapLogMessage": false
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "loki",
+ "uid": "grafanacloud-logs"
+ },
+ "editorMode": "code",
+ "expr": "{job=\"doku\", type=\"prompt\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "hide": false,
+ "queryType": "range",
+ "refId": "B"
+ }
+ ],
+ "title": "Prompts",
+ "transformations": [
+ {
+ "id": "extractFields",
+ "options": {
+ "format": "auto",
+ "source": "labels"
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "logs"
+ },
+ {
+ "datasource": {
+ "type": "loki",
+ "uid": "grafanacloud-logs"
+ },
+ "description": "Displays a log of all responses from the LLM, offering a comprehensive view of how the model interprets prompts and the information it generates.",
+ "gridPos": {
+ "h": 8,
+ "w": 12,
+ "x": 12,
+ "y": 33
+ },
+ "id": 27,
+ "options": {
+ "dedupStrategy": "none",
+ "enableLogDetails": true,
+ "prettifyLogMessage": true,
+ "showCommonLabels": false,
+ "showLabels": false,
+ "showTime": true,
+ "sortOrder": "Descending",
+ "wrapLogMessage": true
+ },
+ "pluginVersion": "10.4.0-65875",
+ "targets": [
+ {
+ "datasource": {
+ "type": "loki",
+ "uid": "grafanacloud-logs"
+ },
+ "editorMode": "code",
+ "expr": "{job=\"doku\", type=\"response\", applicationName=~\"$Application\", environment=~\"$Environment\"}",
+ "hide": false,
+ "queryType": "range",
+ "refId": "B"
+ }
+ ],
+ "title": "Responses",
+ "transformations": [
+ {
+ "id": "extractFields",
+ "options": {
+ "format": "auto",
+ "source": "labels"
+ }
+ }
+ ],
+ "transparent": true,
+ "type": "logs"
+ }
+ ],
+ "refresh": "",
+ "schemaVersion": 39,
+ "tags": [
+ "Doku",
+ "OpenAI",
+ "Cohere",
+ "Anthropic"
+ ],
+ "templating": {
+ "list": [
+ {
+ "current": {
+ "selected": true,
+ "text": [
+ "All"
+ ],
+ "value": [
+ "$__all"
+ ]
+ },
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "definition": "label_values(applicationName)",
+ "hide": 0,
+ "includeAll": true,
+ "label": "Application",
+ "multi": true,
+ "name": "Application",
+ "options": [],
+ "query": {
+ "qryType": 1,
+ "query": "label_values(applicationName)",
+ "refId": "PrometheusVariableQueryEditor-VariableQuery"
+ },
+ "refresh": 1,
+ "regex": "",
+ "skipUrlSync": false,
+ "sort": 0,
+ "type": "query"
+ },
+ {
+ "current": {
+ "selected": true,
+ "text": [
+ "All"
+ ],
+ "value": [
+ "$__all"
+ ]
+ },
+ "datasource": {
+ "type": "prometheus",
+ "uid": "grafanacloud-prom"
+ },
+ "definition": "label_values(environment)",
+ "hide": 0,
+ "includeAll": true,
+ "label": "Environment",
+ "multi": true,
+ "name": "Environment",
+ "options": [],
+ "query": {
+ "qryType": 1,
+ "query": "label_values(environment)",
+ "refId": "PrometheusVariableQueryEditor-VariableQuery"
+ },
+ "refresh": 1,
+ "regex": "",
+ "skipUrlSync": false,
+ "sort": 0,
+ "type": "query"
+ }
+ ]
+ },
+ "time": {
+ "from": "now-24h",
+ "to": "now"
+ },
+ "timepicker": {},
+ "timezone": "",
+ "title": "Doku - LLM Monitoring",
+ "uid": "doku-llm-monitoring",
+ "version": 1,
+ "weekStart": ""
+ }
+ ```
+</Accordion>
+
diff --git a/docs/0.0.1/integrations/grafanaoss.mdx b/docs/0.0.1/integrations/grafanaoss.mdx
index 90b01dc4..89470b28 100644
--- a/docs/0.0.1/integrations/grafanaoss.mdx
+++ b/docs/0.0.1/integrations/grafanaoss.mdx
@@ -3,6 +3,11 @@ title: 'Grafana OSS'
description: 'Connect Doku to Grafana OSS'
---
+<Frame>
+ <img src="https://github.com/dokulabs/doku/blob/main/docs/images/grafana-dashboard-1.jpg?raw=true" />
+ <img src="https://github.com/dokulabs/doku/blob/main/docs/images/grafana-dashboard-3.jpg?raw=true" />
+</Frame>
+
You can add the Doku's backend storage database (`TimescaleDB`) as a datasource in Grafana.
## Steps
@@ -24,9 +29,9 @@ You can add the Doku's backend storage database (`TimescaleDB`) as a datasource
Grafana will attempt to connect to the database using the provided settings. If everything is configured correctly, you will see a message indicating the data source is working.
-## Grafana Dashboard
+## Grafana Resources
-<Accordion title="Dashboard JSON">
+<Accordion title="Dashboard">
```json
{
"annotations": {
@@ -48,10 +53,10 @@ Grafana will attempt to connect to the database using the provided settings. If
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
- "id": 3,
+ "id": 8,
"links": [
{
- "icon": "book",
+ "icon": "info",
"tags": [],
"targetBlank": true,
"title": "Doku Documentation",
@@ -76,18 +81,6 @@ Grafana will attempt to connect to the database using the provided settings. If
"tooltip": "",
"type": "link",
"url": "https://github.com/dokulabs/"
- },
- {
- "asDropdown": false,
- "icon": "external link",
- "includeVars": false,
- "keepTime": false,
- "tags": [],
- "targetBlank": false,
- "title": "New link",
- "tooltip": "",
- "type": "dashboards",
- "url": ""
}
],
"liveNow": false,
@@ -95,7 +88,7 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"gridPos": {
"h": 4,
@@ -120,7 +113,7 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"gridPos": {
"h": 4,
@@ -145,7 +138,7 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"gridPos": {
"h": 4,
@@ -170,8 +163,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the total number of requests sent to Large Language Models (LLM) Platforms in the selected time interval.",
"fieldConfig": {
"defaults": {
"color": {
@@ -222,12 +216,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT count(*) FROM DOKU\nWHERE $__timeFilter(time)",
+ "rawSql": "SELECT count(*) FROM DOKU_DATA\nWHERE $__timeFilter(time)",
"refId": "A",
"sql": {
"columns": [
@@ -262,8 +256,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the average time taken to process each request by the Large Language Models (LLM), offering a snapshot of system performance and efficiency.",
"fieldConfig": {
"defaults": {
"color": {
@@ -314,12 +309,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT AVG(requestduration) FROM doku\nWHERE $__timeFilter(time)",
+ "rawSql": "SELECT AVG(requestduration) FROM DOKU_DATA\nWHERE $__timeFilter(time)",
"refId": "A",
"sql": {
"columns": [
@@ -354,8 +349,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the average count of Total tokens used per LLM request",
"fieldConfig": {
"defaults": {
"color": {
@@ -406,12 +402,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT AVG(totaltokens) FROM doku\nWHERE $__timeFilter(time)",
+ "rawSql": "SELECT AVG(totaltokens) FROM DOKU_DATA\nWHERE $__timeFilter(time)",
"refId": "A",
"sql": {
"columns": [
@@ -446,8 +442,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the average cost incurred per request to the LLM, helping monitor and optimize budget allocation for LLM usage.",
"fieldConfig": {
"defaults": {
"color": {
@@ -498,12 +495,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT AVG(usagecost) FROM doku \nWHERE $__timeFilter(time)",
+ "rawSql": "SELECT AVG(usagecost) FROM DOKU_DATA \nWHERE $__timeFilter(time)",
"refId": "A",
"sql": {
"columns": [
@@ -538,8 +535,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the total cost on LLM requests over the selected period.",
"fieldConfig": {
"defaults": {
"color": {
@@ -586,12 +584,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT SUM(usagecost) FROM doku\nWHERE $__timeFilter(time)",
+ "rawSql": "SELECT SUM(usagecost) FROM DOKU_DATA\nWHERE $__timeFilter(time)",
"refId": "A",
"sql": {
"columns": [
@@ -626,8 +624,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the count of API requests made to LLMs over the selected time.",
"fieldConfig": {
"defaults": {
"color": {
@@ -712,12 +711,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "time_series",
"rawQuery": true,
- "rawSql": "SELECT\n DATE_TRUNC('day', \"time\") AS \"time\",\n COUNT(endpoint) AS total_requests\nFROM \n doku\nWHERE\n $__timeFilter(\"time\")\nGROUP BY \n DATE_TRUNC('day', \"time\")\nORDER BY \n \"time\";",
+ "rawSql": "SELECT\n DATE_TRUNC('day', \"time\") AS \"time\",\n COUNT(endpoint) AS total_requests\nFROM \n DOKU_DATA\nWHERE\n $__timeFilter(\"time\")\nGROUP BY \n DATE_TRUNC('day', \"time\")\nORDER BY \n \"time\";",
"refId": "A",
"sql": {
"columns": [
@@ -781,8 +780,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel illustrates the distribution of LLM requests across different usage categories: Chat Generations, Embeddings, Image Generations, Audio Generations, and Fine Tuning. It visualizes the relative volume of requests, helping to identify which types of operations are most frequently performed and providing insights into usage patterns by category.\n\n",
"fieldConfig": {
"defaults": {
"color": {
@@ -833,12 +833,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT \n category,\n CAST(COUNT(*) AS INTEGER) AS Requests,\n ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER(), 2) AS percentage\n FROM \n (\n SELECT \n CASE\n WHEN endpoint LIKE 'openai.chat%' THEN 'Chat'\n WHEN endpoint LIKE 'openai.completions%' THEN 'Chat'\n WHEN endpoint LIKE 'anthropic.completions%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.summarize%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.generate%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.chat%' THEN 'Chat'\n WHEN endpoint LIKE '%embed%' THEN 'Embeddings'\n WHEN endpoint LIKE 'openai.images%' THEN 'Image'\n WHEN endpoint LIKE 'openai.audio%' THEN 'Audio'\n WHEN endpoint LIKE 'openai.fine_tuning%' THEN 'Fine Tuning'\n END AS category,\n endpoint\n FROM doku\n WHERE $__timeFilter(time)\n ) AS subquery\n GROUP BY category",
+ "rawSql": "SELECT \n category,\n CAST(COUNT(*) AS INTEGER) AS Requests,\n ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER(), 2) AS percentage\n FROM \n (\n SELECT \n CASE\n WHEN endpoint LIKE 'openai.chat%' THEN 'Chat'\n WHEN endpoint LIKE 'openai.completions%' THEN 'Chat'\n WHEN endpoint LIKE 'anthropic.completions%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.summarize%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.generate%' THEN 'Chat'\n WHEN endpoint LIKE 'cohere.chat%' THEN 'Chat'\n WHEN endpoint LIKE '%embed%' THEN 'Embeddings'\n WHEN endpoint LIKE 'openai.images%' THEN 'Image'\n WHEN endpoint LIKE 'openai.audio%' THEN 'Audio'\n WHEN endpoint LIKE 'openai.fine_tuning%' THEN 'Fine Tuning'\n END AS category,\n endpoint\n FROM DOKU_DATA\n WHERE $__timeFilter(time)\n ) AS subquery\n GROUP BY category",
"refId": "A",
"sql": {
"columns": [
@@ -866,8 +866,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "Displays the cost breakdown by provider (OpenAI, Cohere, Anthropic) for LLM requests, offering insights into financial deployment across different services.",
"fieldConfig": {
"defaults": {
"color": {
@@ -919,12 +920,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT\n CASE\n WHEN endpoint LIKE 'openai.%' THEN 'OpenAI'\n WHEN endpoint LIKE 'cohere.%' THEN 'Cohere'\n WHEN endpoint LIKE 'anthropic.%' THEN 'Anthropic'\n END AS provider,\n SUM(usageCost) AS total_cost\nFROM\n doku\nWHERE $__timeFilter(time)\nGROUP BY\n provider\nORDER BY\n total_cost DESC;",
+ "rawSql": "SELECT\n CASE\n WHEN endpoint LIKE 'openai.%' THEN 'OpenAI'\n WHEN endpoint LIKE 'cohere.%' THEN 'Cohere'\n WHEN endpoint LIKE 'anthropic.%' THEN 'Anthropic'\n END AS provider,\n SUM(usageCost) AS total_cost\nFROM\n DOKU_DATA\nWHERE $__timeFilter(time)\nGROUP BY\n provider\nORDER BY\n total_cost DESC;",
"refId": "A",
"sql": {
"columns": [
@@ -952,8 +953,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel presents a pie chart detailing the distribution of expenses for LLM requests by various applications. It shows how budget allocations are consumed by different applications, enabling a deeper understanding of cost drivers and facilitating cost optimization efforts.",
"fieldConfig": {
"defaults": {
"color": {
@@ -1006,12 +1008,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": " SELECT\n applicationName,\n sum(usageCost) AS \"usageCost\"\n FROM\n doku\n WHERE $__timeFilter(time)\n GROUP BY\n applicationName\n ORDER BY\n \"usageCost\" DESC",
+ "rawSql": " SELECT\n applicationName,\n sum(usageCost) AS \"usageCost\"\n FROM\n DOKU_DATA\n WHERE $__timeFilter(time)\n GROUP BY\n applicationName\n ORDER BY\n \"usageCost\" DESC",
"refId": "A",
"sql": {
"columns": [
@@ -1039,8 +1041,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "Visualizes the allocation of LLM request expenses across different environments, such as development, staging, and production. This breakdown helps in tracking and managing costs with respect to operational environments.",
"fieldConfig": {
"defaults": {
"color": {
@@ -1093,12 +1096,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": " SELECT\n environment,\n sum(usageCost) AS \"usageCost\"\n FROM\n doku\n WHERE $__timeFilter(time)\n GROUP BY\n environment\n ORDER BY\n \"usageCost\" DESC",
+ "rawSql": " SELECT\n environment,\n sum(usageCost) AS \"usageCost\"\n FROM\n DOKU_DATA\n WHERE $__timeFilter(time)\n GROUP BY\n environment\n ORDER BY\n \"usageCost\" DESC",
"refId": "A",
"sql": {
"columns": [
@@ -1126,8 +1129,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the average count of Prompt tokens used per LLM request",
"fieldConfig": {
"defaults": {
"color": {
@@ -1156,7 +1160,7 @@ Grafana will attempt to connect to the database using the provided settings. If
"x": 0,
"y": 21
},
- "id": 7,
+ "id": 9,
"options": {
"colorMode": "background",
"graphMode": "area",
@@ -1177,12 +1181,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT AVG(completiontokens) FROM doku WHERE \n $__timeFilter(\"time\")",
+ "rawSql": "SELECT AVG(prompttokens) FROM DOKU_DATA WHERE \n $__timeFilter(\"time\")",
"refId": "A",
"sql": {
"columns": [
@@ -1190,7 +1194,7 @@ Grafana will attempt to connect to the database using the provided settings. If
"name": "AVG",
"parameters": [
{
- "name": "completiontokens",
+ "name": "prompttokens",
"type": "functionParameter"
}
],
@@ -1210,14 +1214,15 @@ Grafana will attempt to connect to the database using the provided settings. If
"table": "doku"
}
],
- "title": "Avg Completion Tokens / Request",
+ "title": "Avg Prompt Tokens / Request",
"type": "stat"
},
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "Provides a time-series analysis of token consumption for LLM requests, highlighting trends in usage intensity and complexity over time.",
"fieldConfig": {
"defaults": {
"color": {
@@ -1300,12 +1305,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "time_series",
"rawQuery": true,
- "rawSql": "SELECT prompttokens AS \"Prompt Tokens\", \"time\" AS \"time\", completiontokens AS \"Completion Tokens\", totaltokens AS \"Total Tokens\" FROM doku",
+ "rawSql": "SELECT prompttokens AS \"Prompt Tokens\", \"time\" AS \"time\", completiontokens AS \"Completion Tokens\", totaltokens AS \"Total Tokens\" FROM DOKU_DATA",
"refId": "A",
"sql": {
"columns": [
@@ -1369,8 +1374,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "This panel shows the average count of Completion tokens used per LLM request",
"fieldConfig": {
"defaults": {
"color": {
@@ -1399,7 +1405,7 @@ Grafana will attempt to connect to the database using the provided settings. If
"x": 0,
"y": 24
},
- "id": 9,
+ "id": 7,
"options": {
"colorMode": "background",
"graphMode": "area",
@@ -1420,12 +1426,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT AVG(prompttokens) FROM doku WHERE \n $__timeFilter(\"time\")",
+ "rawSql": "SELECT AVG(completiontokens) FROM DOKU_DATA WHERE \n $__timeFilter(\"time\")",
"refId": "A",
"sql": {
"columns": [
@@ -1433,7 +1439,7 @@ Grafana will attempt to connect to the database using the provided settings. If
"name": "AVG",
"parameters": [
{
- "name": "prompttokens",
+ "name": "completiontokens",
"type": "functionParameter"
}
],
@@ -1453,14 +1459,15 @@ Grafana will attempt to connect to the database using the provided settings. If
"table": "doku"
}
],
- "title": "Avg Prompt Tokens / Request",
+ "title": "Avg Completion Tokens / Request",
"type": "stat"
},
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "Displays the frequency of model usage over time, identifying the most utilised models and showcasing usage trends across different periods",
"fieldConfig": {
"defaults": {
"color": {
@@ -1514,12 +1521,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT \n model,\n total_count,\n percentage_usage\nFROM \n (\n SELECT \n model,\n COUNT(*) AS total_count,\n ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER(), 2) AS percentage_usage\n FROM \n doku\n WHERE $__timeFilter(time)\n GROUP BY \n model\n ORDER BY \n percentage_usage DESC\n ) sub\nLIMIT 5;",
+ "rawSql": "SELECT \n model,\n total_count,\n percentage_usage\nFROM \n (\n SELECT \n model,\n COUNT(*) AS total_count,\n ROUND(100.0 * COUNT(*) / SUM(COUNT(*)) OVER(), 2) AS percentage_usage\n FROM \n DOKU_DATA\n WHERE $__timeFilter(time)\n GROUP BY \n model\n ORDER BY \n percentage_usage DESC\n ) sub\nLIMIT 5;",
"refId": "A",
"sql": {
"columns": [
@@ -1546,14 +1553,15 @@ Grafana will attempt to connect to the database using the provided settings. If
"table": "doku"
}
],
- "title": "Top used models",
+ "title": "Top models by usage",
"type": "bargauge"
},
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "Breaks down token usage across different LLM models, offering insights into which models are driving the highest token consumption.",
"fieldConfig": {
"defaults": {
"color": {
@@ -1631,12 +1639,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "time_series",
"rawQuery": true,
- "rawSql": "SELECT \n DATE_TRUNC('day', \"time\") AS \"time\",\n model,\n COUNT(*) AS \"Model:\"\nFROM \n doku\nWHERE \n $__timeFilter(\"time\")\nGROUP BY \n 1, model\nORDER BY \n \"time\" ASC, model;",
+ "rawSql": "SELECT \n DATE_TRUNC('day', \"time\") AS \"time\",\n model,\n COUNT(*) AS \"Model:\"\nFROM \n DOKU_DATA\nWHERE \n $__timeFilter(\"time\")\nGROUP BY \n 1, model\nORDER BY \n \"time\" ASC, model;",
"refId": "A",
"sql": {
"columns": [
@@ -1669,8 +1677,9 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
+ "description": "Table showing prompts and responses along with their associated costs and token usage.",
"fieldConfig": {
"defaults": {
"color": {
@@ -1726,12 +1735,12 @@ Grafana will attempt to connect to the database using the provided settings. If
{
"datasource": {
"type": "grafana-postgresql-datasource",
- "uid": "dbed68e2-974f-495c-9ed1-86b2e33253c1"
+ "uid": "${datasource}"
},
"editorMode": "code",
"format": "table",
"rawQuery": true,
- "rawSql": "SELECT \"time\", applicationName, model, prompt, response, usageCost FROM doku WHERE $__timeFilter(time)",
+ "rawSql": "SELECT \"time\", applicationName, model, prompt, response, usageCost FROM DOKU_DATA WHERE $__timeFilter(time)",
"refId": "A",
"sql": {
"columns": [
@@ -1785,6 +1794,7 @@ Grafana will attempt to connect to the database using the provided settings. If
"table": "doku"
}
],
+ "title": "User Activity",
"transparent": true,
"type": "table"
}
@@ -1798,17 +1808,37 @@ Grafana will attempt to connect to the database using the provided settings. If
"Anthropic"
],
"templating": {
- "list": []
+ "list": [
+ {
+ "current": {
+ "selected": true,
+ "text": "grafana-postgresql-datasource",
+ "value": "dbd38750-cf8a-4f35-a031-af943c441f1d"
+ },
+ "hide": 0,
+ "includeAll": false,
+ "label": "Datasource",
+ "multi": false,
+ "name": "datasource",
+ "options": [],
+ "query": "grafana-postgresql-datasource",
+ "queryValue": "",
+ "refresh": 1,
+ "regex": "",
+ "skipUrlSync": false,
+ "type": "datasource"
+ }
+ ]
},
"time": {
- "from": "now-24h",
+ "from": "now-7d",
"to": "now"
},
"timepicker": {},
"timezone": "",
- "title": "LLM Monitoring",
- "uid": "cd0a92bf-7abe-427b-8e65-c61049be328e",
- "version": 4,
+ "title": "Doku - LLM Monitoring",
+ "uid": "doku-llm-monitoring",
+ "version": 1,
"weekStart": ""
}
```
diff --git a/docs/images/grafana-dashboard-1.jpg b/docs/images/grafana-dashboard-1.jpg
new file mode 100644
index 00000000..b9648426
Binary files /dev/null and b/docs/images/grafana-dashboard-1.jpg differ
diff --git a/docs/images/grafana-dashboard-2.jpg b/docs/images/grafana-dashboard-2.jpg
new file mode 100644
index 00000000..d0a4d46a
Binary files /dev/null and b/docs/images/grafana-dashboard-2.jpg differ
diff --git a/docs/images/grafana-dashboard-3.png b/docs/images/grafana-dashboard-3.png
new file mode 100644
index 00000000..ed1e9d99
Binary files /dev/null and b/docs/images/grafana-dashboard-3.png differ
diff --git a/docs/mint.json b/docs/mint.json
index 9e6312bb..a439d259 100644
--- a/docs/mint.json
+++ b/docs/mint.json
@@ -19,7 +19,7 @@
"topbarLinks": [
{
"name": "Contact Us",
- "url": "mailto:[email protected]"
+ "url": "mailto:[email protected]"
}
],
"modeToggle": {
diff --git a/src/ingester/config/config.go b/src/ingester/config/config.go
index 07a7cb9a..02789106 100644
--- a/src/ingester/config/config.go
+++ b/src/ingester/config/config.go
@@ -18,16 +18,14 @@ type Configuration struct {
URL string `yaml:"url"`
} `yaml:"pricingInfo"`
DBConfig struct {
- DBName string `yaml:"name"`
- DBUser string `yaml:"username"`
- DBPassword string `yaml:"password"`
- DBHost string `yaml:"host"`
- DBPort string `yaml:"port"`
- DBSSLMode string `yaml:"sslMode"`
- MaxOpenConns int `yaml:"maxOpenConns"`
- MaxIdleConns int `yaml:"maxIdleConns"`
- DataTableName string `yaml:"dataTable"`
- APIKeyTableName string `yaml:"apiKeyTable"`
+ DBName string `yaml:"name"`
+ DBUser string `yaml:"username"`
+ DBPassword string `yaml:"password"`
+ DBHost string `yaml:"host"`
+ DBPort string `yaml:"port"`
+ DBSSLMode string `yaml:"sslMode"`
+ MaxOpenConns int `yaml:"maxOpenConns"`
+ MaxIdleConns int `yaml:"maxIdleConns"`
} `yaml:"dbConfig"`
ObservabilityPlatform struct {
Enabled bool `yaml:"enabled"`
diff --git a/src/ingester/db/db.go b/src/ingester/db/db.go
index e312597f..43d56381 100644
--- a/src/ingester/db/db.go
+++ b/src/ingester/db/db.go
@@ -49,16 +49,14 @@ var (
// DBConfig holds the database configuration
type DatabaseConfig struct {
- DBName string
- User string
- Password string
- Host string
- Port string
- SSLMode string
- MaxIdleConns int
- MaxOpenConns int
- DataTableName string
- ApiKeyTableName string
+ DBName string
+ User string
+ Password string
+ Host string
+ Port string
+ SSLMode string
+ MaxIdleConns int
+ MaxOpenConns int
}
// PingDB attempts to ping the database to check if it's alive.
@@ -162,9 +160,9 @@ func tableExists(db *sql.DB, tableName string) (bool, error) {
// createTable creates a table in the database if it doesn't exist.
func createTable(db *sql.DB, tableName string) error {
var createTableSQL string
- if tableName == dbConfig.ApiKeyTableName {
+ if tableName == "DOKU_API_KEYS" {
createTableSQL = getCreateAPIKeysTableSQL(tableName)
- } else if tableName == dbConfig.DataTableName {
+ } else if tableName == "DOKU_DATA" {
createTableSQL = getCreateDataTableSQL(tableName)
}
@@ -179,7 +177,7 @@ func createTable(db *sql.DB, tableName string) error {
}
log.Info().Msgf("Table '%s' created in the database", tableName)
- if tableName == dbConfig.ApiKeyTableName {
+ if tableName == "DOKU_API_KEYS" {
createIndexSQL := fmt.Sprintf("CREATE INDEX IF NOT EXISTS idx_api_key ON %s (api_key);", tableName)
_, err = db.Exec(createIndexSQL)
if err != nil {
@@ -189,7 +187,7 @@ func createTable(db *sql.DB, tableName string) error {
}
// If the table to create is the data table, convert it into a hypertable
- if tableName == dbConfig.DataTableName {
+ if tableName == "DOKU_DATA" {
_, err := db.Exec("SELECT create_hypertable($1, 'time')", tableName)
if err != nil {
return fmt.Errorf("Error creating hypertable: %w", err)
@@ -264,7 +262,7 @@ func insertDataToDB(data map[string]interface{}) (string, int) {
go obsPlatform.SendToPlatform(data)
// Define the SQL query for data insertion
- query := fmt.Sprintf("INSERT INTO %s (time, name, environment, endpoint, sourceLanguage, applicationName, completionTokens, promptTokens, totalTokens, finishReason, requestDuration, usageCost, model, prompt, response, imageSize, revisedPrompt, image, audioVoice, finetuneJobId, finetuneJobStatus) VALUES (NOW(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)", dbConfig.DataTableName)
+ query := fmt.Sprintf("INSERT INTO %s (time, name, environment, endpoint, sourceLanguage, applicationName, completionTokens, promptTokens, totalTokens, finishReason, requestDuration, usageCost, model, prompt, response, imageSize, revisedPrompt, image, audioVoice, finetuneJobId, finetuneJobStatus) VALUES (NOW(), $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)", "DOKU_DATA")
// Execute the SQL query
_, err := db.Exec(query,
@@ -303,16 +301,14 @@ func Init(cfg config.Configuration) error {
// Initialize the database configuration
dbConfig = DatabaseConfig{
- DBName: cfg.DBConfig.DBName,
- User: cfg.DBConfig.DBUser,
- Password: cfg.DBConfig.DBPassword,
- Host: cfg.DBConfig.DBHost,
- Port: cfg.DBConfig.DBPort,
- SSLMode: cfg.DBConfig.DBSSLMode,
- MaxIdleConns: cfg.DBConfig.MaxIdleConns,
- MaxOpenConns: cfg.DBConfig.MaxOpenConns,
- DataTableName: cfg.DBConfig.DataTableName,
- ApiKeyTableName: cfg.DBConfig.APIKeyTableName,
+ DBName: cfg.DBConfig.DBName,
+ User: cfg.DBConfig.DBUser,
+ Password: cfg.DBConfig.DBPassword,
+ Host: cfg.DBConfig.DBHost,
+ Port: cfg.DBConfig.DBPort,
+ SSLMode: cfg.DBConfig.DBSSLMode,
+ MaxIdleConns: cfg.DBConfig.MaxIdleConns,
+ MaxOpenConns: cfg.DBConfig.MaxOpenConns,
}
err := initializeDB()
@@ -322,17 +318,17 @@ func Init(cfg config.Configuration) error {
}
// Create the DATA and API keys table if it doesn't exist.
- log.Info().Msgf("Creating '%s' and '%s' tables in the database if they don't exist", dbConfig.ApiKeyTableName, dbConfig.DataTableName)
+ log.Info().Msgf("Creating '%s' and '%s' tables in the database if they don't exist", "DOKU_API_KEYS", "DOKU_DATA")
- err = createTable(db, dbConfig.ApiKeyTableName)
+ err = createTable(db, "DOKU_API_KEYS")
if err != nil {
- log.Error().Err(err).Msgf("Error creating table %s", dbConfig.ApiKeyTableName)
+ log.Error().Err(err).Msgf("Error creating table %s", "DOKU_API_KEYS")
return err
}
- err = createTable(db, dbConfig.DataTableName)
+ err = createTable(db, "DOKU_DATA")
if err != nil {
- log.Error().Err(err).Msgf("Error creating table %s", dbConfig.ApiKeyTableName)
+ log.Error().Err(err).Msgf("Error creating table %s", "DOKU_API_KEYS")
return err
}
return nil
@@ -351,7 +347,7 @@ func PerformDatabaseInsertion(data map[string]interface{}) (string, int) {
func CheckAPIKey(apiKey string) (string, error) {
var name string
- query := fmt.Sprintf("SELECT name FROM %s WHERE api_key = $1", dbConfig.ApiKeyTableName)
+ query := fmt.Sprintf("SELECT name FROM %s WHERE api_key = $1", "DOKU_API_KEYS")
err := db.QueryRow(query, apiKey).Scan(&name)
if err != nil {
if err == sql.ErrNoRows {
@@ -366,7 +362,7 @@ func CheckAPIKey(apiKey string) (string, error) {
func GenerateAPIKey(existingAPIKey, name string) (string, error) {
// If there are any existing API keys, authenticate the provided API key before proceeding
var count int
- countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", dbConfig.ApiKeyTableName)
+ countQuery := fmt.Sprintf("SELECT COUNT(*) FROM %s", "DOKU_API_KEYS")
err := db.QueryRow(countQuery).Scan(&count)
if err != nil {
log.Error().Err(err).Msg("Error checking API key table")
@@ -390,7 +386,7 @@ func GenerateAPIKey(existingAPIKey, name string) (string, error) {
newAPIKey, _ := generateSecureRandomKey()
// Insert the new API key into the database
- insertQuery := fmt.Sprintf("INSERT INTO %s (api_key, name) VALUES ($1, $2)", dbConfig.ApiKeyTableName)
+ insertQuery := fmt.Sprintf("INSERT INTO %s (api_key, name) VALUES ($1, $2)", "DOKU_API_KEYS")
_, err = db.Exec(insertQuery, newAPIKey, name)
if err != nil {
log.Error().Err(err).Msg("Error inserting the new API key in the database")
@@ -412,7 +408,7 @@ func GetAPIKeyForName(existingAPIKey, name string) (string, error) {
// Retrieve the API key for the given name
var apiKey string
- query := fmt.Sprintf("SELECT api_key FROM %s WHERE name = $1", dbConfig.ApiKeyTableName)
+ query := fmt.Sprintf("SELECT api_key FROM %s WHERE name = $1", "DOKU_API_KEYS")
err = db.QueryRow(query, name).Scan(&apiKey)
if err != nil {
if err == sql.ErrNoRows {
@@ -443,7 +439,7 @@ func DeleteAPIKey(existingAPIKey, name string) error {
// Delete the API key from the database
log.Info().Msgf("Deleting API Key with the name '%s' from the database", name)
- query := fmt.Sprintf("DELETE FROM %s WHERE api_key = $1", dbConfig.ApiKeyTableName)
+ query := fmt.Sprintf("DELETE FROM %s WHERE api_key = $1", "DOKU_API_KEYS")
_, err = db.Exec(query, apiKey)
if err != nil {
log.Error().Err(err).Msg("Error deleting API key")
diff --git a/src/ingester/obsPlatform/grafana-cloud.go b/src/ingester/obsPlatform/grafana-cloud.go
new file mode 100644
index 00000000..abc5cc26
--- /dev/null
+++ b/src/ingester/obsPlatform/grafana-cloud.go
@@ -0,0 +1,185 @@
+package obsPlatform
+
+import (
+ "bytes"
+ "fmt"
+ "github.com/rs/zerolog/log"
+ "net/http"
+ "strconv"
+ "strings"
+ "time"
+)
+
+func configureGrafanaCloudData(data map[string]interface{}) {
+ // The current time for the timestamp field.
+ currentTime := strconv.FormatInt(time.Now().UnixNano(), 10)
+
+ // Extract the platform from the endpoint string
+ endpointParts := strings.Split(data["endpoint"].(string), ".")
+ if len(endpointParts) > 0 {
+ platform = endpointParts[0] // The first part of the endpoint string is the platform.
+ } else {
+ platform = "Unknown" // If the endpoint string is not in the expected format, set the platform to "unknown".
+ }
+
+ // Determine the type based on the endpoint by consulting the mapping.
+ call_type, found := endpointTypeMapping[data["endpoint"].(string)]
+ if !found {
+ call_type = "Unknown"
+ }
+ fmt.Println("Platform: ", call_type)
+
+ if data["endpoint"] == "openai.chat.completions" || data["endpoint"] == "openai.completions" || data["endpoint"] == "cohere.generate" || data["endpoint"] == "cohere.chat" || data["endpoint"] == "cohere.summarize" || data["endpoint"] == "anthropic.completions" {
+ if data["finishReason"] == nil {
+ data["finishReason"] = "null"
+ }
+ metrics := []string{
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v,platform=%v,generation=%v completionTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], platform, call_type, data["completionTokens"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v,platform=%v,generation=%v promptTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], platform, call_type, data["promptTokens"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v,platform=%v,generation=%v totalTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], platform, call_type, data["totalTokens"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v,platform=%v,generation=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], platform, call_type, data["requestDuration"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v,platform=%v,generation=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], platform, call_type, data["usageCost"]),
+ }
+ var metricsBody = []byte(strings.Join(metrics, "\n"))
+ authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
+ err := sendTelemetryGrafanaCloud(metricsBody, authHeader, grafanaPromUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
+ }
+
+ authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
+
+ response_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"response\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, normalizeString(data["response"].(string))))
+ err = sendTelemetryGrafanaCloud(response_log, authHeader, grafanaLokiUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
+ }
+
+ prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"prompt\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, normalizeString(data["prompt"].(string))))
+ err = sendTelemetryGrafanaCloud(prompt_log, authHeader, grafanaLokiUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
+ }
+ } else if data["endpoint"] == "openai.embeddings" || data["endpoint"] == "cohere.embed" {
+ if data["endpoint"] == "openai.embeddings" {
+ metrics := []string{
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,platform=%v,generation=%v promptTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, data["promptTokens"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,platform=%v,generation=%v totalTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, data["totalTokens"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,platform=%v,generation=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, data["requestDuration"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,platform=%v,generation=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, data["usageCost"]),
+ }
+ var metricsBody = []byte(strings.Join(metrics, "\n"))
+ authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
+ err := sendTelemetryGrafanaCloud(metricsBody, authHeader, grafanaPromUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
+ }
+
+ authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
+ prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"prompt\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, data["prompt"]))
+ err = sendTelemetryGrafanaCloud(prompt_log, authHeader, grafanaLokiUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
+ }
+ } else {
+ metrics := []string{
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,platform=%v,generation=%v promptTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, data["promptTokens"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,platform=%v,generation=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, data["requestDuration"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,platform=%v,generation=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, data["usageCost"]),
+ }
+ var metricsBody = []byte(strings.Join(metrics, "\n"))
+ authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
+ err := sendTelemetryGrafanaCloud(metricsBody, authHeader, grafanaPromUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
+ }
+
+ authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
+ prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"prompt\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, data["prompt"]))
+ err = sendTelemetryGrafanaCloud(prompt_log, authHeader, grafanaLokiUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
+ }
+ }
+ } else if data["endpoint"] == "openai.fine_tuning" {
+ metrics := []string{
+ fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finetuneJobId=%v,platform=%v,generation=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finetuneJobId"], platform, call_type, data["requestDuration"]),
+ }
+ var metricsBody = []byte(strings.Join(metrics, "\n"))
+ authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
+ err := sendTelemetryGrafanaCloud(metricsBody, authHeader, grafanaPromUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
+ }
+ } else if data["endpoint"] == "openai.images.create" || data["endpoint"] == "openai.images.create.variations" {
+ metrics := []string{
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,imageSize=%v,imageQuality=%v,platform=%v,generation=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["imageSize"], data["imageQuality"], platform, call_type, data["requestDuration"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,imageSize=%v,imageQuality=%v,platform=%v,generation=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["imageSize"], data["imageQuality"], platform, call_type, data["usageCost"]),
+ }
+ var metricsBody = []byte(strings.Join(metrics, "\n"))
+ authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
+ err := sendTelemetryGrafanaCloud(metricsBody, authHeader, grafanaPromUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
+ }
+
+ authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
+ if data["endpoint"] != "openai.images.create.variations" {
+ prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"prompt\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, data["revisedPrompt"]))
+ if data["model"] == "dall-e-2" {
+ prompt_log = []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"prompt\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, data["prompt"]))
+ }
+ err = sendTelemetryGrafanaCloud(prompt_log, authHeader, grafanaLokiUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
+ }
+ }
+ image_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"image\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, data["image"]))
+ err = sendTelemetryGrafanaCloud(image_log, authHeader, grafanaLokiUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
+ }
+ } else if data["endpoint"] == "openai.audio.speech.create" {
+ metrics := []string{
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,audioVoice=%v,platform=%v,generation=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["audioVoice"], platform, call_type, data["requestDuration"]),
+ fmt.Sprintf(`doku_llm,job=doku,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,audioVoice=%v,platform=%v,generation=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["audioVoice"], platform, call_type, data["usageCost"]),
+ }
+ var metricsBody = []byte(strings.Join(metrics, "\n"))
+ authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
+ err := sendTelemetryGrafanaCloud(metricsBody, authHeader, grafanaPromUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
+ }
+
+ authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
+ prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"platform\": \"%v\", \"generation\": \"%v\", \"type\": \"prompt\", \"job\": \"doku\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], platform, call_type, currentTime, data["prompt"]))
+ err = sendTelemetryGrafanaCloud(prompt_log, authHeader, grafanaLokiUrl, "POST")
+ if err != nil {
+ log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
+ }
+ }
+}
+
+func sendTelemetryGrafanaCloud(telemetryData []byte, authHeader string, url string, requestType string) error {
+
+ req, err := http.NewRequest(requestType, url, bytes.NewBuffer(telemetryData))
+ if err != nil {
+ return fmt.Errorf("Error creating request")
+ }
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Authorization", authHeader)
+
+ resp, err := httpClient.Do(req)
+ if err != nil {
+ return fmt.Errorf("Error sending request to %v", url)
+ } else if resp.StatusCode == 404 {
+ return fmt.Errorf("Provided URL %v is not valid", url)
+ } else if resp.StatusCode == 401 {
+ return fmt.Errorf("Provided credentials are not valid")
+ }
+
+ defer resp.Body.Close()
+
+ log.Info().Msgf("Successfully exported data to %v", url)
+ return nil
+}
diff --git a/src/ingester/obsPlatform/obsPlatform.go b/src/ingester/obsPlatform/obsPlatform.go
index 6b76074e..1fc29feb 100644
--- a/src/ingester/obsPlatform/obsPlatform.go
+++ b/src/ingester/obsPlatform/obsPlatform.go
@@ -1,12 +1,9 @@
package obsPlatform
import (
- "bytes"
- "fmt"
"ingester/config"
"net/http"
"regexp"
- "strconv"
"strings"
"time"
@@ -16,17 +13,32 @@ import (
var (
ObservabilityPlatform string // ObservabilityPlatform contains the information on the platform in use.
httpClient *http.Client // httpClient is the HTTP client used to send data to the Observability Platform.
- grafanaPromUrl string // grafanaPrometheusUrl is the URL used to send data to Grafana Prometheus.
- grafanaPromUsername string // grafanaPrometheusUsername is the username used to send data to Grafana Prometheus.
- grafanaLokiUrl string // grafanaPostUrl is the URL used to send data to Grafana Loki.
- grafanaLokiUsername string // grafanaLokiUsername is the username used to send data to Grafana Loki.
- grafanaAccessToken string // grafanaAccessToken is the access token used to send data to Grafana.
- newRelicLicenseKey string // newRelicKey is the key used to send data to New Relic.
- newRelicMetricsUrl string // newRelicMetricsUrl is the URL used to send data to New Relic.
- newRelicLogsUrl string // newRelicLogsUrl is the URL used to send logs to New Relic.
- dataDogMetricsUrl string // dataDogMetricsUrl is the URL used to send data to DataDog.
- dataDogLogsUrl string // dataDogLogsUrl is the URL used to send logs to DataDog.
- dataDogAPIKey string // dataDogAPIKey is the API key used to send data to DataDog.
+ platform, call_type string // platform is the platform used to send data to the Observability Platform.
+ endpointTypeMapping = map[string]string{
+ "openai.chat.completions": "Chat",
+ "openai.completions": "Chat",
+ "cohere.generate": "Chat",
+ "cohere.chat": "Chat",
+ "cohere.summarize": "Chat",
+ "anthropic.completions": "Chat",
+ "openai.embeddings": "Embeddings",
+ "cohere.embed": "Embeddings",
+ "openai.images.create": "Image",
+ "openai.images.create.variations": "Image",
+ "openai.audio.speech.create": "Audio",
+ "openai.fine_tuning": "FineTuning",
+ }
+ grafanaPromUrl string // grafanaPrometheusUrl is the URL used to send data to Grafana Prometheus.
+ grafanaPromUsername string // grafanaPrometheusUsername is the username used to send data to Grafana Prometheus.
+ grafanaLokiUrl string // grafanaPostUrl is the URL used to send data to Grafana Loki.
+ grafanaLokiUsername string // grafanaLokiUsername is the username used to send data to Grafana Loki.
+ grafanaAccessToken string // grafanaAccessToken is the access token used to send data to Grafana.
+ newRelicLicenseKey string // newRelicKey is the key used to send data to New Relic.
+ newRelicMetricsUrl string // newRelicMetricsUrl is the URL used to send data to New Relic.
+ newRelicLogsUrl string // newRelicLogsUrl is the URL used to send logs to New Relic.
+ dataDogMetricsUrl string // dataDogMetricsUrl is the URL used to send data to DataDog.
+ dataDogLogsUrl string // dataDogLogsUrl is the URL used to send logs to DataDog.
+ dataDogAPIKey string // dataDogAPIKey is the API key used to send data to DataDog.
)
func normalizeString(s string) string {
@@ -74,135 +86,7 @@ func Init(cfg config.Configuration) error {
// SendToPlatform sends observability data to the appropriate platform.
func SendToPlatform(data map[string]interface{}) {
if grafanaLokiUrl != "" {
- if data["endpoint"] == "openai.chat.completions" || data["endpoint"] == "openai.completions" || data["endpoint"] == "cohere.generate" || data["endpoint"] == "cohere.chat" || data["endpoint"] == "cohere.summarize" || data["endpoint"] == "anthropic.completions" {
- if data["finishReason"] == nil {
- data["finishReason"] = "null"
- }
- metrics := []string{
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v completionTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], data["completionTokens"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v promptTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], data["promptTokens"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v totalTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], data["totalTokens"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], data["requestDuration"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finishReason=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finishReason"], data["usageCost"]),
- }
- var metricsBody = []byte(strings.Join(metrics, "\n"))
- authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
- err := sendTelemetry(metricsBody, authHeader, grafanaPromUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
- }
-
- authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
-
- response_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"response\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), normalizeString(data["response"].(string))))
- err = sendTelemetry(response_log, authHeader, grafanaLokiUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
- }
-
- prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"prompt\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), normalizeString(data["prompt"].(string))))
- err = sendTelemetry(prompt_log, authHeader, grafanaLokiUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
- }
- } else if data["endpoint"] == "openai.embeddings" || data["endpoint"] == "cohere.embed" {
- if data["endpoint"] == "openai.embeddings" {
- metrics := []string{
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v promptTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["promptTokens"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v totalTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["totalTokens"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["requestDuration"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["usageCost"]),
- }
- var metricsBody = []byte(strings.Join(metrics, "\n"))
- authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
- err := sendTelemetry(metricsBody, authHeader, grafanaPromUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
- }
-
- authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
- prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"prompt\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), data["prompt"]))
- err = sendTelemetry(prompt_log, authHeader, grafanaLokiUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
- }
- } else {
- metrics := []string{
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v promptTokens=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["promptTokens"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["requestDuration"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["usageCost"]),
- }
- var metricsBody = []byte(strings.Join(metrics, "\n"))
- authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
- err := sendTelemetry(metricsBody, authHeader, grafanaPromUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
- }
-
- authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
- prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"prompt\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), data["prompt"]))
- err = sendTelemetry(prompt_log, authHeader, grafanaLokiUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
- }
- }
- } else if data["endpoint"] == "openai.fine_tuning" {
- metrics := []string{
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,finetuneJobId=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["finetuneJobId"], data["requestDuration"]),
- }
- var metricsBody = []byte(strings.Join(metrics, "\n"))
- authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
- err := sendTelemetry(metricsBody, authHeader, grafanaPromUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
- }
- } else if data["endpoint"] == "openai.images.create" || data["endpoint"] == "openai.images.create.variations" {
- metrics := []string{
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,imageSize=%v,imageQuality=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["imageSize"], data["imageQuality"], data["requestDuration"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,imageSize=%v,imageQuality=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["imageSize"], data["imageQuality"], data["usageCost"]),
- }
- var metricsBody = []byte(strings.Join(metrics, "\n"))
- authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
- err := sendTelemetry(metricsBody, authHeader, grafanaPromUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
- }
-
- authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
- if data["endpoint"] != "openai.images.create.variations" {
- prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"prompt\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), data["revisedPrompt"]))
- if data["model"] == "dall-e-2" {
- prompt_log = []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"prompt\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), data["prompt"]))
- }
- err = sendTelemetry(prompt_log, authHeader, grafanaLokiUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
- }
- }
- image_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"image\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), data["image"]))
- err = sendTelemetry(image_log, authHeader, grafanaLokiUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
- }
- } else if data["endpoint"] == "openai.audio.speech.create" {
- metrics := []string{
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,audioVoice=%v requestDuration=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["audioVoice"], data["requestDuration"]),
- fmt.Sprintf(`doku_llm,environment=%v,endpoint=%v,applicationName=%v,source=%v,model=%v,audioVoice=%v usageCost=%v`, data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], data["audioVoice"], data["usageCost"]),
- }
- var metricsBody = []byte(strings.Join(metrics, "\n"))
- authHeader := fmt.Sprintf("Bearer %v:%v", grafanaPromUsername, grafanaAccessToken)
- err := sendTelemetry(metricsBody, authHeader, grafanaPromUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Prometheus")
- }
-
- authHeader = fmt.Sprintf("Bearer %v:%v", grafanaLokiUsername, grafanaAccessToken)
- prompt_log := []byte(fmt.Sprintf("{\"streams\": [{\"stream\": {\"environment\": \"%v\",\"endpoint\": \"%v\", \"applicationName\": \"%v\", \"source\": \"%v\", \"model\": \"%v\", \"type\": \"prompt\" }, \"values\": [[\"%s\", \"%v\"]]}]}", data["environment"], data["endpoint"], data["applicationName"], data["sourceLanguage"], data["model"], strconv.FormatInt(time.Now().UnixNano(), 10), data["prompt"]))
- err = sendTelemetry(prompt_log, authHeader, grafanaLokiUrl, "POST")
- if err != nil {
- log.Error().Err(err).Msgf("Error sending data to Grafana Cloud Loki")
- }
- }
+ configureGrafanaCloudData(data)
} else if newRelicMetricsUrl != "" {
configureNewRelicData(data)
} else if dataDogMetricsUrl != "" {
@@ -211,27 +95,3 @@ func SendToPlatform(data map[string]interface{}) {
log.Info().Msg("No Observability Platform configured")
}
}
-
-func sendTelemetry(telemetryData []byte, authHeader string, url string, requestType string) error {
-
- req, err := http.NewRequest(requestType, url, bytes.NewBuffer(telemetryData))
- if err != nil {
- return fmt.Errorf("Error creating request")
- }
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Authorization", authHeader)
-
- resp, err := httpClient.Do(req)
- if err != nil {
- return fmt.Errorf("Error sending request to %v", url)
- } else if resp.StatusCode == 404 {
- return fmt.Errorf("Provided URL %v is not valid", url)
- } else if resp.StatusCode == 401 {
- return fmt.Errorf("Provided credentials are not valid")
- }
-
- defer resp.Body.Close()
-
- log.Info().Msgf("Successfully exported data to %v", url)
- return nil
-}