-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactoring zipkin to be able to use it in web (#1399)
- Loading branch information
Showing
21 changed files
with
652 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Zipkin Exporter Example</title> | ||
<base href="/"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
|
||
<body> | ||
Example of using Web Tracer with Zipkin Exporter | ||
<script type="text/javascript" src="zipkin.js"></script> | ||
<br/> | ||
<button id="button1">Test WebTracer with Zipkin</button> | ||
|
||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing'; | ||
import { WebTracerProvider } from '@opentelemetry/web'; | ||
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin'; | ||
|
||
const provider = new WebTracerProvider(); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter())); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter())); | ||
|
||
provider.register(); | ||
|
||
const tracer = provider.getTracer('example-tracer-web'); | ||
|
||
const prepareClickEvent = () => { | ||
const element = document.getElementById('button1'); | ||
|
||
const onClick = () => { | ||
const span = tracer.startSpan('foo'); | ||
span.end(); | ||
}; | ||
element.addEventListener('click', onClick); | ||
}; | ||
|
||
window.addEventListener('load', prepareClickEvent); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
module.exports = { | ||
"env": { | ||
"mocha": true, | ||
"node": true | ||
"commonjs": true, | ||
"node": true, | ||
"browser": true | ||
}, | ||
...require('../../eslint.config.js') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/*! | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const karmaWebpackConfig = require('../../karma.webpack'); | ||
const karmaBaseConfig = require('../../karma.base'); | ||
|
||
module.exports = (config) => { | ||
config.set(Object.assign({}, karmaBaseConfig, { | ||
webpack: karmaWebpackConfig, | ||
files: ['test/browser/index-webpack.ts'], | ||
preprocessors: { 'test/browser/index-webpack.ts': ['webpack'] } | ||
})) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
*/ | ||
|
||
export * from './zipkin'; | ||
export * from './platform'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
packages/opentelemetry-exporter-zipkin/src/platform/browser/util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as api from '@opentelemetry/api'; | ||
import { ExportResult } from '@opentelemetry/core'; | ||
import * as zipkinTypes from '../../types'; | ||
import { OT_REQUEST_HEADER } from '../../utils'; | ||
|
||
/** | ||
* Prepares send function that will send spans to the remote Zipkin service. | ||
*/ | ||
export function prepareSend( | ||
logger: api.Logger, | ||
urlStr: string, | ||
headers?: Record<string, string> | ||
) { | ||
let xhrHeaders: Record<string, string>; | ||
const useBeacon = navigator.sendBeacon && !headers; | ||
if (headers) { | ||
xhrHeaders = { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
[OT_REQUEST_HEADER]: '1', | ||
...headers, | ||
}; | ||
} | ||
|
||
/** | ||
* Send spans to the remote Zipkin service. | ||
*/ | ||
return function send( | ||
zipkinSpans: zipkinTypes.Span[], | ||
done: (result: ExportResult) => void | ||
) { | ||
if (zipkinSpans.length === 0) { | ||
logger.debug('Zipkin send with empty spans'); | ||
return done(ExportResult.SUCCESS); | ||
} | ||
const payload = JSON.stringify(zipkinSpans); | ||
if (useBeacon) { | ||
sendWithBeacon(payload, done, urlStr, logger); | ||
} else { | ||
sendWithXhr(payload, done, urlStr, logger, xhrHeaders); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Sends data using beacon | ||
* @param data | ||
* @param done | ||
* @param urlStr | ||
* @param logger | ||
*/ | ||
function sendWithBeacon( | ||
data: string, | ||
done: (result: ExportResult) => void, | ||
urlStr: string, | ||
logger: api.Logger | ||
) { | ||
if (navigator.sendBeacon(urlStr, data)) { | ||
logger.debug('sendBeacon - can send', data); | ||
done(ExportResult.SUCCESS); | ||
} else { | ||
logger.error('sendBeacon - cannot send', data); | ||
done(ExportResult.FAILED_NOT_RETRYABLE); | ||
} | ||
} | ||
|
||
/** | ||
* Sends data using XMLHttpRequest | ||
* @param data | ||
* @param done | ||
* @param urlStr | ||
* @param logger | ||
* @param xhrHeaders | ||
*/ | ||
function sendWithXhr( | ||
data: string, | ||
done: (result: ExportResult) => void, | ||
urlStr: string, | ||
logger: api.Logger, | ||
xhrHeaders: Record<string, string> = {} | ||
) { | ||
const xhr = new window.XMLHttpRequest(); | ||
xhr.open('POST', urlStr); | ||
Object.entries(xhrHeaders).forEach(([k, v]) => { | ||
xhr.setRequestHeader(k, v); | ||
}); | ||
|
||
xhr.onreadystatechange = () => { | ||
if (xhr.readyState === XMLHttpRequest.DONE) { | ||
const statusCode = xhr.status || 0; | ||
logger.debug( | ||
'Zipkin response status code: %d, body: %s', | ||
statusCode, | ||
data | ||
); | ||
|
||
if (xhr.status >= 200 && xhr.status < 400) { | ||
return done(ExportResult.SUCCESS); | ||
} else if (statusCode < 500) { | ||
return done(ExportResult.FAILED_NOT_RETRYABLE); | ||
} else { | ||
return done(ExportResult.FAILED_RETRYABLE); | ||
} | ||
} | ||
}; | ||
|
||
xhr.onerror = err => { | ||
logger.error('Zipkin request error', err); | ||
return done(ExportResult.FAILED_RETRYABLE); | ||
}; | ||
|
||
// Issue request to remote service | ||
logger.debug('Zipkin request payload: %s', data); | ||
xhr.send(data); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/opentelemetry-exporter-zipkin/src/platform/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './node'; |
17 changes: 17 additions & 0 deletions
17
packages/opentelemetry-exporter-zipkin/src/platform/node/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './util'; |
Oops, something went wrong.