问题
I am trying to write to an openTSDB database so I can analyse my data using Bosun.
If I manually add data through the Bosun interface it works fine, however if i do a POST
request to <docker-ip>/api/put
(where <docker-ip>
is configured correctly) the data does not show up in Bosun.
If I send the data points as a a JSON from my Java application nothing shows up at all in Bosun, but if I send the request using the chrome app 'Postman' then the metric shows up, but the data I sent with the request does not.
This is the data I'm sending:
try {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("http://192.168.59.103:8070/api/put?summary");
StringEntity params = new StringEntity("{\"metric\":\"tester.example\",\"timestamp\":\"" + System.currentTimeMillis() + "\", \"value\": \"22\", \"tags\": { \"host\": \"chrisedwards\", \"dc\": \"lga\" }}");
request.setEntity(params);
request.setHeader("Content-Type", "application/json; charset=UTF-8");
HttpResponse response = httpClient.execute(request);
System.out.println(response);
// handle response here...
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// httpClient.close();
}
which returns a 200 response code
. I send the same request using Postmaster to the same address as in the java application however, the postmaster request shows the metric name in Bosun but no data, and the Java request doesn't even show the metric name.
回答1:
Try this, it served my purpose:
try {
String url = "http://192.168.59.103:8070/api/put";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "{\"metric\":\"tester.example\",\"timestamp\":\"" + System.currentTimeMillis() + "\", \"value\": \"22\", \"tags\": { \"host\": \"chrisedwards\", \"dc\": \"lga\" }}";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
catch(Exception e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/31383406/opentsdb-rest-api-is-not-storing-data