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

Merge Chrome and Firefox implementations #19

Merged
merged 5 commits into from
Sep 4, 2017
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
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

63 changes: 0 additions & 63 deletions gulpfile.js

This file was deleted.

52 changes: 31 additions & 21 deletions js/ZipkinPlugin.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import attachBeforeSendHeadersListeners from './attachBeforeSendHeadersListener';
import checkZipkinUI from './checkZipkinUI';

export default class ZipkinPlugin {
constructor({pubsub, storage, setInterval, clearInterval, XMLHttpRequest /* network */}) {
constructor({ pubsub, storage, setInterval, clearInterval /* network */ }) {
this.pubsub = pubsub;
this.storage = storage;
this.setInterval = setInterval;
this.clearInterval = clearInterval;
this.XMLHttpRequest = XMLHttpRequest;
this.zipkinUrls = [];

this.pubsub.sub('zipkinUrls.load', this.loadFromStorage.bind(this));
this.pubsub.sub('zipkinUrls.add', this.addZipkinUrl.bind(this));
this.pubsub.sub('zipkinUrls.remove', this.removeZipkinUrl.bind(this));
}

loadFromStorage() {
this.storage.get('zipkinUrls', []).then(zipkinUrls => {
zipkinUrls.forEach(zipkinUrl => {
const exists = this.zipkinUrls.find(z => z.url === zipkinUrl.url);
if (!exists) {
this.addZipkinUrl(zipkinUrl.url, false);
}
});
}).then(this.publishZipkinUrlStatus.bind(this));
async loadFromStorage() {
const zipkinUrls = await this.storage.get('zipkinUrls', []);

await Promise.all(zipkinUrls.map(zipkinUrl => {
const exists = this.zipkinUrls.find(z => z.url === zipkinUrl.url);
if (!exists) {
return this.addZipkinUrl(zipkinUrl.url, false);
}
})
.filter(Boolean));

this.publishZipkinUrlStatus();
}

publishZipkinUrlStatus() {
Expand All @@ -40,15 +41,22 @@ export default class ZipkinPlugin {
}

saveZipkinUrls() {
this.storage.set('zipkinUrls', this.zipkinUrls.map(z => ({url: z.url})));
return this.storage.set('zipkinUrls',
this.zipkinUrls
.map(url => new URL(url.url))
// Don't save the path part of the URL
.map(url => ({ url: url.origin }))
);
}

async addZipkinUrl(url, saveToStorage = true) {
// Fetch the endpoint to check if we get redirected
const fetchUrl = await fetch(url);

// Never actually download the body
await fetchUrl.body.cancel();
if (fetchUrl.body) {
// Never actually download the body
await fetchUrl.body.cancel();
}

if (fetchUrl.redirected && fetchUrl.url === `${url}/zipkin/`) {
url = `${url}/zipkin`
Expand All @@ -75,12 +83,14 @@ export default class ZipkinPlugin {
}

makeZipkinCheckInterval(url) {
const poll = () => {
checkZipkinUI(this.XMLHttpRequest, url).then(response => {
this.setZipkinUIStatus(url, 'up', response.instrumented);
}).catch(err => {
this.setZipkinUIStatus(url, err.status ? err.status : 'down', []);
});
const poll = async () => {
try {
const instrumented = await checkZipkinUI(url);

this.setZipkinUIStatus(url, 'up', instrumented);
} catch (err) {
this.setZipkinUIStatus(url, err.message || 'down', []);
}
};
poll();
return this.setInterval(poll, 30000);
Expand Down
3 changes: 2 additions & 1 deletion js/addNetworkEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export default function addNetworkEvents(network, pubsub) {
network.onNavigated.addListener(() => {
pubsub.pub('navigated');
});
network.onRequestFinished.addListener(request => {
// https://bugzilla.mozilla.org/show_bug.cgi?id=1311171
network.onRequestFinished && network.onRequestFinished.addListener(request => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity, this is why FF doesn't work yet

pubsub.pub('requestFinished', {
headers: request.request.headers,
url: request.request.url
Expand Down
9 changes: 3 additions & 6 deletions js/attachBeforeSendHeadersListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const filter = {
};

export default function attachBeforeSendHeadersListener(webRequest) {
webRequest.onBeforeSendHeaders.addListener(details => {
webRequest.onBeforeSendHeaders.addListener(({requestHeaders = []}) => {
const traceId = generateZipkinTraceId();
const zipkinHeaders = {
'X-Zipkin-Extension': '1',
Expand All @@ -24,11 +24,8 @@ export default function attachBeforeSendHeadersListener(webRequest) {

return {
requestHeaders: [
...(details.requestHeaders || []),
...Object.keys(zipkinHeaders).map(key => ({
name: key,
value: zipkinHeaders[key]
}))
...requestHeaders,
...Object.entries(zipkinHeaders).map(([name, value]) => ({ name, value }))
]
};
}, filter, ['blocking', 'requestHeaders']);
Expand Down
35 changes: 12 additions & 23 deletions js/checkZipkinUI.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import makeRequest from './request';

export default function checkZipkinUI(XMLHttpRequest, url) {
export default async function checkZipkinUI(url) {
const fetchUrl = url + '/config.json';
return makeRequest(XMLHttpRequest, fetchUrl).then(xhr => {
if (xhr.status !== 200) {
return Promise.reject({status: 'error ' + xhr.status});
} else {
const contentType = xhr.getResponseHeader('content-type');
if (contentType.indexOf('application/json') === -1) {
return Promise.reject({status: 'invalid response type ' + contentType});
} else {
try {
return JSON.parse(xhr.responseText);
} catch (parseError) {
return Promise.reject({status: 'invalid response (JSON parse error)'});
}
}
}
}).then(data => {
return {instrumented: data.instrumented || '.*'};
}).catch(err => {
return Promise.reject({error: err});
});

// Used to identity calls originating from within the plugin itself
const res = await fetch(fetchUrl, { headers: { 'X-Zipkin-Extension': '1' } });

if (!res.ok) {
throw new Error(`Invalid status: ${res.status}.`);
}

const data = await res.json();

return data.instrumented || '.*';
}
19 changes: 0 additions & 19 deletions js/request.js

This file was deleted.

12 changes: 8 additions & 4 deletions vendor/chrome/manifest.json → manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"manifest_version": 2,
"name": "Zipkin Chrome Extension",
"description": "Lets you trigger Zipkin traces from the browser, while using your application",
"version": "{{ version }}",
"name": "Zipkin Browser Extension",
"devtools_page": "devtools.html",
"background": {
"scripts": ["background.bundle.js"],
"scripts": [
"browser-polyfill.js",
"background.bundle.js"
],
"persistent": true
},
"icons": {
"48": "icon64.png"
Copy link
Contributor Author

@SimenB SimenB Sep 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows an icon, so sorta #2. Would like that SVG though

},
"permissions": [
"activeTab",
"webRequest",
Expand Down
Loading