Hosted Graphite Docs
Get StartedBook a Demo
  • Welcome to Hosted Graphite
  • Getting Started
  • HG-CLI
  • Sending Metrics
    • Supported Protocols
    • Graphite Tag Support
    • Metric Management
    • Metric APIs
  • Language Guide
    • Metric Libraries
    • .NET
    • Go
    • Java
    • Javascript
    • Node.js
    • PHP
    • Postman
    • Python 2.x
    • Python 3.x
    • Python Pickle
    • Ruby
    • Shell
    • TypeScript
  • Dashboard and Graphs
    • Primary Dashboards
    • Dashboard Library
    • Local Dashboard Integration
    • Worldmap Panel
    • Graphite Dashboard Guide
    • Graphite Graph Menu Reference
    • Other Dashboard Options
  • Alerting Guide
    • Alerting Overview
    • Alerts API
    • Notification Channels API
    • Scheduled Mutes API
    • Using Your Own Alerting
  • Agents Guide
    • The Hosted Graphite Agent
      • Base Metrics
      • System Layout
    • Telegraf
    • K8 Telegraf Daemon
    • OpenTelemetry
    • collectd Agent
    • StatsD Agent
    • Diamond
  • Add-Ons and Integrations Guide
    • AWS CloudWatch
    • Azure Monitor Metrics
    • GCP Metrics
    • Carbon-C-Relay
    • Circle CI
    • Cloudbees
    • Collectd Add Ons
    • GitHub
    • GitLab
    • Heroku
    • Hosted StatsD
    • New Relic
    • Papertrail
    • Pingdom
    • Sentry
    • Sitespeed
    • StatsD Add-on
    • Statuspage
  • Account Management
    • Access Keys
    • Account Diagnostics
    • Account Settings
    • Team Access: Limited Access Groups
    • SAML Authentication
    • Team Access
  • Additional Features
    • Aggregation Rules
    • Data Views
  • API Guides
    • Metrics API
    • Tag API
    • Graphite Render API
    • Render Variables API
    • Dashboard API
    • Annotations and Events API
    • Aggregation Rules API
    • Alerts APIs
  • FAQ
    • General
    • Business
    • Technical
    • Account Metrics and Limiting
    • Customization
    • Troubleshooting
    • Support
    • Changelog
Powered by GitBook
On this page
  • Interesting Java Metrics Libraries
  • Java DNS Behaviour
  • Sending a metric via TCP
  • Sending a metric via UDP
  • Sending a metric via HTTP POST
  • Google App Engine HTTP Post

Was this helpful?

  1. Language Guide

Java

How to send metrics using Java

PreviousGoNextJavascript

Last updated 1 year ago

Was this helpful?

Contents

  • Java

  • Coda Hale’s metrics library

    - Developed by Yammer to instrument their JVM-based backend services, it’s a really comprehensive Java metrics library and works well with Hosted Graphite.

  • Metrics-StatsD for Coda Hale’s metrics library

Java’s default JVM behaviour includes an optimisation/attempt to prevent DNS cache poisoning whereby it will read DNS entries for a service and then cache them forever. This is something that will result in problems with Hosted Graphite.

To service our hundreds of customers we run a lot of different machines and we swap machines in and out all the time. This means that if you have cached the DNS entry for a machine that no longer exists, you’re going to drop a lot of data.

To prevent this, you can set the JVM cache behaviour to only cache entries for 60 seconds:

java.security.Security.setProperty("networkaddress.cache.ttl" , "60");
import java.io.DataOutputStream;
import java.net.Socket;

...
Socket conn          = new Socket("YOUR-UID.carbon.hostedgraphite.com", 2003);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes("YOUR-API-KEY.foo.java-tcp 1.2\n");
conn.close();
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;

...
DatagramSocket sock   = new DatagramSocket();
InetAddress addr      = InetAddress.getByName("YOUR-UID.carbon.hostedgraphite.com");
byte[] message        = "YOUR-API-KEY.foo.java-udp 1.2\n".getBytes();
DatagramPacket packet = new DatagramPacket(message, message.length, addr, 2003);
sock.send(packet);
sock.close();
import java.util.Base64;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;

// Assumes the reader will try/catch the appropriate exceptions, and clean up
// The connection when they're done with it!

String data = "foo.java-http 1.2";
URL url     = new URL("https://www.hostedgraphite.com/api/v1/sink");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();

String key        = "YOUR_API_KEY";
String authHeader = Base64.getEncoder().encodeToString(key.getBytes());

connection.setRequestProperty("Authorization", "Basic " + authHeader);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length));
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);

OutputStream wr = connection.getOutputStream();
wr.write(data.getBytes("UTF-8"));
wr.flush();
wr.close();
URLFetchService fetcher= URLFetchServiceFactory.getURLFetchService();
String data = "foo 1.2";
String key  = "YOUR_API_KEY";

String authHeaderString = "Basic " + Base64.encodeBase64String(key).getBytes("ISO-8859-1");
HTTPHeader authHeader   = new HTTPHeader("Authorization", authHeaderString);

HTTPRequest request = new HTTPRequest("https://www.hostedgraphite.com/api/v1/sink", HTTPMethod.POST);
request.getFetchOptions().setDeadline(10);
request.setHeader(authHeader);

request.setPayload(data.getBytes());
HTTPResponse response = fetcher.fetch(request);

Your API key can be found on your page.

account home
http://metrics.dropwizard.io/
Interesting Java Metrics Libraries
Java DNS Behaviour
Sending a metric via TCP
Sending a metric via UDP
Sending a metric via HTTP POST
Google App Engine HTTP Post
Interesting Java Metrics Libraries
Java DNS Behaviour
Sending a metric via TCP
Sending a metric via UDP
Sending a metric via HTTP POST
Google App Engine HTTP Post