Skip to content

Commit 83d716e

Browse files
committed
feat: Add opentracing package
1 parent 7b8287d commit 83d716e

19 files changed

+377
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"packages/hub",
1919
"packages/minimal",
2020
"packages/node",
21+
"packages/opentracing",
2122
"packages/types",
2223
"packages/typescript",
2324
"packages/utils"

packages/browser/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "./tsconfig.build.json",
3-
"include": ["src/**/*", "test/**/*"],
3+
"include": ["src/**/*", "test/**/*", "../opentracing/src/sessiontracking.ts"],
44
"exclude": ["dist"],
55
"compilerOptions": {
66
"rootDir": ".",

packages/opentracing/.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*
2+
!/dist/**/*
3+
!/esm/**/*

packages/opentracing/LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2019, Sentry
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

packages/opentracing/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<p align="center">
2+
<a href="https://sentry.io" target="_blank" align="center">
3+
<img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
4+
</a>
5+
<br />
6+
</p>
7+
8+
# Sentry JavaScript OpenTracing API
9+
10+
[![npm version](https://img.shields.io/npm/v/@sentry/opentracing.svg)](https://www.npmjs.com/package/@sentry/opentracing)
11+
[![npm dm](https://img.shields.io/npm/dm/@sentry/opentracing.svg)](https://www.npmjs.com/package/@sentry/opentracing)
12+
[![npm dt](https://img.shields.io/npm/dt/@sentry/opentracing.svg)](https://www.npmjs.com/package/@sentry/opentracing)
13+
[![typedoc](https://img.shields.io/badge/docs-typedoc-blue.svg)](http://getsentry.github.io/sentry-javascript/)
14+
15+
## Links
16+
17+
- [Official SDK Docs](https://docs.sentry.io/quickstart/)
18+
- [TypeDoc](http://getsentry.github.io/sentry-javascript/)
19+
20+
## General
21+
22+
TBD

packages/opentracing/package.json

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "@sentry/opentracing",
3+
"version": "4.6.1",
4+
"description": "Sentry OpenTracing API implementation",
5+
"repository": "git://github.com/getsentry/sentry-javascript.git",
6+
"homepage": "https://github.com/getsentry/sentry-javascript/tree/master/packages/minimal",
7+
"author": "Sentry",
8+
"license": "BSD-3-Clause",
9+
"engines": {
10+
"node": ">=6"
11+
},
12+
"main": "dist/index.js",
13+
"types": "dist/index.d.ts",
14+
"publishConfig": {
15+
"access": "public"
16+
},
17+
"dependencies": {
18+
"@sentry/hub": "4.6.1",
19+
"@sentry/utils": "4.6.1",
20+
"@sentry/types": "4.5.3",
21+
"opentracing": "0.14.3"
22+
},
23+
"devDependencies": {
24+
"jest": "^24.1.0",
25+
"npm-run-all": "^4.1.2",
26+
"prettier": "^1.16.4",
27+
"prettier-check": "^2.0.0",
28+
"rimraf": "^2.6.3",
29+
"tslint": "^5.12.1",
30+
"typescript": "^3.3.3"
31+
},
32+
"scripts": {
33+
"build": "run-p build:es5 build:esm",
34+
"build:es5": "tsc -p tsconfig.build.json",
35+
"build:esm": "run-s build:esm:transpile build:esm:rewrite",
36+
"build:esm:transpile": "tsc -p tsconfig.esm.json",
37+
"build:esm:rewrite": "node ../../scripts/esm-rewrite.js",
38+
"build:watch": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
39+
"clean": "rimraf dist coverage",
40+
"lint": "run-s lint:prettier lint:tslint",
41+
"lint:prettier": "prettier-check \"{src,test}/**/*.ts\"",
42+
"lint:tslint": "tslint -t stylish -p .",
43+
"lint:tslint:json": "tslint --format json -p . | tee lint-results.json",
44+
"fix": "run-s fix:tslint fix:prettier",
45+
"fix:prettier": "prettier --write \"{src,test}/**/*.ts\"",
46+
"fix:tslint": "tslint --fix -t stylish -p .",
47+
"test": "jest",
48+
"test:watch": "jest --watch"
49+
},
50+
"jest": {
51+
"collectCoverage": true,
52+
"transform": {
53+
"^.+\\.ts$": "ts-jest"
54+
},
55+
"moduleFileExtensions": [
56+
"js",
57+
"ts"
58+
],
59+
"testEnvironment": "node",
60+
"testMatch": [
61+
"**/*.test.ts"
62+
],
63+
"globals": {
64+
"ts-jest": {
65+
"tsConfig": "./tsconfig.json",
66+
"diagnostics": false
67+
}
68+
}
69+
},
70+
"sideEffects": false
71+
}

packages/opentracing/src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export { Span } from './span';
2+
export { Tracer } from './tracer';
3+
export { SessionTracking } from './sessiontracking';
4+
export { initGlobalTracer } from 'opentracing/lib/global_tracer';
5+
6+
import { globalTracer as otGlobalTracer } from 'opentracing/lib/global_tracer';
7+
import { Tracer } from './tracer';
8+
9+
/**
10+
* Return the global set Tracer
11+
*/
12+
export function globalTracer(): Tracer {
13+
return (otGlobalTracer() as unknown) as Tracer;
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { getCurrentHub } from '@sentry/hub';
2+
import { Integration } from '@sentry/types';
3+
import { globalTracer, initGlobalTracer } from './index';
4+
import { Tracer } from './tracer';
5+
6+
/**
7+
* Session Tracking Integration
8+
*/
9+
export class SessionTracking implements Integration {
10+
/**
11+
* @inheritDoc
12+
*/
13+
public name: string = SessionTracking.id;
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
public static id: string = 'SessionTracking';
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public setupOnce(): void {
24+
const tracer = new Tracer();
25+
initGlobalTracer(tracer);
26+
const span = globalTracer().startSpan('sdk.init');
27+
console.log(JSON.stringify(span));
28+
setTimeout(() => {
29+
getCurrentHub().captureEvent({ spans: [span] });
30+
}, 10);
31+
}
32+
}

packages/opentracing/src/span.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import { Span as SpanInterface } from '@sentry/types';
2+
import { Span as otSpan } from 'opentracing/lib/span';
3+
import { SpanContext } from './spancontext';
4+
import { Tracer } from './tracer';
5+
6+
/** JSDoc */
7+
interface Log {
8+
keyValuePairs: { [key: string]: any };
9+
timestamp?: number;
10+
}
11+
12+
/** JSDoc */
13+
export class Span extends otSpan implements SpanInterface {
14+
private finishTime: number = 0;
15+
16+
private readonly logs: Log[] = [];
17+
18+
public tags: {
19+
[key: string]: string;
20+
} = {};
21+
22+
public baggage: {
23+
[key: string]: string;
24+
} = {};
25+
26+
public constructor(
27+
private readonly usedTracer: Tracer,
28+
private operation: string,
29+
private readonly spanContext: SpanContext,
30+
private readonly startTime: number = Date.now(),
31+
) {
32+
super();
33+
}
34+
35+
/**
36+
* Returns the context.
37+
*/
38+
protected _context(): SpanContext {
39+
return this.spanContext;
40+
}
41+
42+
/**
43+
* Returns the tracer passed to the span.
44+
*/
45+
protected _tracer(): Tracer {
46+
return this.usedTracer;
47+
}
48+
49+
/**
50+
* Sets the operation name.
51+
*/
52+
protected _setOperationName(name: string): void {
53+
this.operation = name;
54+
}
55+
56+
/** JSDoc */
57+
protected _setBaggageItem(key: string, value: string): void {
58+
this.baggage[key] = value;
59+
}
60+
61+
/** JSDoc */
62+
protected _getBaggageItem(key: string): string | undefined {
63+
return this.baggage[key];
64+
}
65+
66+
/**
67+
* Adds tags { [key: string]: string } to the span
68+
*/
69+
protected _addTags(set: { [key: string]: string }): void {
70+
const keys = Object.keys(set);
71+
for (const key of keys) {
72+
this.tags[key] = set[key];
73+
}
74+
}
75+
76+
/**
77+
* Store log entry.
78+
*/
79+
protected _log(keyValuePairs: { [key: string]: any }, timestamp?: number): void {
80+
this.logs.push({
81+
keyValuePairs,
82+
timestamp,
83+
});
84+
}
85+
86+
/**
87+
* JSDoc
88+
*/
89+
protected _finish(finishTime?: number): void {
90+
this.finishTime = finishTime || Date.now();
91+
}
92+
93+
/**
94+
* Returns the operationName.
95+
*/
96+
public getOperationName(): string {
97+
return this.operation;
98+
}
99+
100+
/**
101+
* Returns the duration of the span.
102+
*/
103+
public duration(): number {
104+
return this.finishTime - this.startTime;
105+
}
106+
107+
/**
108+
* @inheritdoc
109+
*/
110+
public toJSON(): any {
111+
// TODO
112+
console.log('ajajajajaj');
113+
return 'ababab';
114+
}
115+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { uuid4 } from '@sentry/utils/misc';
2+
/**
3+
* JSDoc
4+
*/
5+
export class SpanContext {
6+
public constructor(
7+
private readonly traceId: string,
8+
private readonly spanId: string = uuid4(), // private readonly parentId: string,
9+
) {}
10+
11+
/**
12+
* Returns debug version of the span.
13+
*/
14+
public toString(): string {
15+
return `TraceId: ${this.traceId} SpanId: ${this.spanId}`;
16+
}
17+
}

packages/opentracing/src/tracer.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { SpanOptions, Tracer as otTracer } from 'opentracing/lib/tracer';
2+
import { Span } from './span';
3+
import { SpanContext } from './spancontext';
4+
5+
/**
6+
* JSDoc
7+
*/
8+
export class Tracer extends otTracer {
9+
private readonly spans: Span[] = [];
10+
11+
/**
12+
* Called by public method startSpan
13+
* @param name Name of the operation
14+
* @param fields Options for the span {@link opentracing.SpanOptions}
15+
*/
16+
protected _startSpan(name: string, fields: SpanOptions): Span {
17+
// TODO: Traceid
18+
const span = new Span(this, name, new SpanContext('1'), fields.startTime);
19+
this.spans.push(span);
20+
// TODO: Implement childof
21+
return span;
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public startSpan(name: string, options: SpanOptions = {}): Span {
28+
return (super.startSpan(name, options) as unknown) as Span;
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"outDir": "dist",
6+
"rootDir": "src"
7+
},
8+
"include": ["src/**/*.ts"]
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig.esm.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"outDir": "esm"
6+
},
7+
"include": ["src/**/*"]
8+
}

packages/opentracing/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.build.json",
3+
"include": ["src/**/*.ts"],
4+
"exclude": ["dist"],
5+
"compilerOptions": {
6+
"rootDir": ".",
7+
"types": ["node", "jest"]
8+
}
9+
}

packages/opentracing/tslint.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@sentry/typescript/tslint"
3+
}

0 commit comments

Comments
 (0)