@Override
    public void write(Iterable<QueryResult> results) {
        logger.debug("Export to '{}' results {}", graphiteHttpUrl, results);
        HttpURLConnection urlConnection = null;
        OutputStreamWriter urlWriter;
        try {
            StringBuilder sbUrlWriter = new StringBuilder("");
            for (QueryResult result : results) {
                String msg = metricPathPrefix + result.getName() + " " + result.getValue() + " " + result.getEpoch(TimeUnit.SECONDS) + "\n";
                logger.debug("Export '{}'", msg);
                sbUrlWriter.append(msg);
            }
            if (sbUrlWriter.length() > 0) {
                sbUrlWriter.insert(0, "data=");
                urlConnection = (HttpURLConnection) graphiteHttpUrl.openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.setDoOutput(true);
                urlWriter = new OutputStreamWriter(urlConnection.getOutputStream(), Charset.forName("UTF-8"));
                urlWriter.write(sbUrlWriter.toString());
                urlWriter.flush();
                IoUtils2.closeQuietly(urlWriter);
                int responseCode = urlConnection.getResponseCode();
                if (responseCode != 200) {
                    logger.warn("Failure {}:'{}' to send result to Graphite HTTP proxy'{}' ", responseCode, urlConnection.getResponseMessage(), graphiteHttpUrl);
                }
                if (logger.isTraceEnabled()) {
                    IoUtils2.copy(urlConnection.getInputStream(), System.out);
                }
            }
        } catch (Exception e) {
            logger.warn("Failure to send result to Graphite HTTP proxy '{}'", graphiteHttpUrl, e);
        } finally {
            // Release the connection.
            if (urlConnection != null) {
                try {
                    InputStream in = urlConnection.getInputStream();
                    IoUtils2.copy(in, IoUtils2.nullOutputStream());
                    IoUtils2.closeQuietly(in);
                    InputStream err = urlConnection.getErrorStream();
                    if (err != null) {
                        IoUtils2.copy(err, IoUtils2.nullOutputStream());
                        IoUtils2.closeQuietly(err);
                    }
                } catch (IOException e) {
                    logger.warn("Exception flushing http connection", e);
                }
            }
        }
    }
@ Override public void write ( Iterable < QueryResult > results ) { logger . debug ( "Export to '{}' results {}" , graphiteHttpUrl , results ) ; HttpURLConnection urlConnection = null ; OutputStreamWriter urlWriter ; try { StringBuilder sbUrlWriter = new StringBuilder ( "" ) ; for ( QueryResult result : results ) { String msg = metricPathPrefix + result . getName ( ) + " " + result . getValue ( ) + " " + result . getEpoch ( TimeUnit . SECONDS ) + "\n" ; logger . debug ( "Export '{}'" , msg ) ; sbUrlWriter . append ( msg ) ; } if ( sbUrlWriter . length ( ) > 0 ) { sbUrlWriter . insert ( 0 , "data=" ) ; urlConnection = ( HttpURLConnection ) graphiteHttpUrl . openConnection ( ) ; urlConnection . setRequestMethod ( "POST" ) ; urlConnection . setDoOutput ( true ) ; urlWriter = new OutputStreamWriter ( urlConnection . getOutputStream ( ) , Charset . forName ( "UTF-8" ) ) ; urlWriter . write ( sbUrlWriter . toString ( ) ) ; urlWriter . flush ( ) ; IoUtils2 . closeQuietly ( urlWriter ) ; int responseCode = urlConnection . getResponseCode ( ) ; if ( responseCode != 200 ) { logger . warn ( "Failure {}:'{}' to send result to Graphite HTTP proxy'{}' " , responseCode , urlConnection . getResponseMessage ( ) , graphiteHttpUrl ) ; } if ( logger . isTraceEnabled ( ) ) { IoUtils2 . copy ( urlConnection . getInputStream ( ) , System . out ) ; } } } catch ( Exception e ) { logger . warn ( "Failure to send result to Graphite HTTP proxy '{}'" , graphiteHttpUrl , e ) ; } finally { if ( urlConnection != null ) { try { InputStream in = urlConnection . getInputStream ( ) ; IoUtils2 . copy ( in , IoUtils2 . nullOutputStream ( ) ) ; IoUtils2 . closeQuietly ( in ) ; InputStream err = urlConnection . getErrorStream ( ) ; if ( err != null ) { IoUtils2 . copy ( err , IoUtils2 . nullOutputStream ( ) ) ; IoUtils2 . closeQuietly ( err ) ; } } catch ( IOException e ) { logger . warn ( "Exception flushing http connection" , e ) ; } } } }
Send given metrics to the Graphite server.