Skip to content

Commit

Permalink
feat(@artusx/otl): split to olt/tracer/meter, and update usages
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed May 23, 2024
1 parent 5ebe5ea commit bc1c35e
Show file tree
Hide file tree
Showing 22 changed files with 1,260 additions and 254 deletions.
1,147 changes: 987 additions & 160 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

24 changes: 13 additions & 11 deletions packages/apps/artusx-koa/src/controller/ejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,32 @@ export default class EjsController {

@GET('/')
@ContentType('html')
async view(ctx: ArtusXContext) {
async home(ctx: ArtusXContext) {
const locals = {
title: 'Example',
title: 'Ejs - Home',
message: 'This is a message',
};
ctx.body = await this.ejs.render('view.ejs', {
ctx.body = await this.ejs.render('home.ejs', {
locals,
});
}

@GET('/people')
async people(ctx: ArtusXContext) {
@GET('/user-list')
async userList(ctx: ArtusXContext) {
const people = ['geddy', 'neil', 'alex'];
ctx.body = await this.ejs.render('people.ejs', {
ctx.body = await this.ejs.render('users.ejs', {
title: 'Ejs - user list',
people,
name: 'hello world',
name: 'user list',
layout: false,
});
}

@GET('/user-show')
async userShow(ctx: ArtusXContext) {
ctx.body = await this.ejs.render('user/show.ejs', {
name: 'hello world',
@GET('/user-detail')
async userDetail(ctx: ArtusXContext) {
ctx.body = await this.ejs.render('user/detail.ejs', {
title: 'Ejs - user detail',
name: 'user detail',
layout: false,
});
}
Expand Down
33 changes: 29 additions & 4 deletions packages/apps/artusx-koa/src/controller/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import {
StatusCode,
} from '@artusx/core';

import { addEvent } from '@artusx/otl';
import { getMeter, getTracer, Span } from '@artusx/otl';

import type { ArtusXContext, Log4jsClient, NunjucksClient } from '@artusx/core';
import tracing from '../middleware/tracing';

const meter = getMeter('artusx-koa', '1.0.0');
const tracer = getTracer('artusx-koa', '1.0.0');
const homeCounter = meter.createCounter('home.counter');

@Controller()
export default class HomeController {
@Inject(ArtusXInjectEnum.Application)
Expand All @@ -32,8 +37,28 @@ export default class HomeController {
const infoLogger = this.log4js.getLogger('default');
infoLogger.info(`path: /, method: GET`);

addEvent('home', { key: 'home', value: 'home' });
ctx.body = this.nunjucks.render('index.html', { title: 'ArtusX', message: 'Hello ArtusX!' });
// meter
console.log(homeCounter);
homeCounter.add(1);

// tracer
tracer.startActiveSpan('home', (span: Span) => {
span.addEvent('home', { key: 'home', value: Math.random() });

const spanId = span?.spanContext().spanId;
const traceId = span?.spanContext().traceId;

ctx.body = this.nunjucks.render('index.html', {
title: 'ArtusX',
message: 'Hello ArtusX!',
data: {
spanId,
traceId,
count: homeCounter,
},
});
span.end();
});
}

@POST('/post')
Expand All @@ -45,7 +70,7 @@ export default class HomeController {
@MW([tracing])
@GET('/html')
@Headers({
'x-handler': 'home-controller-html: html',
'x-handler': 'Home-controller-html: html',
})
@StatusCode(200)
async html(_ctx: ArtusXContext) {
Expand Down
6 changes: 4 additions & 2 deletions packages/apps/artusx-koa/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import path from 'path';
import { setupTracing } from '@artusx/otl';
import { initOtl, initMeter, initTracer } from '@artusx/otl';
import { bootstrap } from '@artusx/utils';

const ROOT_DIR = path.resolve(__dirname);

setupTracing('artusx-koa');
initOtl('artusx-koa', '1.0.0');
initMeter();
initTracer();

bootstrap({ root: ROOT_DIR });
8 changes: 7 additions & 1 deletion packages/apps/artusx-koa/src/public/style.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
.nav {
nav {
padding: 2em 4em;
display: flex;
gap: 1em;
}

header,
main,
footer {
padding: 2em 4em;
}

.container {
margin: 2em 4em;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<h1>This is the footer</h1>

<% /*
Content for the `body` section should either be the first thing defined
Content for the `main` section should either be the first thing defined
in the view, or it has to be declared just like any other section.
*/ %>
<%- contentFor('body') %>
<%- contentFor('main') %>

This is part of the body.

Expand Down
10 changes: 8 additions & 2 deletions packages/apps/artusx-koa/src/view-ejs/layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@

<% /* Place any styles in the page in this section. */ %>
<%- style %>

<link rel="stylesheet" href="/public/style.css" />
</head>
<body>
<%- include('nav') %>

<header>
<% /*
Define an required placeholder for the header.
If a page doesn't define a header, there will be an error when rendering.
*/ %>
<%- header %>
<%- header %>
</header>

<%- body %>
<main>
<%- main %>
</main>

<footer>
<% /*
Expand Down
5 changes: 5 additions & 0 deletions packages/apps/artusx-koa/src/view-ejs/nav.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<nav>
<a href="/ejs">ejs</a>
<a href="/ejs/user-list">user-list</a>
<a href="/ejs/user-detail">user-detail</a>
</nav>
4 changes: 0 additions & 4 deletions packages/apps/artusx-koa/src/view-ejs/people.ejs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<div class="container">
<h2>show</h2>
<%= name %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/apps/artusx-koa/src/view-ejs/user/sub/info.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<div class="info">
<h2>info</h2>
<%= name %>
</div>
13 changes: 13 additions & 0 deletions packages/apps/artusx-koa/src/view-ejs/users.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><%= title %></title>
<link rel="stylesheet" href="/public/style.css" />
</head>
<body>
<%= people.join(", "); %>

<%- include('user/detail') %>
</body>
</html>
5 changes: 5 additions & 0 deletions packages/apps/artusx-koa/src/view/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@
<h1>{{title}}</h1>
<p>{{message}}</p>
</div>

<footer>
<div>spanId: {{ data.spanId }}</div>
<div>traceId: {{ data.traceId }}</div>
</footer>
</body>
</html>
9 changes: 5 additions & 4 deletions packages/apps/artusx-koa/src/view/nav.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<div class="nav">
<a href="/">Home</a>
<a href="/html">HTML</a>
</div>
<nav>
<a href="/">home</a>
<a href="/html">html</a>
<a href="/ejs">ejs</a>
</nav>
77 changes: 76 additions & 1 deletion packages/libs/otl/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,76 @@
# @artusx/plugin-opentelemetry
# @artusx/otl

Opentelemetry for ArtusX.

## Usage

### Automatic Instrumentation setup

init opentelemetry with default config.

- KoaInstrumentation
- HttpInstrumentation

```ts
import path from 'path';
import { initOtl } from '@artusx/otl';
import { bootstrap } from '@artusx/utils';

const ROOT_DIR = path.resolve(__dirname);

initOtl('artusx-koa', '1.0.0');
bootstrap({ root: ROOT_DIR });
```

### Manual instrumentation setup

custom tracer and meter.

```ts
import path from 'path';
import {
initOtl,
initMeter,
initTracer,
} from '@artusx/otl';
import { bootstrap } from '@artusx/utils';

const ROOT_DIR = path.resolve(__dirname);

// initOtl('artusx-koa', '1.0.0');
initMeter();
initTracer();

bootstrap({ root: ROOT_DIR });
```

#### addEvent and counter

```ts
import { ArtusXInjectEnum, Inject, Controller, GET } from '@artusx/core';
import { getMeter, getTracer, Span } from '@artusx/otl';

const meter = getMeter('artusx-koa', '1.0.0');
const tracer = getTracer('artusx-koa', '1.0.0');
const homeCounter = meter.createCounter('home.counter');

@Controller()
export default class HomeController {
@Inject(ArtusXInjectEnum.Nunjucks)
nunjucks: NunjucksClient;

@GET('/')
async home(ctx: ArtusXContext) {
// meter
homeCounter.add(1);

// tracer
tracer.startActiveSpan('home', (span: Span) => {
span.addEvent('home', { key: 'home', value: Math.random() });

ctx.body = this.nunjucks.render('index.html', { title: 'ArtusX', message: 'Hello ArtusX!' });
span.end();
});
}
}
```
10 changes: 5 additions & 5 deletions packages/libs/otl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
"tsc": "rm -rf lib && tsc -p ./tsconfig.build.json"
},
"dependencies": {
"@opentelemetry/api": "~1.8.0",
"@opentelemetry/exporter-jaeger": "latest",
"@opentelemetry/exporter-zipkin": "latest",
"@opentelemetry/instrumentation": "latest",
"@opentelemetry/api": "latest",
"@opentelemetry/auto-instrumentations-node": "latest",
"@opentelemetry/instrumentation-http": "latest",
"@opentelemetry/instrumentation-koa": "~0.40.0",
"@opentelemetry/instrumentation-koa": "latest",
"@opentelemetry/resources": "latest",
"@opentelemetry/sdk-metrics": "latest",
"@opentelemetry/sdk-node": "latest",
"@opentelemetry/sdk-trace-base": "latest",
"@opentelemetry/sdk-trace-node": "latest",
"@opentelemetry/semantic-conventions": "latest"
Expand Down
12 changes: 12 additions & 0 deletions packages/libs/otl/src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SEMRESATTRS_SERVICE_NAME, SEMRESATTRS_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';

import { Resource } from '@opentelemetry/resources';

export const initResource = (name?: string, version?: string) => {
const resource = new Resource({
[SEMRESATTRS_SERVICE_NAME]: name || 'artusx',
[SEMRESATTRS_SERVICE_VERSION]: version || '1.0',
});

return Resource.default().merge(resource);
};
Loading

0 comments on commit bc1c35e

Please sign in to comment.