-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forward grafana meta values to downstream API #1072
Conversation
…s to the 3rd party API endpoint (#1056)
58a8f7a
to
fb1799b
Compare
if pCtx.DataSourceInstanceSettings != nil { | ||
value = strings.ReplaceAll(value, "${__ds.uid}", pCtx.DataSourceInstanceSettings.UID) | ||
value = strings.ReplaceAll(value, "${__ds.name}", pCtx.DataSourceInstanceSettings.Name) | ||
value = strings.ReplaceAll(value, "${__ds.id}", fmt.Sprintf("%d", pCtx.DataSourceInstanceSettings.ID)) | ||
} | ||
if pCtx.User != nil { | ||
value = strings.ReplaceAll(value, "${__user.login}", pCtx.User.Login) | ||
value = strings.ReplaceAll(value, "${__user.email}", pCtx.User.Email) | ||
value = strings.ReplaceAll(value, "${__user.name}", pCtx.User.Name) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we throw error in such nil cases to assist more to the users? or it would cause more inconvenience? ( If this used in the context of cron/anonymous jobs such as alert, public dashboard... then potentially the queries will silently fail there without much context but may succeed in the dashboards / explore page )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think throwing an error make sense. I was thinking about logging an error here but it may be better to throw and surface the error to the end user. Thinking out loud that would mean that the query would fail and not run because of this. right? So a user would have to create a separate ds with headers and without headers which is not ideal. How should we let the user know that these headers won't work in alert?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea. it is bit tricky. I prefer to go with current approach now and then later iterations, we can figure out if we can send warnings to the frontend in such cases.
@mahendrapaipuri - if u have more thoughts around this, suggest to respond here in the thread so that it will be useful for reviewers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it out and works as expected.
This PR will allow datasource authors to pass grafana meta data such as user id, datasource uid to the underlying API as query parameters / headers.
Any value set by the user in query will be overidden by the values from the data source settings.
Fixes #1073
Fixes #867
Fixes #457
Design decisions & FAQs
send_user_header
? Answer: That setting is applicable for all the datasource instances. But in infinity, as said before not all the APIs will like additional headersHow to test this
Before changes
git checkout main && git pull && yarn && yarn build && mage -v
docker compose up
Open http://localhost:3000
add a new infinity datasource instance
in datasource custom http header add a new key
X-Grafana-User
with value${__user.login}
in datasource URL query parameter add a new key
X-Grafana-Datasource-UID
with value${__ds.uid}
Add
https://httpbin.org
to the allowed hostsSave and test the datasource
Create a query in explore with URL and backend parser option
Add
https://httpbin.org/get
as URL and run the query. Inspect headers and args in the resultAfter changes
git checkout grafana-headers-forwarding && git pull && yarn && yarn build && mage -v
docker compose up
Open http://localhost:3000
add a new infinity datasource instance
in datasource custom http header add a new key
X-Grafana-User
with value${__user.login}
in datasource URL query parameter add a new key
X-Grafana-Datasource-UID
with value${__ds.uid}
Add
https://httpbin.org
to the allowed hostsSave and test the datasource
Create a query in explore with URL and backend parser option
Add
https://httpbin.org/get
as URL and run the query. Inspect headers and args in the resultalso in query inspector