openTSDB REST API不存储数据 [英] openTSDB REST API is not storing data

查看:165
本文介绍了openTSDB REST API不存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试写入openTSDB数据库,以便可以使用Bosun分析我的数据.

如果我通过Bosun界面手动添加数据,则工作正常,但是,如果我对< docker-ip>/api/put(其中正确配置了<docker-ip>的位置)发出POST请求,则数据不会显示在Bosun中./p>

如果我从Java应用程序将数据点作为JSON发送为JSON,则在Bosun中什么都不会显示,但是如果我使用Chrome应用程序邮递员"发送请求,则会显示该指标,但是我使用请求没有.

这是我发送的数据:

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();
}

返回200 response code.我使用Postmaster将相同的请求发送到与Java应用程序中相同的地址,但是,postmaster请求在Bosun中显示了度量标准名称,但是没有数据,并且Java请求甚至没有显示度量标准名称.

解决方案

尝试一下,它达到了我的目的:

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();
        }

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.

解决方案

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();
        }

这篇关于openTSDB REST API不存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆