0
votes

I am trying to generate grafana dashboard by calling the spring REST API using simple-json-datasource. Implemented all required APIs (/, /search, /query) for enabling simple-json-datasource and maintained the contract as expected by grafana simple-json-datasource.

Also, added the CORS as part of response header and even enabled the CORS for the request API by adding the @CrossOrigin at ****@RestController**.

When i test the APIs, i am getting following header information in the response of the call { “access-control-allow-headers”: “Content-Type, Accept”, “access-control-allow-methods”: “POST, GET”, “access-control-allow-origin”: “*”, “connection”: “keep-alive”, “content-type”: “application/json”, “date”: “Thu, 14 May 2020 16:51:18 GMT”, “keep-alive”: “timeout=60”, “transfer-encoding”: “chunked”, “vary”: “Origin, Access-Control-Request-Method, Access-Control-Request-Headers” }

But while creating the datasource by pointing to /, i was getting the below error

Access to XMLHttpRequest at ‘http://localhost:8888/streams/api/’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed.

Input request for the API call is : curl -X GET --header ‘Accept: application/json’ ‘http://localhost:8888/streams/api/

Any clue?

1

1 Answers

2
votes

To make the integration working, we referred one of the sample API application provided, https://github.com/bergquist/fake-simple-json-datasource (fake-simple-json-datasource). APIs has to follow below protocols,

  1. API / is of type GET, while /search, /query and /annotations are of method types POST
  2. / API is used for creating the datasource in Grafana
  3. /search API has no input, but the response of this API will be used as a one of the input to /query
  4. /query API, as different response structures depending upon the type of matric selected in dashboard
  5. APIs has to be enabled CORS with origins and allowedHeaders
  6. The response of the API has to have header details
  7. Headers should have access-control-allow-methods, access-control-allow-headers and set to POST and accept, content-type respectively. These two properties are mandatory for the response header