Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
florentulve committed Sep 8, 2024
0 parents commit 3153535
Show file tree
Hide file tree
Showing 10 changed files with 5,488 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
OTEL_SERVICE_NAME="my-app"
OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
4 changes: 4 additions & 0 deletions .mocharc.json
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
}
63 changes: 63 additions & 0 deletions README.md
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
```
63 changes: 63 additions & 0 deletions example.ts
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();
}
});
Loading

0 comments on commit 3153535

Please sign in to comment.