Skip to content

Commit

Permalink
feat(artusx-koa): split routes
Browse files Browse the repository at this point in the history
  • Loading branch information
thonatos committed Aug 19, 2024
1 parent 0ab275b commit d19382d
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 52 deletions.
57 changes: 10 additions & 47 deletions packages/apps/artusx-koa/src/controller/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@ import {
Controller,
GET,
POST,
MW,
Headers,
StatusCode,
} from '@artusx/core';

import { getMeter, getTracer, Span } from '@artusx/otl';
import { PluginInjectEnum } from '@artusx/utils';
import type { ArtusXContext, Log4jsClient, NunjucksClient } from '@artusx/core';
import PrometheusClient from '@artusx/plugin-prometheus/client';
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 {
Expand All @@ -32,52 +23,18 @@ export default class HomeController {
@Inject(ArtusXInjectEnum.Nunjucks)
nunjucks: NunjucksClient;

@Inject(PluginInjectEnum.Prometheus)
prometheus: PrometheusClient;

@MW([tracing])
@GET('/')
async home(ctx: ArtusXContext) {
const infoLogger = this.log4js.getLogger('default');
infoLogger.info(`path: /, method: GET`);

// 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();
ctx.body = this.nunjucks.render('index.html', {
title: 'ArtusX',
message: 'Hello ArtusX!',
data: {},
});
}

@GET('/metrics')
async metrics(ctx: ArtusXContext) {
const metrics = await this.prometheus.getMetrics();
ctx.body = metrics;
}

@POST('/post')
@StatusCode(200)
async post(_ctx: ArtusXContext) {
return this.nunjucks.render('index.html', { title: 'ArtusX', message: 'Post method' });
}

@MW([tracing])
@GET('/html')
@Headers({
'x-handler': 'Home-controller-html: html',
Expand All @@ -87,6 +44,12 @@ export default class HomeController {
return this.nunjucks.render('index.html', { title: 'ArtusX', message: 'Render with nunjucks' });
}

@POST('/post')
@StatusCode(200)
async post(_ctx: ArtusXContext) {
return this.nunjucks.render('index.html', { title: 'ArtusX', message: 'Post method' });
}

@GET('/error')
@StatusCode(403)
async error(ctx: ArtusXContext) {
Expand Down
19 changes: 19 additions & 0 deletions packages/apps/artusx-koa/src/controller/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ArtusXInjectEnum, ArtusApplication, Inject, Controller, GET } from '@artusx/core';
import type { ArtusXContext } from '@artusx/core';
import { PluginInjectEnum } from '@artusx/utils';
import PrometheusClient from '@artusx/plugin-prometheus/client';

@Controller('/metrics')
export default class MetricsController {
@Inject(ArtusXInjectEnum.Application)
app: ArtusApplication;

@Inject(PluginInjectEnum.Prometheus)
prometheus: PrometheusClient;

@GET('/')
async index(ctx: ArtusXContext) {
const metrics = await this.prometheus.getMetrics();
ctx.body = metrics;
}
}
42 changes: 42 additions & 0 deletions packages/apps/artusx-koa/src/controller/tracer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ArtusXInjectEnum, Inject, Controller, GET, MW } from '@artusx/core';

import { getMeter, getTracer, Span } from '@artusx/otl';
import type { ArtusXContext, 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 indexCounter = meter.createCounter('index.counter');

@Controller('/tracer')
export default class TracerController {
@Inject(ArtusXInjectEnum.Nunjucks)
nunjucks: NunjucksClient;

@MW([tracing])
@GET('/')
async index(ctx: ArtusXContext) {
// meter
console.log(indexCounter);
indexCounter.add(1);

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

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

ctx.body = this.nunjucks.render('tracer.html', {
title: 'Tracer',
message: 'OTL - OpenTelemetry',
data: {
spanId,
traceId,
count: indexCounter,
},
});
span.end();
});
}
}
48 changes: 48 additions & 0 deletions packages/apps/artusx-koa/src/middleware/metrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ArtusXInjectEnum, Inject, ArtusXContext, ArtusXNext, Middleware } from '@artusx/core';
import { register, Counter, Histogram } from '@artusx/plugin-prometheus';

const counter = new Counter({
name: 'artusx_metrics_request_counter',
help: 'artusx metrics request counter',
labelNames: ['path', 'methods'],
registers: [register],
});

const histogram = new Histogram({
name: 'artusx_metrics_request_histogram',
help: 'artusx metrics request histogram',
labelNames: ['path', 'methods'],
registers: [register],
});

register.registerMetric(counter);
register.registerMetric(histogram);

@Middleware({
enable: true,
})
export default class MetricsMiddleware {
@Inject(ArtusXInjectEnum.Config)
config: Record<string, string | number>;

async use(ctx: ArtusXContext, next: ArtusXNext): Promise<void> {
if (ctx.path === '/metrics') {
return await next();
}

counter.inc({
path: ctx.path,
methods: ctx.method,
});

histogram.observe(
{
path: ctx.path,
methods: ctx.method,
},
1
);

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

<footer>
<div>spanId: {{ data.spanId }}</div>
<div>traceId: {{ data.traceId }}</div>
</footer>
</body>
</html>
2 changes: 2 additions & 0 deletions packages/apps/artusx-koa/src/view/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<a href="/">home</a>
<a href="/html">html</a>
<a href="/ejs">ejs</a>
<a href="/tracer">tracer</a>
<a href="/metrics">metrics</a>
</nav>
21 changes: 21 additions & 0 deletions packages/apps/artusx-koa/src/view/tracer.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{title}}</title>
<link rel="stylesheet" href="/public/style.css" />
</head>
<body>
{% include "nav.html" ignore missing %}
<div class="container">
<h1>{{title}}</h1>
<p>{{message}}</p>
</div>

<footer>
<div>spanId: {{ data.spanId }}</div>
<div>traceId: {{ data.traceId }}</div>
</footer>
</body>
</html>

0 comments on commit d19382d

Please sign in to comment.