Skip to content

Commit

Permalink
Merge pull request Jigsaw-Code#224 from Jigsaw-Code/bemasc-httpmetric
Browse files Browse the repository at this point in the history
Report Firebase Performance metrics for Go-DOH
  • Loading branch information
Benjamin M. Schwartz authored Oct 30, 2019
2 parents 631613a + 862a4c5 commit a8adae3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
43 changes: 38 additions & 5 deletions Android/app/src/main/java/app/intra/net/go/GoIntraListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@
*/
package app.intra.net.go;

import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import androidx.collection.LongSparseArray;
import app.intra.net.dns.DnsPacket;
import app.intra.net.doh.Transaction;
import app.intra.net.doh.Transaction.Status;
import app.intra.sys.IntraVpnService;
import app.intra.sys.Names;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.perf.FirebasePerformance;
import com.google.firebase.perf.metrics.HttpMetric;
import doh.Doh;
import doh.Token;
import intra.TCPSocketSummary;
import intra.UDPSocketSummary;
import java.net.ProtocolException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import split.RetryStats;

/**
Expand Down Expand Up @@ -106,7 +107,7 @@ public void onUDPSocketClosed(UDPSocketSummary summary) {
FirebaseAnalytics.getInstance(vpnService).logEvent(Names.UDP.name(), event);
}

private static final Map<Long, Status> goStatusMap = new HashMap<>();
private static final LongSparseArray<Status> goStatusMap = new LongSparseArray<>();
static {
goStatusMap.put(Doh.Complete, Status.COMPLETE);
goStatusMap.put(Doh.SendFailed, Status.SEND_FAIL);
Expand All @@ -116,8 +117,40 @@ public void onUDPSocketClosed(UDPSocketSummary summary) {
goStatusMap.put(Doh.InternalError, Status.INTERNAL_ERROR);
}

// Wrapping HttpMetric into a doh.Token allows us to get paired query and response notifications
// from Go without reverse-binding any Java APIs into Go. Pairing these notifications is
// required by the structure of the HttpMetric API (which does not have any other way to record
// latency), and reverse binding is worth avoiding, especially because it's not compatible with
// the Go module system (https://github.com/golang/go/issues/27234).
private class Metric implements doh.Token {
final HttpMetric metric;
Metric(String url) {
metric = FirebasePerformance.getInstance().newHttpMetric(url, "POST");
}
}

@Override
public Token onQuery(String url) {
Metric m = new Metric(url);
m.metric.start();
return m;
}

private static int len(byte[] a) {
return a != null ? a.length : 0;
}

@Override
public void onTransaction(doh.Summary summary) {
public void onResponse(Token token, doh.Summary summary) {
if (summary.getHTTPStatus() != 0 && token != null) {
// HTTP transaction completed. Report performance metrics.
Metric m = (Metric)token;
m.metric.setRequestPayloadSize(len(summary.getQuery()));
m.metric.setHttpResponseCode((int)summary.getHTTPStatus());
m.metric.setResponsePayloadSize(len(summary.getResponse()));
m.metric.stop(); // Finalizes the metric and queues it for upload.
}

final DnsPacket query;
try {
query = new DnsPacket(summary.getQuery());
Expand Down
2 changes: 1 addition & 1 deletion Android/tun2socks/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
This copy of tun2socks.aar is built from https://github.com/Jigsaw-Code/outline-go-tun2socks, at
commit 3458ce50676b08e972bca7057ad646ef0e75dcfd. It is used here under the Apache 2.0 license.
commit ec71f9af57c4223425bff49ccc8a1d54f5345cc5. It is used here under the Apache 2.0 license.
Binary file modified Android/tun2socks/tun2socks.aar
Binary file not shown.

0 comments on commit a8adae3

Please sign in to comment.