-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
7 changed files
with
142 additions
and
52 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
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,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; | ||
} | ||
} |
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,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(); | ||
}); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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
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
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,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> |