Skip to content

Commit

Permalink
Add support for User-Agent additions (#19)
Browse files Browse the repository at this point in the history
Follow libhoney-go's lead here and add a special configuration option that lets the user put additional info in the User-Agent string.

For the go sdk we use this to tag integrations, and the same will hold true here.

Also, it's about time we flipped out of beta so people can start getting updates via semver as opposed to explicitly updating to a new beta.
  • Loading branch information
toshok committed Feb 9, 2018
1 parent c6fd5dc commit d303950
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libhoney",
"version": "1.0.0-beta.10",
"version": "1.0.0",
"description": "Javascript library for sending data to Honeycomb",
"bugs": "https://github.com/honeycombio/libhoney-js/issues",
"repository": {
Expand Down
5 changes: 4 additions & 1 deletion src/libhoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ const defaults = Object.freeze({
maxResponseQueueSize: 1000,

// if this is set to true, all sending is disabled. useful for disabling libhoney when testing
disabled: false
disabled: false,

// If this is non-empty, append it to the end of the User-Agent header.
userAgentAddition: ""
});

/**
Expand Down
12 changes: 10 additions & 2 deletions src/transmission.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import superagent from 'superagent';
import urljoin from 'urljoin';

const userAgent = "libhoney-js/LIBHONEY_JS_VERSION";
const USER_AGENT = "libhoney-js/LIBHONEY_JS_VERSION";

const _global = (typeof window !== "undefined" ? window :
typeof global !== "undefined" ? global : undefined);
Expand Down Expand Up @@ -151,6 +151,8 @@ export class Transmission {
this._pendingWorkCapacity = options.pendingWorkCapacity;
}

this._userAgentAddition = options.userAgentAddition || "";

// Included for testing; to stub out randomness and verify that an event
// was dropped.
this._randomFn = Math.random;
Expand Down Expand Up @@ -201,7 +203,7 @@ export class Transmission {

let batchAgg = new BatchEndpointAggregator(batch);

let finishBatch = () => {
const finishBatch = () => {
this._batchCount--;

let queueLength = this._eventQueue.length;
Expand Down Expand Up @@ -232,6 +234,12 @@ export class Transmission {
return;
}

let userAgent = USER_AGENT;
let trimmedAddition = this._userAgentAddition.trim();
if (trimmedAddition) {
userAgent = `${USER_AGENT} ${trimmedAddition}`;
}

var start = Date.now();
req
.set('X-Hny-Team', batch.writeKey)
Expand Down
44 changes: 44 additions & 0 deletions test/transmission_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,48 @@ describe('transmission', function() {
}));
}
});

it('should allow user-agent additions', function(done) {
var responseCount = 0;
var responseExpected = 2;

var UAs = [
{ dataset: "test-transmission1", addition: "", probe: (ua) => ua.indexOf("libhoney") === 0 && ua.indexOf("addition") === -1 },
{ dataset: "test-transmission2", addition: "user-agent addition", probe: (ua) => ua.indexOf("libhoney") === 0 && ua.indexOf("addition") !== -1 },
];

// set up our endpoints
UAs.forEach(ua =>
mock.post(`http://localhost:9999/1/batch/${ua.dataset}`, function(req) {
if (!ua.probe(req.headers["user-agent"])) {
done(new Error("unexpected user-agent addition"));
}
return {};
})
);

// now send our events through separate transmissions with different UA additions
UAs.forEach(ua => {
var transmission = new Transmission({
batchSizeTrigger: 1, // so we'll send individual events
responseCallback (queue) {
let responses = queue.splice(0, queue.length);
responseCount += responses.length;
if (responseCount === responseExpected) {
done();
}
},
userAgentAddition: ua.addition
});

transmission.sendPresampledEvent(new ValidatedEvent({
apiHost: "http://localhost:9999",
writeKey: "123456789",
dataset: ua.dataset,
sampleRate: 1,
timestamp: new Date(),
postData: JSON.stringify({ a: 1, b: 2 })
}));
});
});
});

0 comments on commit d303950

Please sign in to comment.