-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3153535
Showing
10 changed files
with
5,488 additions
and
0 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,2 @@ | ||
OTEL_SERVICE_NAME="my-app" | ||
OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs" |
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 @@ | ||
node_modules/ |
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,4 @@ | ||
{ | ||
"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"], | ||
"timeout": 10000 | ||
} |
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,63 @@ | ||
# Opetelemetry decorator | ||
|
||
This package provides a set of decorators to instrument your code. | ||
|
||
- _WithTracer_: Optionaly set the tracer name to for retrieving the tracer from the global tracer provider `trace.getTracer` | ||
- _WithSpan_: Wrap the decorated function with a new span using `tracer.startActiveSpan` | ||
- _SpanAttribute_: Will add the dedcorated parameter to the span attributes | ||
|
||
If you do not understand what is going on, please read the [OpenTelemetry documentation](https://opentelemetry.io/docs/instrumentation/js/). | ||
|
||
## Basic usage | ||
|
||
```ts | ||
@WithTracer(mypkg) | ||
export class MyClass { | ||
constructor() {} | ||
|
||
@WithSpan({ | ||
spanName: "customSpanName", | ||
recordException: false, | ||
setErrorStatusOnException: false, | ||
setOkStatusOnSuccess: false, | ||
}) | ||
public static async aStaticMethod(@SpanAttribute("arg1") arg: string, @SpanAttribute("arg2") arg2: number) { | ||
console.log("inside aStaticMethod"); | ||
return arg + arg2; | ||
} | ||
|
||
@WithSpan() | ||
public aMethod(arg: string, arg2: number) { | ||
console.log("inside aMethod"); | ||
const span = arguments[arguments.length - 1] as Span; | ||
span.setAttribute("custom.inner.attribute", arg + arg2); | ||
return arg + arg2; | ||
} | ||
} | ||
``` | ||
|
||
## Testing | ||
|
||
Run jaeger all in one | ||
|
||
```bash | ||
docker run -d --name jaeger-aio \ | ||
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \ | ||
-p 6831:6831/udp \ | ||
-p 6832:6832/udp \ | ||
-p 5778:5778 \ | ||
-p 16686:16686 \ | ||
-p 4317:4317 \ | ||
-p 4318:4318 \ | ||
-p 14250:14250 \ | ||
-p 14268:14268 \ | ||
-p 14269:14269 \ | ||
-p 9411:9411 \ | ||
jaegertracing/all-in-one:1.60 | ||
``` | ||
|
||
Run the example | ||
|
||
```bash | ||
node --inspect --loader ts-node/esm -r '@opentelemetry/auto-instrumentations-node/register' ./example.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,63 @@ | ||
import { diag, Span, SpanOptions, SpanStatusCode, trace } from "@opentelemetry/api"; | ||
import { SpanAttribute, WithSpan, WithTracer } from "./src/lib.js"; | ||
|
||
const mypkg = process.env.OTEL_SERVICE_NAME ?? "testing-otel-decorator"; | ||
|
||
@WithTracer(mypkg) | ||
export class MyClass { | ||
constructor() {} | ||
|
||
@WithSpan() | ||
public static async aStaticMethod(@SpanAttribute("arg1") arg: string, @SpanAttribute("arg2") arg2: number) { | ||
console.log("inside aStaticMethod"); | ||
return arg + arg2; | ||
} | ||
|
||
@WithSpan() | ||
public aMethod(arg: string, arg2: number) { | ||
console.log("inside aMethod"); | ||
const span = arguments[arguments.length - 1] as Span; | ||
span.setAttribute("custom.inner.attribute", arg + arg2); | ||
return arg + arg2; | ||
} | ||
|
||
@WithSpan() | ||
public async aAsyncMethod(@SpanAttribute("arg1") arg: string, @SpanAttribute("arg2") arg2: number) { | ||
console.log("inside aAsyncMethod"); | ||
return new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve(arg + arg2); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
@WithSpan({ spanName: "customSpanName" }) | ||
public async anAsyncMethodWithError() { | ||
console.log("inside anAsyncMethodWithError"); | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
reject("or nah"); | ||
("this a very bad error"); | ||
}, 5000); | ||
}); | ||
} | ||
} | ||
|
||
trace.getTracer("main").startActiveSpan("mainSpan", async (span: Span) => { | ||
try { | ||
const myInstance = new MyClass(); | ||
console.log(`#### calling aAsyncMethod`); | ||
const resultAsync = await myInstance.aAsyncMethod("test", 123); | ||
console.log(`result ${resultAsync}`); | ||
console.log(`#### calling aMethod`); | ||
const r1 = myInstance.aMethod("test", 123); | ||
console.log(`result ${r1}`); | ||
console.log(`#### calling aStaticMethod`); | ||
const r2 = await MyClass.aStaticMethod("test", 123); | ||
console.log(`result ${r2}`); | ||
const r3 = await myInstance.anAsyncMethodWithError(); | ||
console.log(`result ${r3}`); | ||
} finally { | ||
span.end(); | ||
} | ||
}); |
Oops, something went wrong.