Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding graphite support #3

Merged
merged 1 commit into from
Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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