Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Jaeger Prometheus metrics #402

Merged
merged 5 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [6.35.1] - 2020-07-22
### Added
- [metrics] Export jaeger metrics.

### Fixed
- [metrics] Typo on event listened to increment `runtime_http_aborted_requests_total`.
- [tracing:entrypoint] Fix waiting for response stream to finish.

## [6.35.0] - 2020-07-08
### Added
- [metrics] Create new Prometheus exported metrics:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vtex/api",
"version": "6.35.0",
"version": "6.35.1",
"description": "VTEX I/O API client",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
10 changes: 9 additions & 1 deletion src/service/tracing/TracerSingleton.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initTracer as initJaegerTracer, TracingConfig, TracingOptions } from 'jaeger-client'
import { initTracer as initJaegerTracer, PrometheusMetricsFactory, TracingConfig, TracingOptions } from 'jaeger-client'
import { Tracer } from 'opentracing'
import promClient from 'prom-client'
import { APP, LINKED, NODE_ENV, NODE_VTEX_API_VERSION, PRODUCTION, REGION, WORKSPACE } from '../../constants'
import { AppTags } from '../../tracing/Tags'
import { appIdToAppAtMajor } from '../../utils'
Expand Down Expand Up @@ -42,6 +43,13 @@ export class TracerSingleton {
}

const options: TracingOptions = {
/**
* Jaeger metric names are available in:
* https://github.com/jaegertracing/jaeger-client-node/blob/master/src/metrics/metrics.js
*
* Runtime will prefix these metrics with 'runtime:'
*/
metrics: new PrometheusMetricsFactory(promClient as any, 'runtime'),
tags: defaultTags,
}

Expand Down
30 changes: 25 additions & 5 deletions src/service/tracing/tracingMiddlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,20 @@ export const addTracingMiddleware = (tracer: Tracer) => {
}

const rootSpan = tracer.extract(FORMAT_HTTP_HEADERS, ctx.request.headers) as undefined | SpanContext
const currentSpan = tracer.startSpan('unknown-operation', { childOf: rootSpan })
const currentSpan = tracer.startSpan('unknown-operation', {
childOf: rootSpan,
tags: { [OpentracingTags.SPAN_KIND]: OpentracingTags.SPAN_KIND_RPC_SERVER },
})

const initialSamplingDecision = getTraceInfo(currentSpan).isSampled

ctx.tracing = { currentSpan, tracer }
ctx.req.once('abort', () => abortedRequests.inc({ [MetricLabels.REQUEST_HANDLER]: (currentSpan as any).operationName as string }, 1))
ctx.req.once('aborted', () =>
abortedRequests.inc({ [MetricLabels.REQUEST_HANDLER]: (currentSpan as any).operationName as string }, 1)
)

let responseClosed = false
ctx.res.once('close', () => (responseClosed = true))

try {
await next()
Expand All @@ -66,8 +77,11 @@ export const addTracingMiddleware = (tracer: Tracer) => {

const traceInfo = getTraceInfo(currentSpan)
if (traceInfo.isSampled) {
if (!initialSamplingDecision) {
currentSpan.setTag(OpentracingTags.SPAN_KIND, OpentracingTags.SPAN_KIND_RPC_SERVER)
}

currentSpan.addTags({
[OpentracingTags.SPAN_KIND]: OpentracingTags.SPAN_KIND_RPC_SERVER,
[OpentracingTags.HTTP_URL]: ctx.request.href,
[OpentracingTags.HTTP_METHOD]: ctx.request.method,
[OpentracingTags.HTTP_STATUS_CODE]: ctx.response.status,
Expand All @@ -82,17 +96,23 @@ export const addTracingMiddleware = (tracer: Tracer) => {
ctx.set(TRACE_ID_HEADER, traceInfo.traceId)
}

onStreamFinished(ctx.res, () => {
const onResFinished = () => {
requestTimings.observe(
{
[MetricLabels.REQUEST_HANDLER]: (currentSpan as any).operationName as string,
},
hrToMillisFloat(process.hrtime(start))
)

concurrentRequests.dec(1)
currentSpan.finish()
})
}

if (responseClosed) {
onResFinished()
} else {
onStreamFinished(ctx.res, onResFinished)
}
}
}
}
Expand Down