ブラウザで js アプリケーションを実行する際に問題が発生しました。クライアント サーバー アプリケーションを作成し、Zipkinを使用してそれらの間の通信を追跡しています。
これは Node.js を使用するクライアントですrequire()
:
const {Tracer, BatchRecorder, ExplicitContext} = require('zipkin');
const {HttpLogger} = require('zipkin-transport-http');
const {restInterceptor} = require('zipkin-instrumentation-cujojs-rest');
const rest = require('rest');
const ctxImpl = new ExplicitContext();
const recorder = new BatchRecorder({
logger: new HttpLogger({
endpoint: 'http://localhost:9411/api/v1/spans'
})
});
const tracer = new Tracer({ctxImpl, recorder});
const nameOfClient = 'cujojs-client';
const client = rest.wrap(restInterceptor, {tracer, serviceName: nameOfClient});
client({
method: 'GET',
path: 'http://localhost:9999/request'
}).then(function(response) {
console.log('response: ', response);
});
Browserify を使用して、ブラウザーで使用する依存関係をバンドルしています。
browserify CujojsClient.js -o bundle.js
ブラウザのバンドルを作成するために実行します。
オプションを指定して browserify を実行する--node --ignore-missing
と、Node.js ではすべて正常に動作しますが、バンドルをブラウザー (Windows では Firefox 45.3.0) で実行すると、次のような結果しか得られませんでした。
SyntaxError: missing : after property id
これは問題のある部分です:
class HttpLogger {
constructor({endpoint, httpInterval = 1000}) {
this.endpoint = endpoint;
this.queue = [];
const timer = setInterval(() => {
this.processQueue();
}, httpInterval);
if (timer.unref) { // unref might not be available in browsers
timer.unref(); // Allows Node to terminate instead of blocking on timer
}
}
logSpan(span) {
this.queue.push(span.toJSON());
}
processQueue() {
if (this.queue.length > 0) {
const postBody = JSON.stringify(this.queue);
fetch(this.endpoint, {
method: 'POST',
body: postBody,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
}).then((response) => {
if (response.status !== 202) {
console.error('Unexpected response while sending Zipkin data, status:' +
`${response.status}, body: ${postBody}`);
}
this.queue.length = 0;
}).catch((error) => {
console.error('Error sending Zipkin data', error);
this.queue.length = 0;
});
}
}
}
私のindex.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="bundle.js"></script>
</head>
</html>