@platformatic/client
Advanced tools
Comparing version 0.37.1 to 0.37.2
@@ -16,3 +16,3 @@ import { FastifyPluginAsync, FastifyReply, FastifyRequest } from 'fastify' | ||
serviceId?: string; | ||
getHeaders?: (request: FastifyRequest, reply: FastifyReply) => Headers; | ||
getHeaders?: (request: FastifyRequest, reply: FastifyReply, options: { url: URL, method: string, headers: Headers, telemetryHeaders?: Headers, body: object }) => Promise<Headers>; | ||
} | ||
@@ -19,0 +19,0 @@ |
@@ -84,5 +84,2 @@ 'use strict' | ||
let telemetryContext = null | ||
if (this[kGetHeaders]) { | ||
headers = { ...headers, ...(await this[kGetHeaders]()) } | ||
} | ||
if (this[kTelemetryContext]) { | ||
@@ -121,2 +118,8 @@ telemetryContext = this[kTelemetryContext] | ||
const { span, telemetryHeaders } = openTelemetry?.startSpanClient(urlToCall.toString(), method, telemetryContext) || { span: null, telemetryHeaders: {} } | ||
if (this[kGetHeaders]) { | ||
const options = { url: urlToCall, method, headers, telemetryHeaders, body } | ||
headers = { ...headers, ...(await this[kGetHeaders](options)) } | ||
} | ||
let res | ||
@@ -123,0 +126,0 @@ try { |
@@ -34,3 +34,3 @@ import { expectError, expectType } from 'tsd' | ||
url: 'http://127.0.0.1/path/42', | ||
getHeaders: ({ url }, { sent }) => ({ foo: 'bar', url, baz: sent.toString() }), | ||
getHeaders: async ({ url }, { sent }, options) => ({ foo: 'bar', url, baz: sent.toString(), opt: JSON.stringify(options) }), | ||
headers: { foo: 'bar' }, | ||
@@ -37,0 +37,0 @@ name: 'Frassica', |
{ | ||
"name": "@platformatic/client", | ||
"version": "0.37.1", | ||
"version": "0.37.2", | ||
"description": "A client for all platformatic backends", | ||
@@ -26,3 +26,3 @@ "main": "index.js", | ||
"tsd": "^0.28.1", | ||
"@platformatic/telemetry": "0.37.1" | ||
"@platformatic/telemetry": "0.37.2" | ||
}, | ||
@@ -29,0 +29,0 @@ "dependencies": { |
@@ -146,3 +146,3 @@ 'use strict' | ||
test('req decorator with OpenAPI', async ({ teardown, same, rejects }) => { | ||
test('req decorator with OpenAPI', async ({ teardown, same }) => { | ||
try { | ||
@@ -165,3 +165,24 @@ await fs.unlink(join(__dirname, 'fixtures', 'auth', 'db.sqlite')) | ||
name: 'client', | ||
async getHeaders (req) { | ||
async getHeaders (req, reply, options) { | ||
same(typeof req, 'object') | ||
same(typeof reply, 'object') | ||
same(options.url, { | ||
href: 'http://127.0.0.1:51363/movies/', | ||
origin: 'http://127.0.0.1:51363', | ||
protocol: 'http:', | ||
username: '', | ||
password: '', | ||
host: '127.0.0.1:51363', | ||
hostname: '127.0.0.1', | ||
port: '51363', | ||
pathname: '/movies/', | ||
search: '', | ||
searchParams: {}, | ||
hash: '' | ||
}) | ||
same(options.method, 'POST') | ||
same(options.headers, {}) | ||
same(options.telemetryHeaders, {}) | ||
same(options.body, { title: 'The Matrix' }) | ||
return { | ||
@@ -168,0 +189,0 @@ 'x-platformatic-admin-secret': req.headers['x-platformatic-admin-secret'] |
@@ -54,4 +54,4 @@ 'use strict' | ||
const { exporter } = app.openTelemetry | ||
const finishedSpans = exporter.getFinishedSpans() | ||
const { exporters } = app.openTelemetry | ||
const finishedSpans = exporters[0].getFinishedSpans() | ||
// The first span is the client span, the second (because ended after the first) is the span for the POST that triggers the client | ||
@@ -72,5 +72,5 @@ equal(finishedSpans.length, 2) | ||
// Target app, we check that propagation works | ||
same(targetApp.openTelemetry.exporter.getFinishedSpans().length, 2) | ||
same(targetApp.openTelemetry.exporters[0].getFinishedSpans().length, 2) | ||
// The first span is the client call to `/documentation/json`, the second is the server call to `/movies/ | ||
const serverSpan = targetApp.openTelemetry.exporter.getFinishedSpans()[1] | ||
const serverSpan = targetApp.openTelemetry.exporters[0].getFinishedSpans()[1] | ||
same(serverSpan.name, 'POST /movies/') | ||
@@ -132,4 +132,4 @@ const serverTraceId = serverSpan.spanContext().traceId | ||
const { exporter } = app.openTelemetry | ||
const finishedSpans = exporter.getFinishedSpans() | ||
const { exporters } = app.openTelemetry | ||
const finishedSpans = exporters[0].getFinishedSpans() | ||
@@ -152,5 +152,5 @@ // The first span is the client span, the second (because ended after the first) is the span for the POST that triggers the client | ||
// Target app | ||
same(targetApp.openTelemetry.exporter.getFinishedSpans().length, 2) | ||
same(targetApp.openTelemetry.exporters[0].getFinishedSpans().length, 2) | ||
// The first span is the client call to `/documentation/json`, the second is the server call to `/movies/ | ||
const serverSpan = targetApp.openTelemetry.exporter.getFinishedSpans()[1] | ||
const serverSpan = targetApp.openTelemetry.exporters[0].getFinishedSpans()[1] | ||
same(serverSpan.name, 'POST /movies/') | ||
@@ -214,4 +214,4 @@ const serverTraceId = serverSpan.spanContext().traceId | ||
const { exporter } = app.openTelemetry | ||
const finishedSpans = exporter.getFinishedSpans() | ||
const { exporters } = app.openTelemetry | ||
const finishedSpans = exporters[0].getFinishedSpans() | ||
// The first span is the client span, the second (because ended after the first) is the span for the POST that triggers the client | ||
@@ -232,4 +232,4 @@ equal(finishedSpans.length, 2) | ||
// Target app, we check that propagation works | ||
same(targetApp.openTelemetry.exporter.getFinishedSpans().length, 1) | ||
const serverSpan = targetApp.openTelemetry.exporter.getFinishedSpans()[0] | ||
same(targetApp.openTelemetry.exporters[0].getFinishedSpans().length, 1) | ||
const serverSpan = targetApp.openTelemetry.exporters[0].getFinishedSpans()[0] | ||
same(serverSpan.name, 'POST /graphql') | ||
@@ -236,0 +236,0 @@ const serverTraceId = serverSpan.spanContext().traceId |
135068
4908