diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 1850e808487..49d503cce43 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -105,6 +105,7 @@ /pkg/server/ @grafana/backend-platform /pkg/apiserver @grafana/grafana-app-platform-squad /pkg/apimachinery @grafana/grafana-app-platform-squad +/pkg/promlib @grafana/observability-metrics /pkg/services/annotations/ @grafana/backend-platform /pkg/services/apikey/ @grafana/identity-access-team /pkg/services/cleanup/ @grafana/backend-platform diff --git a/Makefile b/Makefile index f6f07671f02..3d53ca4dcfc 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ include .bingo/Variables.mk .PHONY: all deps-go deps-js deps build-go build-backend build-server build-cli build-js build build-docker-full build-docker-full-ubuntu lint-go golangci-lint test-go test-js gen-ts test run run-frontend clean devenv devenv-down protobuf drone help gen-go gen-cue fix-cue GO = go -GO_FILES ?= ./pkg/... ./pkg/apiserver/... ./pkg/apimachinery/... +GO_FILES ?= ./pkg/... ./pkg/apiserver/... ./pkg/apimachinery/... ./pkg/promlib/... SH_FILES ?= $(shell find ./scripts -name *.sh) GO_BUILD_FLAGS += $(if $(GO_BUILD_DEV),-dev) GO_BUILD_FLAGS += $(if $(GO_BUILD_TAGS),-build-tags=$(GO_BUILD_TAGS)) diff --git a/go.mod b/go.mod index 9f83190cea9..dfc6557978a 100644 --- a/go.mod +++ b/go.mod @@ -122,7 +122,7 @@ require ( gopkg.in/mail.v2 v2.3.1 // @grafana/backend-platform gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // @grafana/alerting-squad-backend - xorm.io/builder v0.3.6 // @grafana/backend-platform + xorm.io/builder v0.3.6 // indirect; @grafana/backend-platform xorm.io/core v0.7.3 // @grafana/backend-platform xorm.io/xorm v0.8.2 // @grafana/alerting-squad-backend ) @@ -174,7 +174,7 @@ require ( github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20191002090509-6af20e3a5340 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-msgpack v0.5.5 // indirect - github.com/hashicorp/go-multierror v1.1.1 // @grafana/alerting-squad + github.com/hashicorp/go-multierror v1.1.1 // indirect; @grafana/alerting-squad github.com/hashicorp/go-sockaddr v1.0.6 // indirect github.com/hashicorp/golang-lru v0.6.0 // indirect github.com/hashicorp/yamux v0.1.1 // indirect @@ -337,7 +337,7 @@ require ( github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect - github.com/hashicorp/golang-lru/v2 v2.0.7 // @grafana/alerting-squad-backend + github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect; @grafana/alerting-squad-backend github.com/hashicorp/memberlist v0.5.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/invopop/yaml v0.2.0 // indirect diff --git a/go.work b/go.work index 33dd16d7ea2..3bb653aaf42 100644 --- a/go.work +++ b/go.work @@ -4,6 +4,7 @@ use ( . ./pkg/apimachinery ./pkg/apiserver + ./pkg/promlib ./pkg/util/xorm ) diff --git a/go.work.sum b/go.work.sum index 5c27afae06d..5e6aae55939 100644 --- a/go.work.sum +++ b/go.work.sum @@ -247,19 +247,18 @@ github.com/aws/aws-sdk-go-v2/service/sns v1.17.4 h1:7TdmoJJBwLFyakXjfrGztejwY5Ie github.com/aws/aws-sdk-go-v2/service/sqs v1.18.3 h1:uHjK81fESbGy2Y9lspub1+C6VN5W2UXTDo2A/Pm4G0U= github.com/aws/aws-sdk-go-v2/service/ssm v1.24.1 h1:zc1YLcknvxdW/i1MuJKmEnFB2TNkOfguuQaGRvJXPng= github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible h1:Ppm0npCCsmuR9oQaBtRuZcmILVE74aXE+AmrJj8L2ns= -github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs= -github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/bwesterb/go-ristretto v1.2.3 h1:1w53tCkGhCQ5djbat3+MH0BAQ5Kfgbt56UZQ/JMzngw= github.com/casbin/casbin/v2 v2.37.0 h1:/poEwPSovi4bTOcP752/CsTQiRz2xycyVKFG7GUhbDw= github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g= github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= +github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2/go.mod h1:At5TxYYdxkbQL0TSefRjhLE3Q0lgvqKKMSFUglJ7i1U= github.com/chromedp/chromedp v0.9.2 h1:dKtNz4kApb06KuSXoTQIyUC2TrA0fhGDwNZf3bcgfKw= github.com/chromedp/sysutil v1.0.0 h1:+ZxhTpfpZlmchB58ih/LBHX52ky7w2VhQVKQMucy3Ic= github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM= @@ -350,7 +349,6 @@ github.com/go-fonts/stix v0.1.0 h1:UlZlgrvvmT/58o573ot7NFw0vZasZ5I6bcIft/oMdgg= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 h1:QbL/5oDUmRBzO9/Z7Seo6zf912W/a6Sr4Eu0G/3Jho0= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4 h1:WtGNWLvXpe6ZudgnXrq0barxBImvnnJoMEhXAzcbM0I= github.com/go-ini/ini v1.25.4 h1:Mujh4R/dH6YL8bxuISne3xX2+qcQ9p0IxKAP6ExWoUo= -github.com/go-jose/go-jose/v3 v3.0.3 h1:fFKWeig/irsp7XD2zBxvnmA/XaRWp5V3CBsZXJF7G7k= github.com/go-kit/kit v0.12.0 h1:e4o3o3IsBfAKQh5Qbbiqyfu97Ku7jrO/JbohvztANh4= github.com/go-latex/latex v0.0.0-20210823091927-c0d11ff05a81 h1:6zl3BbBhdnMkpSj2YY30qV3gDcVBGtFgVsV3+/i+mKQ= github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab h1:xveKWz2iaueeTaUgdetzel+U7exyigDYBryyVfV/rZk= @@ -405,6 +403,7 @@ github.com/grafana/e2e v0.1.1-0.20221018202458-cffd2bb71c7b h1:Ha+kSIoTutf4ytlVw github.com/grafana/e2e v0.1.1-0.20221018202458-cffd2bb71c7b/go.mod h1:3UsooRp7yW5/NJQBlXcTsAHOoykEhNUYXkQ3r6ehEEY= github.com/grafana/gomemcache v0.0.0-20231023152154-6947259a0586 h1:/of8Z8taCPftShATouOrBVy6GaTTjgQd/VfNiZp/VXQ= github.com/grafana/gomemcache v0.0.0-20231023152154-6947259a0586/go.mod h1:PGk3RjYHpxMM8HFPhKKo+vve3DdlPUELZLSDEFehPuU= +github.com/grafana/grafana-plugin-sdk-go v0.212.0/go.mod h1:qsI4ktDf0lig74u8SLPJf9zRdVxWV/W4Wi+Ox6gifgs= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWetyv/0+wjACpqVH+eVULgEjkurDLq3goeM= github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1/go.mod h1:YvJ2f6MplWDhfxiUC3KpyTy76kYUZA4W3pTv/wdKQ9Y= github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645 h1:MJG/KsmcqMwFAkh8mTnAwhyKoB+sTAnY4CACC110tbU= @@ -430,7 +429,6 @@ github.com/ianlancetaylor/demangle v0.0.0-20230524184225-eabc099b10ab h1:BA4a7pe github.com/imkira/go-interpol v1.1.0 h1:KIiKr0VSG2CUW1hl1jpiyuzuJeKUUpC8iM1AIE7N1Vk= github.com/influxdata/influxdb v1.7.6 h1:8mQ7A/V+3noMGCt/P9pD09ISaiz9XvgCk303UYA3gcs= github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab h1:HqW4xhhynfjrtEiiSGcQUd6vrK23iMam1FO8rI7mwig= -github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI= github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0= github.com/iris-contrib/blackfriday v2.0.0+incompatible h1:o5sHQHHm0ToHUlAJSTjW9UWicjJSDDauOOQ2AHuIVp4= github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE= @@ -491,6 +489,7 @@ github.com/kataras/sitemap v0.0.5 h1:4HCONX5RLgVy6G4RkYOV3vKNcma9p236LdGOipJsaFE github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kisielk/errcheck v1.5.0 h1:e8esj/e4R+SAOwFwN+n3zr0nYeCyeweozKfO23MvHzY= github.com/kisielk/gotool v1.0.0 h1:AV2c/EiW3KqPNT9ZKl07ehoAGi4C5/01Cfbblndcapg= +github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/cpuid v1.2.1 h1:vJi+O/nMdFt0vqm8NZBI6wzALWdA2X+egi0ogNyrC/w= github.com/knadh/koanf v1.5.0 h1:q2TSd/3Pyc/5yP9ldIrSdIz26MCcyNQzW0pEAugLPNs= github.com/knadh/koanf v1.5.0/go.mod h1:Hgyjp4y8v44hpZtPzs7JZfRAW5AhN7KfZcwv1RYggDs= @@ -693,7 +692,6 @@ github.com/willf/bitset v1.1.11 h1:N7Z7E9UvjW+sGsEl7k/SJrvY2reP1A07MrGuCjIOjRE= github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI= github.com/willf/bloom v2.0.3+incompatible h1:QDacWdqcAUI1MPOwIQZRy9kOR7yxfyEmxX8Wdm2/JPA= github.com/willf/bloom v2.0.3+incompatible/go.mod h1:MmAltL9pDMNTrvUkxdg0k0q5I0suxmuwp3KbyrZLOZ8= -github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/xanzy/go-gitlab v0.15.0 h1:rWtwKTgEnXyNUGrOArN7yyc3THRkpYcKXIXia9abywQ= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= @@ -739,9 +737,10 @@ go.opentelemetry.io/contrib v0.18.0 h1:uqBh0brileIvG6luvBjdxzoFL8lxDGuhxJWsvK3Bv go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.42.0/go.mod h1:5z+/ZWJQKXa9YT34fQNx5K8Hd1EoIhvtUygUQPqEOgQ= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1/go.mod h1:4UoMYEZOC0yN/sPGH76KPkkU7zgiEWYWL9vwmbnTJPE= go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.47.0/go.mod h1:r9vWsPS/3AQItv3OSlEJ/E4mbrhUbbw18meOjArPtKQ= +go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.46.1/go.mod h1:GnOaBaFQ2we3b9AGWJpsBa7v1S5RlQzlC3O7dRMxZhM= go.opentelemetry.io/contrib/propagators/b3 v1.15.0 h1:bMaonPyFcAvZ4EVzkUNkfnUHP5Zi63CIDlA3dRsEg8Q= go.opentelemetry.io/contrib/propagators/b3 v1.15.0/go.mod h1:VjU0g2v6HSQ+NwfifambSLAeBgevjIcqmceaKWEzl0c= -go.opentelemetry.io/contrib/samplers/jaegerremote v0.18.0 h1:Q9PrD94WoMolBx44ef5UWWvufpVSME0MiSymXZfedso= +go.opentelemetry.io/contrib/samplers/jaegerremote v0.16.0/go.mod h1:StxwPndBVNZD2sZez0RQ0SP/129XGCd4aEmVGaw1/QM= go.opentelemetry.io/otel/bridge/opencensus v0.37.0 h1:ieH3gw7b1eg90ARsFAlAsX5LKVZgnCYfaDwRrK6xLHU= go.opentelemetry.io/otel/bridge/opencensus v0.37.0/go.mod h1:ddiK+1PE68l/Xk04BGTh9Y6WIcxcLrmcVxVlS0w5WZ0= go.opentelemetry.io/otel/bridge/opentracing v1.10.0 h1:WzAVGovpC1s7KD5g4taU6BWYZP3QGSDVTlbRu9fIHw8= @@ -764,12 +763,15 @@ go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee h1:0mgffUl7nfd+FpvXMVz4IDEa go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e/go.mod h1:Kr81I6Kryrl9sr8s2FK3vxD90NdsKWRuOIl2O4CvYbA= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/image v0.0.0-20220302094943-723b81ca9867 h1:TcHcE0vrmgzNH1v3ppjcMGbhG5+9fMuvOmUYwNEF4q4= golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028 h1:4+4C/Iv2U4fMZBiMCc98MG1In4gJY5YRhtpDNeDeHWs= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0= gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0 h1:OE9mWmgKkjJyEmDAAtGMPjXu+YNeGvK9VTSHY6+Qihc= gonum.org/v1/plot v0.10.1 h1:dnifSs43YJuNMDzB7v8wV64O4ABBHReuAVAoBxqBqS4= diff --git a/pkg/tsdb/prometheus/client/client.go b/pkg/promlib/client/client.go similarity index 98% rename from pkg/tsdb/prometheus/client/client.go rename to pkg/promlib/client/client.go index 47a5969d373..b23196b6f5b 100644 --- a/pkg/tsdb/prometheus/client/client.go +++ b/pkg/promlib/client/client.go @@ -13,7 +13,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/models" ) type doer interface { diff --git a/pkg/tsdb/prometheus/client/client_test.go b/pkg/promlib/client/client_test.go similarity index 98% rename from pkg/tsdb/prometheus/client/client_test.go rename to pkg/promlib/client/client_test.go index 1f957571601..16dc76fb270 100644 --- a/pkg/tsdb/prometheus/client/client_test.go +++ b/pkg/promlib/client/client_test.go @@ -11,7 +11,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/stretchr/testify/require" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/models" ) type MockDoer struct { diff --git a/pkg/tsdb/prometheus/client/transport.go b/pkg/promlib/client/transport.go similarity index 56% rename from pkg/tsdb/prometheus/client/transport.go rename to pkg/promlib/client/transport.go index a333e04fafb..429befa1527 100644 --- a/pkg/tsdb/prometheus/client/transport.go +++ b/pkg/promlib/client/transport.go @@ -5,20 +5,17 @@ import ( "fmt" "strings" - "github.com/grafana/grafana-azure-sdk-go/azsettings" - "github.com/grafana/grafana-azure-sdk-go/util/maputil" "github.com/grafana/grafana-plugin-sdk-go/backend" sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + "github.com/grafana/grafana-plugin-sdk-go/data/utils/maputil" "github.com/grafana/grafana-plugin-sdk-go/backend/log" - "github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth" - "github.com/grafana/grafana/pkg/tsdb/prometheus/middleware" - "github.com/grafana/grafana/pkg/tsdb/prometheus/utils" + "github.com/grafana/grafana/pkg/promlib/middleware" + "github.com/grafana/grafana/pkg/promlib/utils" ) -// CreateTransportOptions creates options for the http client. Probably should be shared and should not live in the -// buffered package. +// CreateTransportOptions creates options for the http client. func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, logger log.Logger) (*sdkhttpclient.Options, error) { opts, err := settings.HTTPClientOptions(ctx) if err != nil { @@ -33,25 +30,6 @@ func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInst opts.Middlewares = middlewares(logger, httpMethod) - // Set SigV4 service namespace - if opts.SigV4 != nil { - opts.SigV4.Service = "aps" - } - - azureSettings, err := azsettings.ReadSettings(ctx) - if err != nil { - logger.Error("failed to read Azure settings from Grafana", "error", err.Error()) - return nil, fmt.Errorf("failed to read Azure settings from Grafana: %v", err) - } - - // Set Azure authentication - if azureSettings.AzureAuthEnabled { - err = azureauth.ConfigureAzureAuthentication(settings, azureSettings, &opts) - if err != nil { - return nil, fmt.Errorf("error configuring Azure auth: %v", err) - } - } - return &opts, nil } diff --git a/pkg/tsdb/prometheus/client/transport_test.go b/pkg/promlib/client/transport_test.go similarity index 50% rename from pkg/tsdb/prometheus/client/transport_test.go rename to pkg/promlib/client/transport_test.go index de7b165c6f6..61c481adca2 100644 --- a/pkg/tsdb/prometheus/client/transport_test.go +++ b/pkg/promlib/client/transport_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - "github.com/grafana/grafana-azure-sdk-go/azsettings" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/stretchr/testify/require" ) @@ -24,25 +23,4 @@ func TestCreateTransportOptions(t *testing.T) { require.Equal(t, map[string]string{"foo": "bar"}, opts.Headers) require.Equal(t, 2, len(opts.Middlewares)) }) - - t.Run("add azure credentials if configured", func(t *testing.T) { - cfg := backend.NewGrafanaCfg(map[string]string{ - azsettings.AzureCloud: azsettings.AzurePublic, - azsettings.AzureAuthEnabled: "true", - }) - settings := backend.DataSourceInstanceSettings{ - BasicAuthEnabled: false, - BasicAuthUser: "", - JSONData: []byte(`{ - "azureCredentials": { - "authType": "msi" - } - }`), - DecryptedSecureJSONData: map[string]string{}, - } - ctx := backend.WithGrafanaConfig(context.Background(), cfg) - opts, err := CreateTransportOptions(ctx, settings, backend.NewLoggerWith("logger", "test")) - require.NoError(t, err) - require.Equal(t, 3, len(opts.Middlewares)) - }) } diff --git a/pkg/tsdb/prometheus/converter/prom.go b/pkg/promlib/converter/prom.go similarity index 100% rename from pkg/tsdb/prometheus/converter/prom.go rename to pkg/promlib/converter/prom.go diff --git a/pkg/tsdb/prometheus/converter/prom_test.go b/pkg/promlib/converter/prom_test.go similarity index 100% rename from pkg/tsdb/prometheus/converter/prom_test.go rename to pkg/promlib/converter/prom_test.go diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-a-frame.jsonc b/pkg/promlib/converter/testdata/loki-streams-a-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-a-frame.jsonc rename to pkg/promlib/converter/testdata/loki-streams-a-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-a.json b/pkg/promlib/converter/testdata/loki-streams-a.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-a.json rename to pkg/promlib/converter/testdata/loki-streams-a.json diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-b-frame.jsonc b/pkg/promlib/converter/testdata/loki-streams-b-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-b-frame.jsonc rename to pkg/promlib/converter/testdata/loki-streams-b-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-b.json b/pkg/promlib/converter/testdata/loki-streams-b.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-b.json rename to pkg/promlib/converter/testdata/loki-streams-b.json diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-c-frame.jsonc b/pkg/promlib/converter/testdata/loki-streams-c-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-c-frame.jsonc rename to pkg/promlib/converter/testdata/loki-streams-c-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-c.json b/pkg/promlib/converter/testdata/loki-streams-c.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-c.json rename to pkg/promlib/converter/testdata/loki-streams-c.json diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-structured-metadata-frame.jsonc b/pkg/promlib/converter/testdata/loki-streams-structured-metadata-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-structured-metadata-frame.jsonc rename to pkg/promlib/converter/testdata/loki-streams-structured-metadata-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/loki-streams-structured-metadata.json b/pkg/promlib/converter/testdata/loki-streams-structured-metadata.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/loki-streams-structured-metadata.json rename to pkg/promlib/converter/testdata/loki-streams-structured-metadata.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-error-frame.jsonc b/pkg/promlib/converter/testdata/prom-error-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-error-frame.jsonc rename to pkg/promlib/converter/testdata/prom-error-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-error.json b/pkg/promlib/converter/testdata/prom-error.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-error.json rename to pkg/promlib/converter/testdata/prom-error.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a-frame.json b/pkg/promlib/converter/testdata/prom-exemplars-a-frame.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a-frame.json rename to pkg/promlib/converter/testdata/prom-exemplars-a-frame.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a-frame.jsonc b/pkg/promlib/converter/testdata/prom-exemplars-a-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a-frame.jsonc rename to pkg/promlib/converter/testdata/prom-exemplars-a-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a-golden.txt b/pkg/promlib/converter/testdata/prom-exemplars-a-golden.txt similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a-golden.txt rename to pkg/promlib/converter/testdata/prom-exemplars-a-golden.txt diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a.json b/pkg/promlib/converter/testdata/prom-exemplars-a.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-a.json rename to pkg/promlib/converter/testdata/prom-exemplars-a.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b-frame.json b/pkg/promlib/converter/testdata/prom-exemplars-b-frame.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b-frame.json rename to pkg/promlib/converter/testdata/prom-exemplars-b-frame.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b-frame.jsonc b/pkg/promlib/converter/testdata/prom-exemplars-b-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b-frame.jsonc rename to pkg/promlib/converter/testdata/prom-exemplars-b-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b-golden.txt b/pkg/promlib/converter/testdata/prom-exemplars-b-golden.txt similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b-golden.txt rename to pkg/promlib/converter/testdata/prom-exemplars-b-golden.txt diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b.json b/pkg/promlib/converter/testdata/prom-exemplars-b.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-b.json rename to pkg/promlib/converter/testdata/prom-exemplars-b.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-diff-labels-frame.jsonc b/pkg/promlib/converter/testdata/prom-exemplars-diff-labels-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-diff-labels-frame.jsonc rename to pkg/promlib/converter/testdata/prom-exemplars-diff-labels-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-diff-labels.json b/pkg/promlib/converter/testdata/prom-exemplars-diff-labels.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-diff-labels.json rename to pkg/promlib/converter/testdata/prom-exemplars-diff-labels.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars-frame.jsonc b/pkg/promlib/converter/testdata/prom-exemplars-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars-frame.jsonc rename to pkg/promlib/converter/testdata/prom-exemplars-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-exemplars.json b/pkg/promlib/converter/testdata/prom-exemplars.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-exemplars.json rename to pkg/promlib/converter/testdata/prom-exemplars.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-labels-frame.jsonc b/pkg/promlib/converter/testdata/prom-labels-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-labels-frame.jsonc rename to pkg/promlib/converter/testdata/prom-labels-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-labels.json b/pkg/promlib/converter/testdata/prom-labels.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-labels.json rename to pkg/promlib/converter/testdata/prom-labels.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix-frame.jsonc b/pkg/promlib/converter/testdata/prom-matrix-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix-frame.jsonc rename to pkg/promlib/converter/testdata/prom-matrix-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-no-labels-frame.jsonc b/pkg/promlib/converter/testdata/prom-matrix-histogram-no-labels-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-no-labels-frame.jsonc rename to pkg/promlib/converter/testdata/prom-matrix-histogram-no-labels-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-no-labels.json b/pkg/promlib/converter/testdata/prom-matrix-histogram-no-labels.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-no-labels.json rename to pkg/promlib/converter/testdata/prom-matrix-histogram-no-labels.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-partitioned-frame.jsonc b/pkg/promlib/converter/testdata/prom-matrix-histogram-partitioned-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-partitioned-frame.jsonc rename to pkg/promlib/converter/testdata/prom-matrix-histogram-partitioned-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-partitioned.json b/pkg/promlib/converter/testdata/prom-matrix-histogram-partitioned.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix-histogram-partitioned.json rename to pkg/promlib/converter/testdata/prom-matrix-histogram-partitioned.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix-with-nans-frame.jsonc b/pkg/promlib/converter/testdata/prom-matrix-with-nans-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix-with-nans-frame.jsonc rename to pkg/promlib/converter/testdata/prom-matrix-with-nans-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix-with-nans.json b/pkg/promlib/converter/testdata/prom-matrix-with-nans.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix-with-nans.json rename to pkg/promlib/converter/testdata/prom-matrix-with-nans.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-matrix.json b/pkg/promlib/converter/testdata/prom-matrix.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-matrix.json rename to pkg/promlib/converter/testdata/prom-matrix.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-scalar-frame.jsonc b/pkg/promlib/converter/testdata/prom-scalar-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-scalar-frame.jsonc rename to pkg/promlib/converter/testdata/prom-scalar-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-scalar.json b/pkg/promlib/converter/testdata/prom-scalar.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-scalar.json rename to pkg/promlib/converter/testdata/prom-scalar.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-series-frame.jsonc b/pkg/promlib/converter/testdata/prom-series-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-series-frame.jsonc rename to pkg/promlib/converter/testdata/prom-series-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-series.json b/pkg/promlib/converter/testdata/prom-series.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-series.json rename to pkg/promlib/converter/testdata/prom-series.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-string-frame.jsonc b/pkg/promlib/converter/testdata/prom-string-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-string-frame.jsonc rename to pkg/promlib/converter/testdata/prom-string-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-string.json b/pkg/promlib/converter/testdata/prom-string.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-string.json rename to pkg/promlib/converter/testdata/prom-string.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-vector-frame.jsonc b/pkg/promlib/converter/testdata/prom-vector-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-vector-frame.jsonc rename to pkg/promlib/converter/testdata/prom-vector-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-vector-histogram-no-labels-frame.jsonc b/pkg/promlib/converter/testdata/prom-vector-histogram-no-labels-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-vector-histogram-no-labels-frame.jsonc rename to pkg/promlib/converter/testdata/prom-vector-histogram-no-labels-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-vector-histogram-no-labels.json b/pkg/promlib/converter/testdata/prom-vector-histogram-no-labels.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-vector-histogram-no-labels.json rename to pkg/promlib/converter/testdata/prom-vector-histogram-no-labels.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-vector.json b/pkg/promlib/converter/testdata/prom-vector.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-vector.json rename to pkg/promlib/converter/testdata/prom-vector.json diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-warnings-frame.jsonc b/pkg/promlib/converter/testdata/prom-warnings-frame.jsonc similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-warnings-frame.jsonc rename to pkg/promlib/converter/testdata/prom-warnings-frame.jsonc diff --git a/pkg/tsdb/prometheus/converter/testdata/prom-warnings.json b/pkg/promlib/converter/testdata/prom-warnings.json similarity index 100% rename from pkg/tsdb/prometheus/converter/testdata/prom-warnings.json rename to pkg/promlib/converter/testdata/prom-warnings.json diff --git a/pkg/promlib/go.mod b/pkg/promlib/go.mod new file mode 100644 index 00000000000..b6a16bfab7a --- /dev/null +++ b/pkg/promlib/go.mod @@ -0,0 +1,109 @@ +module github.com/grafana/grafana/pkg/promlib + +go 1.21.0 + +require ( + github.com/grafana/grafana-plugin-sdk-go v0.214.0 + github.com/json-iterator/go v1.1.12 + github.com/patrickmn/go-cache v2.1.0+incompatible + github.com/prometheus/client_golang v1.18.0 + github.com/prometheus/common v0.46.0 + github.com/prometheus/prometheus v1.8.2-0.20221021121301-51a44e6657c3 + github.com/stretchr/testify v1.8.4 + go.opentelemetry.io/otel v1.24.0 + go.opentelemetry.io/otel/trace v1.24.0 + golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb +) + +require ( + github.com/BurntSushi/toml v1.3.2 // indirect + github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect + github.com/apache/arrow/go/v15 v15.0.0 // indirect + github.com/aws/aws-sdk-go v1.50.8 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cenkalti/backoff/v4 v4.2.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/cheekybits/genny v1.0.0 // indirect + github.com/chromedp/cdproto v0.0.0-20230802225258-3cf4e6d46a89 // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/dennwc/varint v1.0.0 // indirect + github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 // indirect + github.com/fatih/color v1.15.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/getkin/kin-openapi v0.120.0 // indirect + github.com/go-kit/log v0.2.1 // indirect + github.com/go-logfmt/logfmt v0.6.0 // indirect + github.com/go-logr/logr v1.4.1 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-openapi/jsonpointer v0.20.2 // indirect + github.com/go-openapi/swag v0.22.9 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/google/flatbuffers v23.5.26+incompatible // indirect + github.com/google/go-cmp v0.6.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/gorilla/mux v1.8.0 // indirect + github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db // indirect + github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect + github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20191002090509-6af20e3a5340 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect + github.com/hashicorp/go-hclog v1.6.2 // indirect + github.com/hashicorp/go-plugin v1.6.0 // indirect + github.com/hashicorp/yamux v0.1.1 // indirect + github.com/invopop/yaml v0.2.0 // indirect + github.com/josharian/intern v1.0.0 // indirect + github.com/klauspost/compress v1.17.4 // indirect + github.com/klauspost/cpuid/v2 v2.2.5 // indirect + github.com/magefile/mage v1.15.0 // indirect + github.com/mailru/easyjson v0.7.7 // indirect + github.com/mattetti/filebuffer v1.0.1 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-runewidth v0.0.13 // indirect + github.com/mitchellh/go-testing-interface v1.14.1 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect + github.com/oklog/run v1.1.0 // indirect + github.com/olekukonko/tablewriter v0.0.5 // indirect + github.com/perimeterx/marshmallow v1.1.5 // indirect + github.com/pierrec/lz4/v4 v4.1.18 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/prometheus/client_model v0.5.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect + github.com/rivo/uniseg v0.3.4 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/smartystreets/goconvey v1.6.4 // indirect + github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8 // indirect + github.com/unknwon/com v1.0.1 // indirect + github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 // indirect + github.com/urfave/cli v1.22.14 // indirect + github.com/zeebo/xxh3 v1.0.2 // indirect + go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect + go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.49.0 // indirect + go.opentelemetry.io/contrib/propagators/jaeger v1.22.0 // indirect + go.opentelemetry.io/contrib/samplers/jaegerremote v0.18.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 // indirect + go.opentelemetry.io/otel/metric v1.24.0 // indirect + go.opentelemetry.io/otel/sdk v1.24.0 // indirect + go.opentelemetry.io/proto/otlp v1.1.0 // indirect + go.uber.org/atomic v1.11.0 // indirect + go.uber.org/goleak v1.3.0 // indirect + golang.org/x/mod v0.14.0 // indirect + golang.org/x/net v0.21.0 // indirect + golang.org/x/sys v0.17.0 // indirect + golang.org/x/text v0.14.0 // indirect + golang.org/x/tools v0.17.0 // indirect + golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect + google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect + google.golang.org/grpc v1.62.1 // indirect + google.golang.org/protobuf v1.32.0 // indirect + gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/pkg/promlib/go.sum b/pkg/promlib/go.sum new file mode 100644 index 00000000000..8f3fa371931 --- /dev/null +++ b/pkg/promlib/go.sum @@ -0,0 +1,129 @@ +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs= +github.com/apache/arrow/go/v15 v15.0.0 h1:1zZACWf85oEZY5/kd9dsQS7i+2G5zVQcbKTHgslqHNA= +github.com/aws/aws-sdk-go v1.50.8 h1:gY0WoOW+/Wz6XmYSgDH9ge3wnAevYDSQWPxxJvqAkP4= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= +github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE= +github.com/chromedp/cdproto v0.0.0-20230802225258-3cf4e6d46a89 h1:aPflPkRFkVwbW6dmcVqfgwp1i+UWGFH6VgR1Jim5Ygc= +github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/dennwc/varint v1.0.0 h1:kGNFFSSw8ToIy3obO/kKr8U9GZYUAxQEVuix4zfDWzE= +github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 h1:1L0aalTpPz7YlMxETKpmQoWMBkeiuorElZIXoNmgiPE= +github.com/elazarl/goproxy/ext v0.0.0-20220115173737-adb46da277ac h1:9yrT5tmn9Zc0ytWPASlaPwQfQMQYnRf0RSDe1XvHw0Q= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/getkin/kin-openapi v0.120.0 h1:MqJcNJFrMDFNc07iwE8iFC5eT2k/NPUFDIpNeiZv8Jg= +github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-openapi/jsonpointer v0.20.2 h1:mQc3nmndL8ZBzStEo3JYF8wzmeWffDH4VbXz58sAx6Q= +github.com/go-openapi/swag v0.22.9 h1:XX2DssF+mQKM2DHsbgZK74y/zj4mo9I99+89xUmuZCE= +github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= +github.com/google/flatbuffers v23.5.26+incompatible h1:M9dgRyhJemaM4Sw8+66GHBu8ioaQmyPLg1b8VwK5WJg= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1ks85zJ1lfDGgIiMDuIptTOhJq+zKyg= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/grafana/grafana-plugin-sdk-go v0.214.0 h1:09AoomxfsMdKmS4bc5tF81f7fvI9HjHckGFAmu/UQls= +github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db h1:7aN5cccjIqCLTzedH7MZzRZt5/lsAHch6Z3L2ZGn5FA= +github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20191002090509-6af20e3a5340 h1:uGoIog/wiQHI9GAxXO5TJbT0wWKH3O9HhOJW1F9c3fY= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 h1:Wqo399gCIufwto+VfwCSvsnfGpF/w5E9CNxSwbpD6No= +github.com/hashicorp/go-hclog v1.6.2 h1:NOtoftovWkDheyUM/8JW3QMiXyxJK3uHRK7wV04nD2I= +github.com/hashicorp/go-hclog v1.6.2/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A= +github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= +github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY= +github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= +github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mattetti/filebuffer v1.0.1 h1:gG7pyfnSIZCxdoKq+cPa8T0hhYtD9NxCdI4D7PTjRLM= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU= +github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= +github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= +github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4= +github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s= +github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/prometheus/client_golang v1.18.0 h1:HzFfmkOzH5Q8L8G+kSJKUx5dtG87sewO+FoDDqP5Tbk= +github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= +github.com/prometheus/common v0.46.0 h1:doXzt5ybi1HBKpsZOL0sSkaNHJJqkyfEWZGGqqScV0Y= +github.com/prometheus/common/sigv4 v0.1.0 h1:qoVebwtwwEhS85Czm2dSROY5fTo2PAPEVdDeppTwGX4= +github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= +github.com/prometheus/prometheus v1.8.2-0.20221021121301-51a44e6657c3 h1:etRZv4bJf9YAuyPWbyFufjkijfeoPSmyA5xNcd4DoyI= +github.com/prometheus/prometheus v1.8.2-0.20221021121301-51a44e6657c3/go.mod h1:plwr4+63Q1xL8oIdBDeU854um7Cct0Av8dhP44lutMw= +github.com/rivo/uniseg v0.3.4 h1:3Z3Eu6FGHZWSfNKJTOUiPatWwfc7DzJRU04jFUqJODw= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/smartystreets/assertions v0.0.0-20190116191733-b6c0e53d7304 h1:Jpy1PXuP99tXNrhbq2BaPz9B+jNAvH1JPQQpG/9GCXY= +github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= +github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8 h1:aVGB3YnaS/JNfOW3tiHIlmNmTDg618va+eT0mVomgyI= +github.com/unknwon/com v1.0.1 h1:3d1LTxD+Lnf3soQiD4Cp/0BRB+Rsa/+RTvz8GMMzIXs= +github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 h1:4EYQaWAatQokdji3zqZloVIW/Ke1RQjYw2zHULyrHJg= +github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk= +github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= +github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0= +go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 h1:4Pp6oUg3+e/6M4C0A/3kJ2VYa++dsWVTtGgLVj5xtHg= +go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.49.0 h1:RtcvQ4iw3w9NBB5yRwgA4sSa82rfId7n4atVpvKx3bY= +go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 h1:jq9TW8u3so/bN+JPT166wjOI6/vQPF6Xe7nMNIltagk= +go.opentelemetry.io/contrib/propagators/jaeger v1.22.0 h1:bAHX+zN/inu+Rbqk51REmC8oXLl+Dw6pp9ldQf/onaY= +go.opentelemetry.io/contrib/samplers/jaegerremote v0.18.0 h1:Q9PrD94WoMolBx44ef5UWWvufpVSME0MiSymXZfedso= +go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 h1:t6wl9SPayj+c7lEIFgm4ooDBZVb01IhLB4InpomhRw8= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.24.0 h1:Mw5xcxMwlqoJd97vwPxA8isEaIoxsta9/Q51+TTJLGE= +go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI= +go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw= +go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI= +go.opentelemetry.io/proto/otlp v1.1.0 h1:2Di21piLrCqJ3U3eXGCTPHE9R8Nh+0uglSnOyxikMeI= +go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc= +golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU= +gonum.org/v1/gonum v0.12.0 h1:xKuo6hzt+gMav00meVPUlXwSdoEJP46BR+wdxQEFK2o= +google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM= +google.golang.org/genproto v0.0.0-20240123012728-ef4313101c80 h1:KAeGQVN3M9nD0/bQXnr/ClcEMJ968gUXJQ9pwfSynuQ= +google.golang.org/genproto/googleapis/api v0.0.0-20240123012728-ef4313101c80 h1:Lj5rbfG876hIAYFjqiJnPHfhXbv+nzTWfm04Fg/XSVU= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 h1:AjyfHzEPEFp/NpvfN5g+KDla3EMojjhRVZc1i7cj+oM= +google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/fsnotify/fsnotify.v1 v1.4.7 h1:XNNYLJHt73EyYiCZi6+xjupS9CpvmiDgjPTAjrBlQbo= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/pkg/tsdb/prometheus/healthcheck.go b/pkg/promlib/healthcheck.go similarity index 92% rename from pkg/tsdb/prometheus/healthcheck.go rename to pkg/promlib/healthcheck.go index 1f5e6324c09..c2e2de4224c 100644 --- a/pkg/tsdb/prometheus/healthcheck.go +++ b/pkg/promlib/healthcheck.go @@ -1,4 +1,4 @@ -package prometheus +package promlib import ( "context" @@ -8,16 +8,15 @@ import ( "time" "github.com/grafana/grafana-plugin-sdk-go/backend" - "github.com/grafana/grafana-plugin-sdk-go/backend/log" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/models" ) const ( refID = "__healthcheck__" ) -var logger log.Logger = backend.NewLoggerWith("logger", "tsdb.prometheus") +var logger = backend.NewLoggerWith("logger", "tsdb.prometheus") func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { diff --git a/pkg/tsdb/prometheus/healthcheck_test.go b/pkg/promlib/healthcheck_test.go similarity index 78% rename from pkg/tsdb/prometheus/healthcheck_test.go rename to pkg/promlib/healthcheck_test.go index b50c6c649f7..20e06edd6b7 100644 --- a/pkg/tsdb/prometheus/healthcheck_test.go +++ b/pkg/promlib/healthcheck_test.go @@ -1,4 +1,4 @@ -package prometheus +package promlib import ( "context" @@ -10,17 +10,18 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" - "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/stretchr/testify/assert" ) type healthCheckProvider[T http.RoundTripper] struct { - httpclient.Provider + sdkhttpclient.Provider RoundTripper *T } type healthCheckSuccessRoundTripper struct { } + type healthCheckFailRoundTripper struct { } @@ -55,34 +56,34 @@ func (rt *healthCheckFailRoundTripper) RoundTrip(req *http.Request) (*http.Respo }, nil } -func (provider *healthCheckProvider[T]) New(opts ...httpclient.Options) (*http.Client, error) { +func (provider *healthCheckProvider[T]) New(opts ...sdkhttpclient.Options) (*http.Client, error) { client := &http.Client{} provider.RoundTripper = new(T) client.Transport = *provider.RoundTripper return client, nil } -func (provider *healthCheckProvider[T]) GetTransport(opts ...httpclient.Options) (http.RoundTripper, error) { +func (provider *healthCheckProvider[T]) GetTransport(opts ...sdkhttpclient.Options) (http.RoundTripper, error) { return *new(T), nil } -func getMockProvider[T http.RoundTripper]() *httpclient.Provider { +func getMockProvider[T http.RoundTripper]() *sdkhttpclient.Provider { p := &healthCheckProvider[T]{ RoundTripper: new(T), } - anotherFN := func(o httpclient.Options, next http.RoundTripper) http.RoundTripper { + anotherFN := func(o sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper { return *p.RoundTripper } - fn := httpclient.MiddlewareFunc(anotherFN) - mid := httpclient.NamedMiddlewareFunc("mock", fn) - return httpclient.NewProvider(httpclient.ProviderOptions{Middlewares: []httpclient.Middleware{mid}}) + fn := sdkhttpclient.MiddlewareFunc(anotherFN) + mid := sdkhttpclient.NamedMiddlewareFunc("mock", fn) + return sdkhttpclient.NewProvider(sdkhttpclient.ProviderOptions{Middlewares: []sdkhttpclient.Middleware{mid}}) } func Test_healthcheck(t *testing.T) { t.Run("should do a successful health check", func(t *testing.T) { httpProvider := getMockProvider[*healthCheckSuccessRoundTripper]() s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"), mockExtendClientOpts)), } req := &backend.CheckHealthRequest{ @@ -98,7 +99,7 @@ func Test_healthcheck(t *testing.T) { t.Run("should return an error for an unsuccessful health check", func(t *testing.T) { httpProvider := getMockProvider[*healthCheckFailRoundTripper]() s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"), mockExtendClientOpts)), } req := &backend.CheckHealthRequest{ diff --git a/pkg/tsdb/prometheus/heuristics.go b/pkg/promlib/heuristics.go similarity index 99% rename from pkg/tsdb/prometheus/heuristics.go rename to pkg/promlib/heuristics.go index 78e38c027ee..c0c3f1e318f 100644 --- a/pkg/tsdb/prometheus/heuristics.go +++ b/pkg/promlib/heuristics.go @@ -1,4 +1,4 @@ -package prometheus +package promlib import ( "context" diff --git a/pkg/tsdb/prometheus/heuristics_test.go b/pkg/promlib/heuristics_test.go similarity index 74% rename from pkg/tsdb/prometheus/heuristics_test.go rename to pkg/promlib/heuristics_test.go index 58a9befc683..3ed20a65553 100644 --- a/pkg/tsdb/prometheus/heuristics_test.go +++ b/pkg/promlib/heuristics_test.go @@ -1,4 +1,4 @@ -package prometheus +package promlib import ( "context" @@ -13,7 +13,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" - "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" ) type heuristicsSuccessRoundTripper struct { @@ -32,13 +32,17 @@ func (rt *heuristicsSuccessRoundTripper) RoundTrip(req *http.Request) (*http.Res }, nil } -func newHeuristicsSDKProvider(hrt heuristicsSuccessRoundTripper) *httpclient.Provider { - anotherFN := func(o httpclient.Options, next http.RoundTripper) http.RoundTripper { +func newHeuristicsSDKProvider(hrt heuristicsSuccessRoundTripper) *sdkhttpclient.Provider { + anotherFN := func(o sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper { return &hrt } - fn := httpclient.MiddlewareFunc(anotherFN) - mid := httpclient.NamedMiddlewareFunc("mock", fn) - return httpclient.NewProvider(httpclient.ProviderOptions{Middlewares: []httpclient.Middleware{mid}}) + fn := sdkhttpclient.MiddlewareFunc(anotherFN) + mid := sdkhttpclient.NamedMiddlewareFunc("mock", fn) + return sdkhttpclient.NewProvider(sdkhttpclient.ProviderOptions{Middlewares: []sdkhttpclient.Middleware{mid}}) +} + +func mockExtendClientOpts(ctx context.Context, settings backend.DataSourceInstanceSettings, clientOpts *sdkhttpclient.Options) error { + return nil } func Test_GetHeuristics(t *testing.T) { @@ -47,10 +51,9 @@ func Test_GetHeuristics(t *testing.T) { res: io.NopCloser(strings.NewReader("{\"status\":\"success\",\"data\":{\"version\":\"1.0\"}}")), status: http.StatusOK, } - //httpProvider := getHeuristicsMockProvider(&rt) httpProvider := newHeuristicsSDKProvider(rt) s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"), mockExtendClientOpts)), } req := HeuristicsRequest{ @@ -70,7 +73,7 @@ func Test_GetHeuristics(t *testing.T) { } httpProvider := newHeuristicsSDKProvider(rt) s := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), + im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"), mockExtendClientOpts)), } req := HeuristicsRequest{ diff --git a/pkg/tsdb/prometheus/instrumentation/instrumentation.go b/pkg/promlib/instrumentation/instrumentation.go similarity index 100% rename from pkg/tsdb/prometheus/instrumentation/instrumentation.go rename to pkg/promlib/instrumentation/instrumentation.go diff --git a/pkg/tsdb/prometheus/instrumentation/instrumentation_test.go b/pkg/promlib/instrumentation/instrumentation_test.go similarity index 100% rename from pkg/tsdb/prometheus/instrumentation/instrumentation_test.go rename to pkg/promlib/instrumentation/instrumentation_test.go diff --git a/pkg/tsdb/prometheus/intervalv2/intervalv2.go b/pkg/promlib/intervalv2/intervalv2.go similarity index 100% rename from pkg/tsdb/prometheus/intervalv2/intervalv2.go rename to pkg/promlib/intervalv2/intervalv2.go diff --git a/pkg/tsdb/prometheus/intervalv2/intervalv2_test.go b/pkg/promlib/intervalv2/intervalv2_test.go similarity index 100% rename from pkg/tsdb/prometheus/intervalv2/intervalv2_test.go rename to pkg/promlib/intervalv2/intervalv2_test.go diff --git a/pkg/promlib/library.go b/pkg/promlib/library.go new file mode 100644 index 00000000000..c8316dbe750 --- /dev/null +++ b/pkg/promlib/library.go @@ -0,0 +1,154 @@ +package promlib + +import ( + "context" + "errors" + "fmt" + "strings" + "time" + + "github.com/grafana/grafana-plugin-sdk-go/backend" + "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" + sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" + "github.com/grafana/grafana-plugin-sdk-go/backend/log" + "github.com/patrickmn/go-cache" + apiv1 "github.com/prometheus/client_golang/api/prometheus/v1" + + "github.com/grafana/grafana/pkg/promlib/client" + "github.com/grafana/grafana/pkg/promlib/instrumentation" + "github.com/grafana/grafana/pkg/promlib/querydata" + "github.com/grafana/grafana/pkg/promlib/resource" +) + +type Service struct { + im instancemgmt.InstanceManager + logger log.Logger +} + +type instance struct { + queryData *querydata.QueryData + resource *resource.Resource + versionCache *cache.Cache +} + +type ExtendOptions func(ctx context.Context, settings backend.DataSourceInstanceSettings, clientOpts *sdkhttpclient.Options) error + +func NewService(httpClientProvider *sdkhttpclient.Provider, plog log.Logger, extendOptions ExtendOptions) *Service { + if httpClientProvider == nil { + httpClientProvider = sdkhttpclient.NewProvider() + } + return &Service{ + im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider, plog, extendOptions)), + logger: plog, + } +} + +func newInstanceSettings(httpClientProvider *sdkhttpclient.Provider, log log.Logger, extendOptions ExtendOptions) datasource.InstanceFactoryFunc { + return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { + // Creates a http roundTripper. + opts, err := client.CreateTransportOptions(ctx, settings, log) + if err != nil { + return nil, fmt.Errorf("error creating transport options: %v", err) + } + + err = extendOptions(ctx, settings, opts) + if err != nil { + return nil, fmt.Errorf("error extending transport options: %v", err) + } + + httpClient, err := httpClientProvider.New(*opts) + if err != nil { + return nil, fmt.Errorf("error creating http client: %v", err) + } + + // New version using custom client and better response parsing + qd, err := querydata.New(httpClient, settings, log) + if err != nil { + return nil, err + } + + // Resource call management using new custom client same as querydata + r, err := resource.New(httpClient, settings, log) + if err != nil { + return nil, err + } + + return instance{ + queryData: qd, + resource: r, + versionCache: cache.New(time.Minute*1, time.Minute*5), + }, nil + } +} + +func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { + if len(req.Queries) == 0 { + err := fmt.Errorf("query contains no queries") + instrumentation.UpdateQueryDataMetrics(err, nil) + return &backend.QueryDataResponse{}, err + } + + i, err := s.getInstance(ctx, req.PluginContext) + if err != nil { + instrumentation.UpdateQueryDataMetrics(err, nil) + return nil, err + } + + qd, err := i.queryData.Execute(ctx, req) + instrumentation.UpdateQueryDataMetrics(err, qd) + + return qd, err +} + +func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { + i, err := s.getInstance(ctx, req.PluginContext) + if err != nil { + return err + } + + if strings.EqualFold(req.Path, "version-detect") { + versionObj, found := i.versionCache.Get("version") + if found { + return sender.Send(versionObj.(*backend.CallResourceResponse)) + } + + vResp, err := i.resource.DetectVersion(ctx, req) + if err != nil { + return err + } + i.versionCache.Set("version", vResp, cache.DefaultExpiration) + return sender.Send(vResp) + } + + resp, err := i.resource.Execute(ctx, req) + if err != nil { + return err + } + + return sender.Send(resp) +} + +func (s *Service) getInstance(ctx context.Context, pluginCtx backend.PluginContext) (*instance, error) { + i, err := s.im.Get(ctx, pluginCtx) + if err != nil { + return nil, err + } + in := i.(instance) + return &in, nil +} + +// IsAPIError returns whether err is or wraps a Prometheus error. +func IsAPIError(err error) bool { + // Check if the right error type is in err's chain. + var e *apiv1.Error + return errors.As(err, &e) +} + +func ConvertAPIError(err error) error { + var e *apiv1.Error + if errors.As(err, &e) { + return fmt.Errorf("%s: %s", e.Msg, e.Detail) + } + return err +} diff --git a/pkg/promlib/library_test.go b/pkg/promlib/library_test.go new file mode 100644 index 00000000000..cf789c43f60 --- /dev/null +++ b/pkg/promlib/library_test.go @@ -0,0 +1,118 @@ +package promlib + +import ( + "context" + "io" + "net/http" + "testing" + "time" + + "github.com/grafana/grafana-plugin-sdk-go/backend" + sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + "github.com/stretchr/testify/require" +) + +type fakeSender struct{} + +func (sender *fakeSender) Send(resp *backend.CallResourceResponse) error { + return nil +} + +type fakeRoundtripper struct { + Req *http.Request +} + +func (rt *fakeRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) { + rt.Req = req + return &http.Response{ + Status: "200", + StatusCode: 200, + Header: nil, + Body: nil, + ContentLength: 0, + }, nil +} + +type fakeHTTPClientProvider struct { + sdkhttpclient.Provider + Roundtripper *fakeRoundtripper +} + +func (provider *fakeHTTPClientProvider) New(opts ...sdkhttpclient.Options) (*http.Client, error) { + client := &http.Client{} + provider.Roundtripper = &fakeRoundtripper{} + client.Transport = provider.Roundtripper + return client, nil +} + +func (provider *fakeHTTPClientProvider) GetTransport(opts ...sdkhttpclient.Options) (http.RoundTripper, error) { + return &fakeRoundtripper{}, nil +} + +func getMockPromTestSDKProvider(f *fakeHTTPClientProvider) *sdkhttpclient.Provider { + anotherFN := func(o sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper { + _, _ = f.New() + return f.Roundtripper + } + fn := sdkhttpclient.MiddlewareFunc(anotherFN) + mid := sdkhttpclient.NamedMiddlewareFunc("mock", fn) + return sdkhttpclient.NewProvider(sdkhttpclient.ProviderOptions{Middlewares: []sdkhttpclient.Middleware{mid}}) +} + +func mockExtendTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, clientOpts *sdkhttpclient.Options) error { + return nil +} + +func TestService(t *testing.T) { + t.Run("Service", func(t *testing.T) { + t.Run("CallResource", func(t *testing.T) { + t.Run("creates correct request", func(t *testing.T) { + f := &fakeHTTPClientProvider{} + httpProvider := getMockPromTestSDKProvider(f) + service := NewService(httpProvider, backend.NewLoggerWith("logger", "test"), mockExtendTransportOptions) + + req := &backend.CallResourceRequest{ + PluginContext: backend.PluginContext{ + OrgID: 0, + PluginID: "prometheus", + User: nil, + AppInstanceSettings: nil, + DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ + ID: 0, + UID: "", + Type: "prometheus", + Name: "test-prom", + URL: "http://localhost:9090", + User: "", + Database: "", + BasicAuthEnabled: true, + BasicAuthUser: "admin", + Updated: time.Time{}, + JSONData: []byte("{}"), + }, + }, + Path: "/api/v1/series", + Method: http.MethodPost, + URL: "/api/v1/series", + Body: []byte("match%5B%5D: ALERTS\nstart: 1655271408\nend: 1655293008"), + } + + sender := &fakeSender{} + err := service.CallResource(context.Background(), req, sender) + require.NoError(t, err) + require.Equal( + t, + http.Header{ + "Content-Type": {"application/x-www-form-urlencoded"}, + "Idempotency-Key": []string(nil), + }, + f.Roundtripper.Req.Header) + require.Equal(t, http.MethodPost, f.Roundtripper.Req.Method) + body, err := io.ReadAll(f.Roundtripper.Req.Body) + require.NoError(t, err) + require.Equal(t, []byte("match%5B%5D: ALERTS\nstart: 1655271408\nend: 1655293008"), body) + require.Equal(t, "http://localhost:9090/api/v1/series", f.Roundtripper.Req.URL.String()) + }) + }) + }) +} diff --git a/pkg/tsdb/prometheus/middleware/custom_query_params.go b/pkg/promlib/middleware/custom_query_params.go similarity index 100% rename from pkg/tsdb/prometheus/middleware/custom_query_params.go rename to pkg/promlib/middleware/custom_query_params.go diff --git a/pkg/tsdb/prometheus/middleware/custom_query_params_test.go b/pkg/promlib/middleware/custom_query_params_test.go similarity index 100% rename from pkg/tsdb/prometheus/middleware/custom_query_params_test.go rename to pkg/promlib/middleware/custom_query_params_test.go diff --git a/pkg/tsdb/prometheus/middleware/force_http_get.go b/pkg/promlib/middleware/force_http_get.go similarity index 100% rename from pkg/tsdb/prometheus/middleware/force_http_get.go rename to pkg/promlib/middleware/force_http_get.go diff --git a/pkg/tsdb/prometheus/middleware/force_http_get_test.go b/pkg/promlib/middleware/force_http_get_test.go similarity index 100% rename from pkg/tsdb/prometheus/middleware/force_http_get_test.go rename to pkg/promlib/middleware/force_http_get_test.go diff --git a/pkg/tsdb/prometheus/models/query.go b/pkg/promlib/models/query.go similarity index 99% rename from pkg/tsdb/prometheus/models/query.go rename to pkg/promlib/models/query.go index b510b769e01..3c9a96bab91 100644 --- a/pkg/tsdb/prometheus/models/query.go +++ b/pkg/promlib/models/query.go @@ -13,7 +13,7 @@ import ( "github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/promql/parser" - "github.com/grafana/grafana/pkg/tsdb/prometheus/intervalv2" + "github.com/grafana/grafana/pkg/promlib/intervalv2" ) // PromQueryFormat defines model for PromQueryFormat. diff --git a/pkg/tsdb/prometheus/models/query_test.go b/pkg/promlib/models/query_test.go similarity index 99% rename from pkg/tsdb/prometheus/models/query_test.go rename to pkg/promlib/models/query_test.go index 2a9ffab1077..085352f3f1a 100644 --- a/pkg/tsdb/prometheus/models/query_test.go +++ b/pkg/promlib/models/query_test.go @@ -9,8 +9,8 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/stretchr/testify/require" - "github.com/grafana/grafana/pkg/tsdb/prometheus/intervalv2" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/intervalv2" + "github.com/grafana/grafana/pkg/promlib/models" ) var ( diff --git a/pkg/tsdb/prometheus/models/result.go b/pkg/promlib/models/result.go similarity index 100% rename from pkg/tsdb/prometheus/models/result.go rename to pkg/promlib/models/result.go diff --git a/pkg/tsdb/prometheus/models/scope.go b/pkg/promlib/models/scope.go similarity index 100% rename from pkg/tsdb/prometheus/models/scope.go rename to pkg/promlib/models/scope.go diff --git a/pkg/tsdb/prometheus/querydata/exemplar/framer.go b/pkg/promlib/querydata/exemplar/framer.go similarity index 100% rename from pkg/tsdb/prometheus/querydata/exemplar/framer.go rename to pkg/promlib/querydata/exemplar/framer.go diff --git a/pkg/tsdb/prometheus/querydata/exemplar/labels.go b/pkg/promlib/querydata/exemplar/labels.go similarity index 100% rename from pkg/tsdb/prometheus/querydata/exemplar/labels.go rename to pkg/promlib/querydata/exemplar/labels.go diff --git a/pkg/tsdb/prometheus/querydata/exemplar/sampler.go b/pkg/promlib/querydata/exemplar/sampler.go similarity index 93% rename from pkg/tsdb/prometheus/querydata/exemplar/sampler.go rename to pkg/promlib/querydata/exemplar/sampler.go index be7f518cb89..f0b3e0eed3a 100644 --- a/pkg/tsdb/prometheus/querydata/exemplar/sampler.go +++ b/pkg/promlib/querydata/exemplar/sampler.go @@ -4,7 +4,7 @@ import ( "sort" "time" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/models" ) type Sampler interface { diff --git a/pkg/tsdb/prometheus/querydata/exemplar/sampler_stddev.go b/pkg/promlib/querydata/exemplar/sampler_stddev.go similarity index 97% rename from pkg/tsdb/prometheus/querydata/exemplar/sampler_stddev.go rename to pkg/promlib/querydata/exemplar/sampler_stddev.go index 9028e22cff5..92ebf0ef510 100644 --- a/pkg/tsdb/prometheus/querydata/exemplar/sampler_stddev.go +++ b/pkg/promlib/querydata/exemplar/sampler_stddev.go @@ -5,7 +5,7 @@ import ( "sort" "time" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/models" ) type StandardDeviationSampler struct { diff --git a/pkg/tsdb/prometheus/querydata/exemplar/sampler_stddev_test.go b/pkg/promlib/querydata/exemplar/sampler_stddev_test.go similarity index 84% rename from pkg/tsdb/prometheus/querydata/exemplar/sampler_stddev_test.go rename to pkg/promlib/querydata/exemplar/sampler_stddev_test.go index b96f6d63b28..c7a4ff0aa7a 100644 --- a/pkg/tsdb/prometheus/querydata/exemplar/sampler_stddev_test.go +++ b/pkg/promlib/querydata/exemplar/sampler_stddev_test.go @@ -6,8 +6,8 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/experimental" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" - "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata/exemplar" + "github.com/grafana/grafana/pkg/promlib/models" + "github.com/grafana/grafana/pkg/promlib/querydata/exemplar" ) func TestStdDevSampler(t *testing.T) { diff --git a/pkg/tsdb/prometheus/querydata/exemplar/sampler_test.go b/pkg/promlib/querydata/exemplar/sampler_test.go similarity index 88% rename from pkg/tsdb/prometheus/querydata/exemplar/sampler_test.go rename to pkg/promlib/querydata/exemplar/sampler_test.go index f071f1b9025..26cdc5c2a8d 100644 --- a/pkg/tsdb/prometheus/querydata/exemplar/sampler_test.go +++ b/pkg/promlib/querydata/exemplar/sampler_test.go @@ -6,8 +6,8 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/experimental" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" - "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata/exemplar" + "github.com/grafana/grafana/pkg/promlib/models" + "github.com/grafana/grafana/pkg/promlib/querydata/exemplar" ) const update = true diff --git a/pkg/tsdb/prometheus/querydata/exemplar/testdata/noop_sampler.jsonc b/pkg/promlib/querydata/exemplar/testdata/noop_sampler.jsonc similarity index 100% rename from pkg/tsdb/prometheus/querydata/exemplar/testdata/noop_sampler.jsonc rename to pkg/promlib/querydata/exemplar/testdata/noop_sampler.jsonc diff --git a/pkg/tsdb/prometheus/querydata/exemplar/testdata/stddev_sampler.jsonc b/pkg/promlib/querydata/exemplar/testdata/stddev_sampler.jsonc similarity index 100% rename from pkg/tsdb/prometheus/querydata/exemplar/testdata/stddev_sampler.jsonc rename to pkg/promlib/querydata/exemplar/testdata/stddev_sampler.jsonc diff --git a/pkg/tsdb/prometheus/querydata/framing_bench_test.go b/pkg/promlib/querydata/framing_bench_test.go similarity index 93% rename from pkg/tsdb/prometheus/querydata/framing_bench_test.go rename to pkg/promlib/querydata/framing_bench_test.go index 2b4cd28545b..b769df2b47d 100644 --- a/pkg/tsdb/prometheus/querydata/framing_bench_test.go +++ b/pkg/promlib/querydata/framing_bench_test.go @@ -17,11 +17,11 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/stretchr/testify/require" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/models" ) // when memory-profiling this benchmark, these commands are recommended: -// - go test -benchmem -run=^$ -bench ^BenchmarkExemplarJson$ github.com/grafana/grafana/pkg/tsdb/prometheus/querydata -memprofile memprofile.out -count 6 | tee old.txt +// - go test -benchmem -run=^$ -bench ^BenchmarkExemplarJson$ github.com/grafana/grafana/pkg/promlib/querydata -memprofile memprofile.out -count 6 | tee old.txt // - go tool pprof -http=localhost:6061 memprofile.out func BenchmarkExemplarJson(b *testing.B) { queryFileName := filepath.Join("../testdata", "exemplar.query.json") @@ -55,7 +55,7 @@ func BenchmarkExemplarJson(b *testing.B) { var resp *backend.QueryDataResponse // when memory-profiling this benchmark, these commands are recommended: -// - go test -benchmem -run=^$ -bench ^BenchmarkRangeJson$ github.com/grafana/grafana/pkg/tsdb/prometheus/querydata -memprofile memprofile.out -count 6 | tee old.txt +// - go test -benchmem -run=^$ -bench ^BenchmarkRangeJson$ github.com/grafana/grafana/pkg/promlib/querydata -memprofile memprofile.out -count 6 | tee old.txt // - go tool pprof -http=localhost:6061 memprofile.out // - benchstat old.txt new.txt func BenchmarkRangeJson(b *testing.B) { diff --git a/pkg/tsdb/prometheus/querydata/framing_test.go b/pkg/promlib/querydata/framing_test.go similarity index 98% rename from pkg/tsdb/prometheus/querydata/framing_test.go rename to pkg/promlib/querydata/framing_test.go index 7138ba19e9e..0d194f189ff 100644 --- a/pkg/tsdb/prometheus/querydata/framing_test.go +++ b/pkg/promlib/querydata/framing_test.go @@ -16,7 +16,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/experimental" "github.com/stretchr/testify/require" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" + "github.com/grafana/grafana/pkg/promlib/models" ) var update = true diff --git a/pkg/tsdb/prometheus/querydata/request.go b/pkg/promlib/querydata/request.go similarity index 94% rename from pkg/tsdb/prometheus/querydata/request.go rename to pkg/promlib/querydata/request.go index c80952b1a05..aabd347b9ae 100644 --- a/pkg/tsdb/prometheus/querydata/request.go +++ b/pkg/promlib/querydata/request.go @@ -7,20 +7,20 @@ import ( "regexp" "time" - "github.com/grafana/grafana-azure-sdk-go/util/maputil" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/data" + "github.com/grafana/grafana-plugin-sdk-go/data/utils/maputil" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" "github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/backend/tracing" - "github.com/grafana/grafana/pkg/tsdb/prometheus/client" - "github.com/grafana/grafana/pkg/tsdb/prometheus/intervalv2" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" - "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata/exemplar" - "github.com/grafana/grafana/pkg/tsdb/prometheus/utils" + "github.com/grafana/grafana/pkg/promlib/client" + "github.com/grafana/grafana/pkg/promlib/intervalv2" + "github.com/grafana/grafana/pkg/promlib/models" + "github.com/grafana/grafana/pkg/promlib/querydata/exemplar" + "github.com/grafana/grafana/pkg/promlib/utils" ) const legendFormatAuto = "__auto" diff --git a/pkg/tsdb/prometheus/querydata/request_test.go b/pkg/promlib/querydata/request_test.go similarity index 98% rename from pkg/tsdb/prometheus/querydata/request_test.go rename to pkg/promlib/querydata/request_test.go index cd3ba0f8672..3a1ac8986f9 100644 --- a/pkg/tsdb/prometheus/querydata/request_test.go +++ b/pkg/promlib/querydata/request_test.go @@ -20,9 +20,9 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/data" - "github.com/grafana/grafana/pkg/tsdb/prometheus/client" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" - "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata" + "github.com/grafana/grafana/pkg/promlib/client" + "github.com/grafana/grafana/pkg/promlib/models" + "github.com/grafana/grafana/pkg/promlib/querydata" ) func TestPrometheus_parseTimeSeriesResponse(t *testing.T) { diff --git a/pkg/tsdb/prometheus/querydata/response.go b/pkg/promlib/querydata/response.go similarity index 95% rename from pkg/tsdb/prometheus/querydata/response.go rename to pkg/promlib/querydata/response.go index 70602c0158e..7181400df16 100644 --- a/pkg/tsdb/prometheus/querydata/response.go +++ b/pkg/promlib/querydata/response.go @@ -12,10 +12,10 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/data" jsoniter "github.com/json-iterator/go" - "github.com/grafana/grafana/pkg/tsdb/prometheus/converter" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" - "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata/exemplar" - "github.com/grafana/grafana/pkg/tsdb/prometheus/utils" + "github.com/grafana/grafana/pkg/promlib/converter" + "github.com/grafana/grafana/pkg/promlib/models" + "github.com/grafana/grafana/pkg/promlib/querydata/exemplar" + "github.com/grafana/grafana/pkg/promlib/utils" ) func (s *QueryData) parseResponse(ctx context.Context, q *models.Query, res *http.Response, enablePrometheusDataplaneFlag bool) backend.DataResponse { diff --git a/pkg/tsdb/prometheus/querydata/response_test.go b/pkg/promlib/querydata/response_test.go similarity index 96% rename from pkg/tsdb/prometheus/querydata/response_test.go rename to pkg/promlib/querydata/response_test.go index 62d27222d8e..8cec7ba460a 100644 --- a/pkg/tsdb/prometheus/querydata/response_test.go +++ b/pkg/promlib/querydata/response_test.go @@ -9,8 +9,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/grafana/grafana/pkg/tsdb/prometheus/models" - "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata/exemplar" + "github.com/grafana/grafana/pkg/promlib/models" + "github.com/grafana/grafana/pkg/promlib/querydata/exemplar" ) func TestQueryData_parseResponse(t *testing.T) { diff --git a/pkg/tsdb/prometheus/resource/resource.go b/pkg/promlib/resource/resource.go similarity index 92% rename from pkg/tsdb/prometheus/resource/resource.go rename to pkg/promlib/resource/resource.go index aeda2303ea2..888267d4138 100644 --- a/pkg/tsdb/prometheus/resource/resource.go +++ b/pkg/promlib/resource/resource.go @@ -6,12 +6,12 @@ import ( "fmt" "net/http" - "github.com/grafana/grafana-azure-sdk-go/util/maputil" "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-plugin-sdk-go/backend/log" + "github.com/grafana/grafana-plugin-sdk-go/data/utils/maputil" - "github.com/grafana/grafana/pkg/tsdb/prometheus/client" - "github.com/grafana/grafana/pkg/tsdb/prometheus/utils" + "github.com/grafana/grafana/pkg/promlib/client" + "github.com/grafana/grafana/pkg/promlib/utils" ) type Resource struct { diff --git a/pkg/tsdb/prometheus/testdata/exemplar.query.json b/pkg/promlib/testdata/exemplar.query.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/exemplar.query.json rename to pkg/promlib/testdata/exemplar.query.json diff --git a/pkg/tsdb/prometheus/testdata/exemplar.result.golden.jsonc b/pkg/promlib/testdata/exemplar.result.golden.jsonc similarity index 100% rename from pkg/tsdb/prometheus/testdata/exemplar.result.golden.jsonc rename to pkg/promlib/testdata/exemplar.result.golden.jsonc diff --git a/pkg/tsdb/prometheus/testdata/exemplar.result.json b/pkg/promlib/testdata/exemplar.result.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/exemplar.result.json rename to pkg/promlib/testdata/exemplar.result.json diff --git a/pkg/tsdb/prometheus/testdata/range_auto.query.json b/pkg/promlib/testdata/range_auto.query.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_auto.query.json rename to pkg/promlib/testdata/range_auto.query.json diff --git a/pkg/tsdb/prometheus/testdata/range_auto.result.golden.jsonc b/pkg/promlib/testdata/range_auto.result.golden.jsonc similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_auto.result.golden.jsonc rename to pkg/promlib/testdata/range_auto.result.golden.jsonc diff --git a/pkg/tsdb/prometheus/testdata/range_auto.result.json b/pkg/promlib/testdata/range_auto.result.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_auto.result.json rename to pkg/promlib/testdata/range_auto.result.json diff --git a/pkg/tsdb/prometheus/testdata/range_infinity.query.json b/pkg/promlib/testdata/range_infinity.query.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_infinity.query.json rename to pkg/promlib/testdata/range_infinity.query.json diff --git a/pkg/tsdb/prometheus/testdata/range_infinity.result.golden.jsonc b/pkg/promlib/testdata/range_infinity.result.golden.jsonc similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_infinity.result.golden.jsonc rename to pkg/promlib/testdata/range_infinity.result.golden.jsonc diff --git a/pkg/tsdb/prometheus/testdata/range_infinity.result.json b/pkg/promlib/testdata/range_infinity.result.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_infinity.result.json rename to pkg/promlib/testdata/range_infinity.result.json diff --git a/pkg/tsdb/prometheus/testdata/range_missing.query.json b/pkg/promlib/testdata/range_missing.query.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_missing.query.json rename to pkg/promlib/testdata/range_missing.query.json diff --git a/pkg/tsdb/prometheus/testdata/range_missing.result.golden.jsonc b/pkg/promlib/testdata/range_missing.result.golden.jsonc similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_missing.result.golden.jsonc rename to pkg/promlib/testdata/range_missing.result.golden.jsonc diff --git a/pkg/tsdb/prometheus/testdata/range_missing.result.json b/pkg/promlib/testdata/range_missing.result.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_missing.result.json rename to pkg/promlib/testdata/range_missing.result.json diff --git a/pkg/tsdb/prometheus/testdata/range_nan.query.json b/pkg/promlib/testdata/range_nan.query.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_nan.query.json rename to pkg/promlib/testdata/range_nan.query.json diff --git a/pkg/tsdb/prometheus/testdata/range_nan.result.golden.jsonc b/pkg/promlib/testdata/range_nan.result.golden.jsonc similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_nan.result.golden.jsonc rename to pkg/promlib/testdata/range_nan.result.golden.jsonc diff --git a/pkg/tsdb/prometheus/testdata/range_nan.result.json b/pkg/promlib/testdata/range_nan.result.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_nan.result.json rename to pkg/promlib/testdata/range_nan.result.json diff --git a/pkg/tsdb/prometheus/testdata/range_simple.query.json b/pkg/promlib/testdata/range_simple.query.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_simple.query.json rename to pkg/promlib/testdata/range_simple.query.json diff --git a/pkg/tsdb/prometheus/testdata/range_simple.result.golden.jsonc b/pkg/promlib/testdata/range_simple.result.golden.jsonc similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_simple.result.golden.jsonc rename to pkg/promlib/testdata/range_simple.result.golden.jsonc diff --git a/pkg/tsdb/prometheus/testdata/range_simple.result.json b/pkg/promlib/testdata/range_simple.result.json similarity index 100% rename from pkg/tsdb/prometheus/testdata/range_simple.result.json rename to pkg/promlib/testdata/range_simple.result.json diff --git a/pkg/tsdb/prometheus/utils/utils.go b/pkg/promlib/utils/utils.go similarity index 100% rename from pkg/tsdb/prometheus/utils/utils.go rename to pkg/promlib/utils/utils.go diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index ea090f1403d..1eedafa338b 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -11,12 +11,12 @@ import ( "github.com/grafana/grafana/pkg/components/null" "github.com/grafana/grafana/pkg/components/simplejson" + prometheus "github.com/grafana/grafana/pkg/promlib" "github.com/grafana/grafana/pkg/services/alerting" "github.com/grafana/grafana/pkg/services/datasources" ngalertmodels "github.com/grafana/grafana/pkg/services/ngalert/models" "github.com/grafana/grafana/pkg/tsdb/legacydata" "github.com/grafana/grafana/pkg/tsdb/legacydata/interval" - "github.com/grafana/grafana/pkg/tsdb/prometheus" ) func init() { diff --git a/pkg/tsdb/loki/api.go b/pkg/tsdb/loki/api.go index 272edafd8b5..aa6c9d88396 100644 --- a/pkg/tsdb/loki/api.go +++ b/pkg/tsdb/loki/api.go @@ -23,8 +23,8 @@ import ( "github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/tracing" + "github.com/grafana/grafana/pkg/promlib/converter" "github.com/grafana/grafana/pkg/tsdb/loki/instrumentation" - "github.com/grafana/grafana/pkg/tsdb/prometheus/converter" ) type LokiAPI struct { diff --git a/pkg/tsdb/prometheus/azureauth/azure.go b/pkg/tsdb/prometheus/azureauth/azure.go index 93b8e5263e8..f5553c2b3f0 100644 --- a/pkg/tsdb/prometheus/azureauth/azure.go +++ b/pkg/tsdb/prometheus/azureauth/azure.go @@ -12,7 +12,7 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" - "github.com/grafana/grafana/pkg/tsdb/prometheus/utils" + "github.com/grafana/grafana/pkg/promlib/utils" ) func ConfigureAzureAuthentication(settings backend.DataSourceInstanceSettings, azureSettings *azsettings.AzureSettings, clientOpts *sdkhttpclient.Options) error { diff --git a/pkg/tsdb/prometheus/prometheus.go b/pkg/tsdb/prometheus/prometheus.go index a6b92b87000..1f3ab688cbd 100644 --- a/pkg/tsdb/prometheus/prometheus.go +++ b/pkg/tsdb/prometheus/prometheus.go @@ -2,145 +2,67 @@ package prometheus import ( "context" - "errors" "fmt" - "strings" - "time" + "github.com/grafana/grafana-azure-sdk-go/azsettings" "github.com/grafana/grafana-plugin-sdk-go/backend" - "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" - "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" - "github.com/grafana/grafana-plugin-sdk-go/backend/log" - "github.com/patrickmn/go-cache" - apiv1 "github.com/prometheus/client_golang/api/prometheus/v1" + sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" - "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" - - "github.com/grafana/grafana/pkg/tsdb/prometheus/client" - "github.com/grafana/grafana/pkg/tsdb/prometheus/instrumentation" - "github.com/grafana/grafana/pkg/tsdb/prometheus/querydata" - "github.com/grafana/grafana/pkg/tsdb/prometheus/resource" + "github.com/grafana/grafana/pkg/promlib" + "github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth" ) type Service struct { - im instancemgmt.InstanceManager - logger log.Logger + lib *promlib.Service } -type instance struct { - queryData *querydata.QueryData - resource *resource.Resource - versionCache *cache.Cache -} - -func ProvideService(httpClientProvider *httpclient.Provider) *Service { +func ProvideService(httpClientProvider *sdkhttpclient.Provider) *Service { plog := backend.NewLoggerWith("logger", "tsdb.prometheus") plog.Debug("Initializing") return &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpClientProvider, plog)), - logger: plog, - } -} - -func newInstanceSettings(httpClientProvider *httpclient.Provider, log log.Logger) datasource.InstanceFactoryFunc { - return func(ctx context.Context, settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { - // Creates a http roundTripper. - opts, err := client.CreateTransportOptions(ctx, settings, log) - if err != nil { - return nil, fmt.Errorf("error creating transport options: %v", err) - } - httpClient, err := httpClientProvider.New(*opts) - if err != nil { - return nil, fmt.Errorf("error creating http client: %v", err) - } - - // New version using custom client and better response parsing - qd, err := querydata.New(httpClient, settings, log) - if err != nil { - return nil, err - } - - // Resource call management using new custom client same as querydata - r, err := resource.New(httpClient, settings, log) - if err != nil { - return nil, err - } - - return instance{ - queryData: qd, - resource: r, - versionCache: cache.New(time.Minute*1, time.Minute*5), - }, nil + lib: promlib.NewService(httpClientProvider, plog, extendClientOpts), } } func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { - if len(req.Queries) == 0 { - err := fmt.Errorf("query contains no queries") - instrumentation.UpdateQueryDataMetrics(err, nil) - return &backend.QueryDataResponse{}, err - } - - i, err := s.getInstance(ctx, req.PluginContext) - if err != nil { - instrumentation.UpdateQueryDataMetrics(err, nil) - return nil, err - } - - qd, err := i.queryData.Execute(ctx, req) - instrumentation.UpdateQueryDataMetrics(err, qd) - - return qd, err + return s.lib.QueryData(ctx, req) } func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { - i, err := s.getInstance(ctx, req.PluginContext) - if err != nil { - return err + return s.lib.CallResource(ctx, req, sender) +} + +func (s *Service) GetBuildInfo(ctx context.Context, req promlib.BuildInfoRequest) (*promlib.BuildInfoResponse, error) { + return s.lib.GetBuildInfo(ctx, req) +} + +func (s *Service) GetHeuristics(ctx context.Context, req promlib.HeuristicsRequest) (*promlib.Heuristics, error) { + return s.lib.GetHeuristics(ctx, req) +} + +func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, + error) { + return s.lib.CheckHealth(ctx, req) +} + +func extendClientOpts(ctx context.Context, settings backend.DataSourceInstanceSettings, clientOpts *sdkhttpclient.Options) error { + // Set SigV4 service namespace + if clientOpts.SigV4 != nil { + clientOpts.SigV4.Service = "aps" } - if strings.EqualFold(req.Path, "version-detect") { - versionObj, found := i.versionCache.Get("version") - if found { - return sender.Send(versionObj.(*backend.CallResourceResponse)) - } + azureSettings, err := azsettings.ReadSettings(ctx) + if err != nil { + return fmt.Errorf("failed to read Azure settings from Grafana: %v", err) + } - vResp, err := i.resource.DetectVersion(ctx, req) + // Set Azure authentication + if azureSettings.AzureAuthEnabled { + err = azureauth.ConfigureAzureAuthentication(settings, azureSettings, clientOpts) if err != nil { - return err + return fmt.Errorf("error configuring Azure auth: %v", err) } - i.versionCache.Set("version", vResp, cache.DefaultExpiration) - return sender.Send(vResp) } - resp, err := i.resource.Execute(ctx, req) - if err != nil { - return err - } - - return sender.Send(resp) -} - -func (s *Service) getInstance(ctx context.Context, pluginCtx backend.PluginContext) (*instance, error) { - i, err := s.im.Get(ctx, pluginCtx) - if err != nil { - return nil, err - } - in := i.(instance) - return &in, nil -} - -// IsAPIError returns whether err is or wraps a Prometheus error. -func IsAPIError(err error) bool { - // Check if the right error type is in err's chain. - var e *apiv1.Error - return errors.As(err, &e) -} - -func ConvertAPIError(err error) error { - var e *apiv1.Error - if errors.As(err, &e) { - return fmt.Errorf("%s: %s", e.Msg, e.Detail) - } - return err + return nil } diff --git a/pkg/tsdb/prometheus/prometheus_test.go b/pkg/tsdb/prometheus/prometheus_test.go index 1d21212a18f..458fa31a5ce 100644 --- a/pkg/tsdb/prometheus/prometheus_test.go +++ b/pkg/tsdb/prometheus/prometheus_test.go @@ -2,116 +2,53 @@ package prometheus import ( "context" - "io" - "net/http" "testing" - "time" + "github.com/grafana/grafana-azure-sdk-go/azsettings" "github.com/grafana/grafana-plugin-sdk-go/backend" - "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" - "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" + sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/stretchr/testify/require" ) -type fakeSender struct{} - -func (sender *fakeSender) Send(resp *backend.CallResourceResponse) error { - return nil -} - -type fakeRoundtripper struct { - Req *http.Request -} - -func (rt *fakeRoundtripper) RoundTrip(req *http.Request) (*http.Response, error) { - rt.Req = req - return &http.Response{ - Status: "200", - StatusCode: 200, - Header: nil, - Body: nil, - ContentLength: 0, - }, nil -} - -type fakeHTTPClientProvider struct { - httpclient.Provider - Roundtripper *fakeRoundtripper -} - -func (provider *fakeHTTPClientProvider) New(opts ...httpclient.Options) (*http.Client, error) { - client := &http.Client{} - provider.Roundtripper = &fakeRoundtripper{} - client.Transport = provider.Roundtripper - return client, nil -} - -func (provider *fakeHTTPClientProvider) GetTransport(opts ...httpclient.Options) (http.RoundTripper, error) { - return &fakeRoundtripper{}, nil -} - -func getMockPromTestSDKProvider(f *fakeHTTPClientProvider) *httpclient.Provider { - anotherFN := func(o httpclient.Options, next http.RoundTripper) http.RoundTripper { - _, _ = f.New() - return f.Roundtripper - } - fn := httpclient.MiddlewareFunc(anotherFN) - mid := httpclient.NamedMiddlewareFunc("mock", fn) - return httpclient.NewProvider(httpclient.ProviderOptions{Middlewares: []httpclient.Middleware{mid}}) -} - -func TestService(t *testing.T) { - t.Run("Service", func(t *testing.T) { - t.Run("CallResource", func(t *testing.T) { - t.Run("creates correct request", func(t *testing.T) { - f := &fakeHTTPClientProvider{} - httpProvider := getMockPromTestSDKProvider(f) - service := &Service{ - im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, backend.NewLoggerWith("logger", "test"))), - } - - req := &backend.CallResourceRequest{ - PluginContext: backend.PluginContext{ - OrgID: 0, - PluginID: "prometheus", - User: nil, - AppInstanceSettings: nil, - DataSourceInstanceSettings: &backend.DataSourceInstanceSettings{ - ID: 0, - UID: "", - Type: "prometheus", - Name: "test-prom", - URL: "http://localhost:9090", - User: "", - Database: "", - BasicAuthEnabled: true, - BasicAuthUser: "admin", - Updated: time.Time{}, - JSONData: []byte("{}"), - }, - }, - Path: "/api/v1/series", - Method: http.MethodPost, - URL: "/api/v1/series", - Body: []byte("match%5B%5D: ALERTS\nstart: 1655271408\nend: 1655293008"), - } - - sender := &fakeSender{} - err := service.CallResource(context.Background(), req, sender) - require.NoError(t, err) - require.Equal( - t, - http.Header{ - "Content-Type": {"application/x-www-form-urlencoded"}, - "Idempotency-Key": []string(nil), - }, - f.Roundtripper.Req.Header) - require.Equal(t, http.MethodPost, f.Roundtripper.Req.Method) - body, err := io.ReadAll(f.Roundtripper.Req.Body) - require.NoError(t, err) - require.Equal(t, []byte("match%5B%5D: ALERTS\nstart: 1655271408\nend: 1655293008"), body) - require.Equal(t, "http://localhost:9090/api/v1/series", f.Roundtripper.Req.URL.String()) - }) +func TestExtendClientOpts(t *testing.T) { + t.Run("add azure credentials if configured", func(t *testing.T) { + cfg := backend.NewGrafanaCfg(map[string]string{ + azsettings.AzureCloud: azsettings.AzurePublic, + azsettings.AzureAuthEnabled: "true", }) + settings := backend.DataSourceInstanceSettings{ + BasicAuthEnabled: false, + BasicAuthUser: "", + JSONData: []byte(`{ + "azureCredentials": { + "authType": "msi" + } + }`), + DecryptedSecureJSONData: map[string]string{}, + } + ctx := backend.WithGrafanaConfig(context.Background(), cfg) + opts := &sdkhttpclient.Options{} + err := extendClientOpts(ctx, settings, opts) + require.NoError(t, err) + require.Equal(t, 1, len(opts.Middlewares)) + }) + + t.Run("add sigV4 auth if opts has SigV4 configured", func(t *testing.T) { + settings := backend.DataSourceInstanceSettings{ + BasicAuthEnabled: false, + BasicAuthUser: "", + JSONData: []byte(""), + DecryptedSecureJSONData: map[string]string{}, + } + opts := &sdkhttpclient.Options{ + SigV4: &sdkhttpclient.SigV4Config{ + AuthType: "test", + AccessKey: "accesskey", + SecretKey: "secretkey", + }, + } + err := extendClientOpts(context.Background(), settings, opts) + require.NoError(t, err) + require.Equal(t, "aps", opts.SigV4.Service) }) }