Skip to content

Commit

Permalink
Merge pull request #3 from percolate/graphite
Browse files Browse the repository at this point in the history
Adding graphite support
  • Loading branch information
brentwatson committed Apr 23, 2015
2 parents 881a3ec + 0e81939 commit d8e8c97
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
String mixpanel() default "";
String googleAnalytics() default "";
String flurry() default "";
String graphite() default "";

}
3 changes: 3 additions & 0 deletions Foam/foam/src/main/java/com/percolate/foam/FoamMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected void init(FoamApiKeys foamApiKeys) {
services.put(ServiceType.MIXPANEL, new Mixpanel(context));
services.put(ServiceType.FLURRY, new Flurry(context));
services.put(ServiceType.GOOGLE_ANALYTICS, new GoogleAnalytics(context));
services.put(ServiceType.GRAPHITE, new Graphite(context));
}

/**
Expand Down Expand Up @@ -81,6 +82,8 @@ private void initializeServices() {
if (((Flurry) service).checkForJar()) {
apiKey = foamApiKeys.flurry();
}
} else if(serviceType == ServiceType.GRAPHITE){
apiKey = foamApiKeys.graphite();
}

if(Utils.isNotBlank(apiKey)){
Expand Down
119 changes: 119 additions & 0 deletions Foam/foam/src/main/java/com/percolate/foam/Graphite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.percolate.foam;

import android.content.Context;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
* {@inheritDoc}
*/
class Graphite extends ServiceImpl implements EventTrackingService {

private String host;
private int port;
private String apiKey;

Graphite(Context context) {
super(context);
}

/**
* {@inheritDoc}
*/
@Override
public void enable(String url) {
if(url.contains(":") && url.split(":").length == 2) {
String first = url.split(":")[0];
String second = url.split(":")[1];

if (first.contains("@")) {
this.apiKey = first.split("@")[0];
this.host = first.split("@")[1];
} else {
this.host = url.split(":")[0];
}

try {
this.port = Integer.parseInt(second);
} catch (NumberFormatException ex) {
Utils.logIssue("Invalid port in Graphite URL [" + second + "]", ex);
}
} else {
Utils.logIssue("Invalid Graphite URL. Expecting \"[key@]host:port\" format.", null);
}
}

/**
* {@inheritDoc}
*/
@Override
public boolean isEnabled() {
return host != null && port != 0;
}

/**
* {@inheritDoc}
*/
@Override
public ServiceType getServiceType() {
return ServiceType.GRAPHITE;
}

/**
* Send data to graphite in the format "metric_path value timestamp\n".
* Example: "com.myapp.activities.MainActivity 1 1429801333\n"
* ApiKey will be appended to the metric_path as expected by hosted providers.
*
* {@inheritDoc}
*/
@Override
public void logEvent(Context context, String event) {
StringBuilder eventData = new StringBuilder();
if(Utils.isNotBlank(apiKey)){
eventData.append(apiKey);
eventData.append(".");
}
eventData.append(Utils.getApplicationPackageName(context));
eventData.append(".");
eventData.append(event);
eventData.append(" 1 ");
eventData.append(System.currentTimeMillis() / 1000L);
eventData.append("\n");

sendData(eventData.toString());
}

/**
* Send data to Graphite server.
* Data is sent over TCP in a new thread.
*
* @param graphiteEvent Event data to send to graphite.
*/
private void sendData(final String graphiteEvent) {
new Thread(new Runnable() {
@Override
public void run() {
Socket conn = null;
try {
conn = new Socket(host, port);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(graphiteEvent);
dos.flush();
} catch (IOException ex) {
Utils.logIssue("Error sending graphite event [" + graphiteEvent + "].", ex);
} finally {
if(conn != null) {
try {
conn.close();
} catch (IOException ex) {
Utils.logIssue("Could not close graphite socket.", ex);
}
}
}
}
}).start();
}

}
3 changes: 2 additions & 1 deletion Foam/foam/src/main/java/com/percolate/foam/ServiceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ enum ServiceType {
MIXPANEL,
GOOGLE_ANALYTICS,
FLURRY,
LOGENTRIES
LOGENTRIES,
GRAPHITE
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You add your API Keys, we'll do the rest.
| PagerDuty | Flurry | Papertrail |
| Papertrail | Mixpanel | Logentries |
| HockeyApp | Google Analytics | |
| Flurry | | |
| Flurry | Graphite | |


**Crash Reporting:** Unhandled exceptions will be reported.
Expand All @@ -54,6 +54,7 @@ You add your API Keys, we'll do the rest.
mixpanel = "221b331c441d551e661f771g881h991i",
googleAnalytics = "UA-00000000-1",
logentries = "data.logentries.com:12345",
graphite = "[api-key@]graphite.myhost.com:2003"
)
public class MyApplication extends FoamApplication {
}
Expand Down Expand Up @@ -96,6 +97,8 @@ That's it. You've just enabled all of these services for your application. Wel

**Flurry**: Create an application in Flurry then add your application key. _(FlurryAnalytics-x.x.x.jar must be added manually to your project {TODO document in wiki})_

**Graphite**: Provided by the maintainer of your graphite host.

## Notes:

- It takes time for some services to process incoming data. There may be a delay of few hours before anything shows up. Be patient.
Expand Down

0 comments on commit d8e8c97

Please sign in to comment.