-
Notifications
You must be signed in to change notification settings - Fork 31
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
Bug: still request data from browser even after using getBackendSrv and adding routes settings in plugin.json #1661
Comments
"routes": [
{
"path": "myRoutePath",
"url": "{{ .JsonData.apiUrl }}"
}
]
constructor(instanceSettings: DataSourceInstanceSettings<MyDataSourceOptions>) {
super(instanceSettings);
this.baseUrl = instanceSettings.jsonData.apiUrl!;
console.log(this.baseUrl);
}
export function ConfigEditor(props: Props) {
const { onOptionsChange, options } = props;
const { jsonData } = options;
const onApiUrlChange = (event: ChangeEvent<HTMLInputElement>) => {
onOptionsChange({
...options,
jsonData: {
...jsonData,
// notice we set the apiUrl value inside jsonData
apiUrl: event.target.value,
},
});
};
return (
<InlineField label="apiUrl" labelWidth={12}>
<Input
onChange={onApiUrlChange}
value={jsonData.apiUrl || ''}
placeholder="json field returned to frontend"
width={40}
/>
</InlineField>
);
} |
Hi @liubq919 and thanks for taking the time to report this issue 🙌 In your async query(options: DataQueryRequest): Promise<DataQueryResponse> {
const response = getBackendSrv().fetch<TODO[]>({
// You can see above that `this.baseUrl` is set in the constructor
// in this example we assume the configured url is
// https://jsonplaceholder.typicode.com
/// if you inspect `this.baseUrl` you'll see the Grafana data proxy url
url: `${this.baseUrl}/myRoutePath/todos`, // <==== need to use the route defined in plugin.json here
});
// backendSrv fetch returns an observable object
// we should unwrap with rxjs
const responseData = await lastValueFrom(response);
const todos = responseData.data;
// we'll return the same todos for all queries in this example
// in a real data source each target should fetch the data
// as necessary.
const data: PartialDataFrame[] = options.targets.map((target) => {
return {
refId: target.refId,
fields: [
{ name: 'Id', type: FieldType.number, values: todos.map((todo) => todo.id) },
{ name: 'Title', type: FieldType.string, values: todos.map((todo) => todo.title) },
],
};
});
return { data };
} |
@ hugohaggmark, thanks hugohaggmark, retried but it still does not work, myRoutePath is part of the url that i need to request, still not proxy request. the request i need to request: http://127.0.0.1:8848/all-spans/d3e10152e97f3de52df81e25d93122e0 after adding myRoutePath, it becames http://127.0.0.1:8848/myRoutePath/all-spans/d3e10152e97f3de52df81e25d93122e0 |
Thank for the update @liubq919 Are you able to share your code and your grafana.ini file? |
@hugohaggmark thanks hugohaggmark, rebuild my data source plugin and restart grafana server, it works now, thanks for your help. |
Great to hear that @liubq919 👍 |
Which package(s) does this bug affect?
Package versions
What happened?
Follow the How to use the data proxy in data source plugins with a custom configuration page.
but it does not work, the request still happens from the browser, not the backend.
What you expected to happen
request data from grafana backend not browser
How to reproduce it (as minimally and precisely as possible)
Environment
Additional context
grafana: v11.2
The text was updated successfully, but these errors were encountered: