Skip to content

Commit 247233d

Browse files
author
eirslett
committed
Initial draft of a chrome extension for zipkin
1 parent 7a4eabb commit 247233d

23 files changed

+600
-0
lines changed

.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015"],
3+
"plugins": ["transform-object-rest-spread", "transform-react-jsx"]
4+
}

.editorconfig

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[*.js]
2+
end_of_line = lf
3+
insert_final_newline = true
4+
indent_style = space
5+
indent_size = 2
6+
trim_trailing_whitespace = true

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
dist
2+
node_modules

LICENSE

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Apache License
2+
3+
Version 2.0, January 2004
4+
5+
http://www.apache.org/licenses/
6+
7+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8+
9+
1. Definitions.
10+
11+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
12+
13+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
14+
15+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
16+
17+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
18+
19+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
20+
21+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
22+
23+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
24+
25+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
26+
27+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
28+
29+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
30+
31+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
32+
33+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
34+
35+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
36+
37+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
38+
You must cause any modified files to carry prominent notices stating that You changed the files; and
39+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
40+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
41+
42+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
43+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
44+
45+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
46+
47+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
48+
49+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
50+
51+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
52+
53+
END OF TERMS AND CONDITIONS

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ This is an extension to the Chrome browser that lets you trigger trace generatio
66

77
TODO: This extension will later be submitted to Chrome Web Store, where it will be available for free.
88

9+
## Usage
10+
11+
12+
913
## Developing
1014

1115
- git clone [repo url] into [working directory]
@@ -16,5 +20,7 @@ TODO: This extension will later be submitted to Chrome Web Store, where it will
1620

1721
## Building/publishing
1822

23+
- npm install
1924
- npm run build
2025

26+
This will build a .zip file that should be uploaded to Chrome Web Store.

build.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/env/node
2+
const webpack = require('webpack');
3+
const JSZip = require('jszip');
4+
const fs = require('fs');
5+
const webpackConfig = Object.assign(
6+
{},
7+
require('./webpack.config.js'), {
8+
bail: true
9+
}
10+
);
11+
12+
console.log('Running Webpack...');
13+
webpack(webpackConfig, (err, stats) => {
14+
if (err) {
15+
console.log('Webpack error', err.message);
16+
process.exit(1);
17+
} else {
18+
console.log('Webpack run OK');
19+
console.log('Generating zip...');
20+
const zip = new JSZip();
21+
[
22+
'devtools.html',
23+
'manifest.json',
24+
'panel.html',
25+
'zipkin.png',
26+
'dist/background.bundle.js',
27+
'dist/devtools.bundle.js',
28+
'dist/panel.bundle.js'
29+
].forEach(f => zip.file(f, fs.readFileSync(f)));
30+
fs.writeFileSync('dist/zipkin-chrome-extension.zip', zip.generate({type: 'nodebuffer'}));
31+
console.log('Zip was generated.');
32+
// zip.folder('dist');
33+
34+
}
35+
});

devtools.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Zipkin Chrome Extension</title>
6+
</head>
7+
<body>
8+
<script src="dist/devtools.bundle.js"></script>
9+
</body>
10+
</html>

docs/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Usage
2+
=====
3+
4+
1) Install the extension, either via Chrome Web Store, or build it manually from source.
5+
6+
2) Open Chrome and go to a web site, right-clict -> Inspect. This will open Chrome developer tools.
7+
8+
3) A panel appears, called Zipkin. Choose it.
9+
10+
4) Here, you'll need to configure the URL to your Zipkin UI/query server.
11+
12+
Once you've provided a URL, Chrome will attempt to contact that server, and "auto-discover" its configuration.
13+
You can configure multiple Zipkin endpoints if you want to; e.g. one for your development environment, and one for production.
14+
In this example case, we specified there's a Zipkin UI running on http://localhost:9411, which probably isn't what you will have in an actual environment; only if you're running the Zipkin server locally. Usually, it would be set to something like http://zipkin.mysite.com/.
15+
16+
5) Use your application and generate traces
17+
While you navigate around, Chrome will add Zipkin trace headers to all HTTP requests. In the Zipkin extension, you can
18+
click on a generated trace ID to take you directly to the Zipkin UI, displaying that trace.

docs/add-zipkin-endpoint.png

11.2 KB
Loading

docs/opening-1.png

9.15 KB
Loading

docs/opening-2.png

10.1 KB
Loading

docs/show-traces.png

27.3 KB
Loading

manifest.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"manifest_version": 2,
3+
"name": "Zipkin Chrome Extension",
4+
"description": "Lets you trigger Zipkin traces from the browser, while using your application",
5+
"version": "1.0",
6+
"devtools_page": "devtools.html",
7+
"background": {
8+
"scripts": ["dist/background.bundle.js"],
9+
"persistent": true
10+
},
11+
"permissions": [
12+
"activeTab",
13+
"webRequest",
14+
"webRequestBlocking",
15+
"storage",
16+
"http://*/",
17+
"https://*/"
18+
]
19+
}

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "zipkin-chrome-extension",
3+
"version": "1.0.0",
4+
"description": "Lets you trigger Zipkin traces from the browser, while using your application",
5+
"scripts": {
6+
"dev": "node node_modules/webpack/bin/webpack.js --watch",
7+
"build": "node build.js"
8+
},
9+
"author": "OpenZipkin",
10+
"license": "Apache 2",
11+
"devDependencies": {
12+
"babel-core": "^6.7.4",
13+
"babel-loader": "^6.2.4",
14+
"babel-plugin-transform-object-rest-spread": "^6.6.5",
15+
"babel-plugin-transform-react-jsx": "^6.7.4",
16+
"babel-preset-es2015": "^6.6.0",
17+
"express": "^4.13.4",
18+
"extract-text-webpack-plugin": "^1.0.1",
19+
"file-loader": "^0.8.5",
20+
"jszip": "^2.6.0",
21+
"node-zip": "^1.1.1",
22+
"webpack": "^1.12.14"
23+
},
24+
"dependencies": {
25+
"minimatch": "^3.0.0",
26+
"random-js": "^1.0.8",
27+
"react": "^0.14.8",
28+
"react-dom": "^0.14.8"
29+
}
30+
}

panel.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>Zipkin Chrome Extension</title>
6+
</head>
7+
<body>
8+
<div id="content"></div>
9+
<script src="dist/panel.bundle.js"></script>
10+
</body>
11+
</html>

src/background.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import Random from 'random-js';
2+
import makeRequest from './request';
3+
4+
const random = new Random();
5+
function generateZipkinTraceId() {
6+
return random.hex(32);
7+
}
8+
9+
const filter = {
10+
urls: ['*://*/*']
11+
};
12+
13+
chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
14+
const traceId = generateZipkinTraceId();
15+
const zipkinHeaders = {
16+
'X-Zipkin-Extension': '1',
17+
'X-B3-Flags': '1',
18+
'X-B3-TraceId': traceId,
19+
'X-B3-SpanId': traceId
20+
};
21+
22+
const modified = {
23+
requestHeaders: [
24+
...(details.requestHeaders || []),
25+
...Object.keys(zipkinHeaders).map(key => ({
26+
name: key,
27+
value: zipkinHeaders[key]
28+
}))
29+
]
30+
};
31+
return modified;
32+
}, filter, ['blocking', 'requestHeaders']);
33+
34+
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
35+
if (request.type === 'checkZipkinUI') {
36+
const fetchUrl = request.zipkinUrl + '/config.json';
37+
makeRequest(fetchUrl).then(xhr => {
38+
if (xhr.status !== 200) {
39+
return Promise.reject({status: 'error ' + xhr.status});
40+
} else {
41+
const contentType = xhr.getResponseHeader('content-type');
42+
if (contentType.indexOf('application/json') === -1) {
43+
return Promise.reject({status: 'invalid response type ' + contentType});
44+
} else {
45+
try {
46+
return JSON.parse(xhr.responseText);
47+
} catch (parseError) {
48+
return Promise.reject({status: 'invalid response (JSON parse error)'});
49+
}
50+
}
51+
}
52+
}).then(data => {
53+
const r = {instrumented: data.instrumented || ['**']};
54+
sendResponse(r);
55+
}).catch(err => {
56+
const r = {error: err};
57+
sendResponse(r);
58+
});
59+
return true;
60+
} else {
61+
sendResponse({error: `Invalid message type "${request.type}"`});
62+
}
63+
});

src/devtools.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const panel = chrome.devtools.panels.create('Zipkin',
2+
'zipkin.png',
3+
'panel.html',
4+
panel => {
5+
}
6+
);

src/panel.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React, {Component, PropTypes} from 'react';
2+
import {render} from 'react-dom';
3+
import minimatch from 'minimatch';
4+
import ZipkinUI from './zipkinUI';
5+
6+
class ZipkinPanel extends Component {
7+
constructor(props) {
8+
super(props);
9+
this.state = {
10+
requests: [],
11+
zipkinUrls: []
12+
};
13+
}
14+
15+
componentDidMount() {
16+
chrome.devtools.network.onNavigated.addListener(() => {
17+
this.setState({requests: []});
18+
});
19+
20+
chrome.devtools.network.onRequestFinished.addListener(request => {
21+
// alert(JSON.stringify(request.request));
22+
const [traceId] = request.request.headers.filter(h => h.name === 'X-B3-TraceId');
23+
24+
if (traceId) {
25+
this.setState({
26+
requests: [...this.state.requests, {
27+
traceId: traceId.value,
28+
url: request.request.url
29+
}]
30+
});
31+
}
32+
});
33+
}
34+
35+
matches(url, matcher) {
36+
console.log('checking for url '+url+' and matcher '+matcher);
37+
const result = minimatch(url, matcher);
38+
console.log('was it match?', result);
39+
return result;
40+
}
41+
42+
traceLink(traceId, requestUrl) {
43+
console.log('matching traceID '+traceId + 'and requestUrl '+requestUrl+' for uis', this.state.zipkinUrls);
44+
const url = this.state.zipkinUrls.find(url =>
45+
url.instrumented.find(matcher => this.matches(requestUrl, matcher)) != null
46+
);
47+
if (url == null) {
48+
return null;
49+
} else {
50+
return `${url.name}/traces/${encodeURIComponent(traceId)}`;
51+
}
52+
}
53+
54+
handleZipkinUrlsChange(value) {
55+
this.setState({
56+
zipkinUrls: value
57+
});
58+
}
59+
60+
render() {
61+
const alignLeft = {textAlign: 'left', verticalAlign: 'top'};
62+
return (
63+
<div className="container">
64+
<div className="row">
65+
<div className="col-md-12">
66+
<h2>Zipkin traces</h2>
67+
<ZipkinUI onZipkinUrlsChange={this.handleZipkinUrlsChange.bind(this)} />
68+
<table>
69+
<thead>
70+
<tr>
71+
<th style={alignLeft}>Trace</th>
72+
<th style={alignLeft}>Request</th>
73+
</tr>
74+
</thead>
75+
<tbody>
76+
{this.state.requests.length > 0 ? this.state.requests.map(request => (
77+
<tr key={request.traceId}>
78+
<td style={alignLeft}><a target="blank" href={this.traceLink(request.traceId, request.url)}>{request.traceId}</a></td>
79+
<td style={alignLeft}>{request.url}</td>
80+
</tr>
81+
)
82+
) :
83+
<tr><td colSpan="2">Recording network activity... Perform a request or hit F5 to record the reload.</td></tr>}
84+
</tbody>
85+
</table>
86+
</div>
87+
</div>
88+
</div>
89+
);
90+
}
91+
}
92+
93+
render(<ZipkinPanel />, document.getElementById('content'));

src/request.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function request(url) {
2+
return new Promise((resolve, reject) => {
3+
var xhr = new XMLHttpRequest();
4+
xhr.open('GET', url, true);
5+
xhr.onreadystatechange = function() {
6+
if (xhr.readyState == 4) {
7+
resolve(xhr);
8+
}
9+
};
10+
xhr.onerror = function(error) {
11+
reject(error);
12+
};
13+
xhr.send();
14+
});
15+
}

0 commit comments

Comments
 (0)