From 34a3a13e07aa1d5a800eb331d0a68cb0e792149f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Martins=20N=C3=A1poli?= Date: Fri, 10 Jul 2020 19:01:45 -0300 Subject: [PATCH 1/5] Export jaeger prometheus metrics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tiago Martins Nápoli --- src/service/tracing/TracerSingleton.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/service/tracing/TracerSingleton.ts b/src/service/tracing/TracerSingleton.ts index a9da67f13..671361755 100644 --- a/src/service/tracing/TracerSingleton.ts +++ b/src/service/tracing/TracerSingleton.ts @@ -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' @@ -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, } From 84f0ba4dd8f6e4aed3825e8dba752b4d3b1a41dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Martins=20N=C3=A1poli?= Date: Fri, 10 Jul 2020 19:04:59 -0300 Subject: [PATCH 2/5] Fix abort event and response finished watching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tiago Martins Nápoli --- src/service/tracing/tracingMiddlewares.ts | 30 +++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/service/tracing/tracingMiddlewares.ts b/src/service/tracing/tracingMiddlewares.ts index 2bd762bc5..c1f8361cf 100644 --- a/src/service/tracing/tracingMiddlewares.ts +++ b/src/service/tracing/tracingMiddlewares.ts @@ -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() @@ -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, @@ -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) + } } } } From 2641d12d35467b3116c660f862896003d4639b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Martins=20N=C3=A1poli?= Date: Fri, 10 Jul 2020 19:08:28 -0300 Subject: [PATCH 3/5] changelog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tiago Martins Nápoli --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40dda2c7c..f1c0df1e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- [metrics] Export jaeger metrics. + +### Fixed +- [metrics] Typo on event listened by to increment `runtime_http_aborted_requests_total`. +- [tracing:entrypoint] Fix listening for response stream to finish. ## [6.35.0] - 2020-07-08 ### Added From 93a9fc302389c86906018e2d4c874ae812f78aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Martins=20N=C3=A1poli?= Date: Wed, 15 Jul 2020 17:13:49 -0300 Subject: [PATCH 4/5] fix changelog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tiago Martins Nápoli --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1c0df1e8..e024463b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - [metrics] Export jaeger metrics. ### Fixed -- [metrics] Typo on event listened by to increment `runtime_http_aborted_requests_total`. -- [tracing:entrypoint] Fix listening for response stream to finish. +- [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 From cb24b91e9140c2f6600450dce37a6bd2783d3d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Martins=20N=C3=A1poli?= Date: Wed, 22 Jul 2020 11:45:52 -0300 Subject: [PATCH 5/5] Release v6.35.1 --- CHANGELOG.md | 2 ++ package.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e024463b2..5c16f27c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] + +## [6.35.1] - 2020-07-22 ### Added - [metrics] Export jaeger metrics. diff --git a/package.json b/package.json index 3f2287e64..369a94ea7 100644 --- a/package.json +++ b/package.json @@ -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",