Skip to content

Commit fef0907

Browse files
authored
fix(publish-metrics): fix vu.uuid and improve attribute handling (#2285)
* fix(publish-metrics): fix vu.uuid and improve attribute handling * fix: handle errors with onError hook * feat: use SemanticAttributes object to set attributes
1 parent 15fcca7 commit fef0907

File tree

1 file changed

+38
-16
lines changed
  • packages/artillery-plugin-publish-metrics/lib/open-telemetry

1 file changed

+38
-16
lines changed

packages/artillery-plugin-publish-metrics/lib/open-telemetry/index.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const {
1616
} = require('@opentelemetry/api');
1717
const { Resource } = require('@opentelemetry/resources');
1818
const {
19-
SemanticResourceAttributes
19+
SemanticResourceAttributes,
20+
SemanticAttributes
2021
} = require('@opentelemetry/semantic-conventions');
2122

2223
const {
@@ -363,7 +364,11 @@ class OTelReporter {
363364
userContext.scenario?.name || `artillery-${engine}-scenario`,
364365
{
365366
startTime: Date.now(),
366-
kind: SpanKind.CLIENT
367+
kind: SpanKind.CLIENT,
368+
attributes: {
369+
'vu.uuid': userContext.vars.$uuid,
370+
[SemanticAttributes.PEER_SERVICE]: this.config.serviceName
371+
}
367372
}
368373
);
369374

@@ -397,11 +402,28 @@ class OTelReporter {
397402
this.traceConfig.useRequestNames && req.name
398403
? req.name
399404
: req.method.toLowerCase();
405+
406+
const url = new URL(req.url);
407+
let parsedUrl;
408+
if (url.username || url.password) {
409+
parsedUrl = url.origin + url.pathname + url.search + url.hash;
410+
}
400411
const span = this.httpTracer.startSpan(spanName, {
401412
startTime,
402413
kind: SpanKind.CLIENT,
403-
attributes: { 'vu.uuid': userContext.$uuid }
414+
attributes: {
415+
'vu.uuid': userContext.vars.$uuid,
416+
[SemanticAttributes.HTTP_URL]: parsedUrl || url.href,
417+
418+
// We set the port if it is specified, if not we set to a default port based on the protocol
419+
[SemanticAttributes.HTTP_SCHEME]:
420+
url.port || (url.protocol === 'http' ? 80 : 443),
421+
[SemanticAttributes.HTTP_METHOD]: req.method,
422+
[SemanticAttributes.NET_HOST_NAME]: url.hostname,
423+
...(this.traceConfig.attributes || {})
424+
}
404425
});
426+
405427
userContext.vars['__otlpHTTPRequestSpan'] = span;
406428

407429
events.on('error', (err) => {
@@ -449,7 +471,8 @@ class OTelReporter {
449471
this.httpTracer
450472
.startSpan(name, {
451473
kind: SpanKind.CLIENT,
452-
startTime: res.timings[value.start]
474+
startTime: res.timings[value.start],
475+
attributes: { 'vu.uuid': userContext.vars.$uuid }
453476
})
454477
.end(res.timings[value.end]);
455478
}
@@ -459,23 +482,22 @@ class OTelReporter {
459482
}
460483

461484
try {
462-
const url = new URL(req.url);
463485
span.setAttributes({
464-
'url.full': url.href,
465-
'url.path': url.pathname,
466-
'server.address': url.hostname,
467-
// We set the port if it is specified, if not we set to a default port based on the protocol
468-
'server.port': url.port || (url.protocol === 'http' ? 80 : 443),
469-
'http.request.method': req.method,
470-
'http.response.status_code': res.statusCode
486+
[SemanticAttributes.HTTP_STATUS_CODE]: res.statusCode,
487+
[SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH]:
488+
res.request.options.headers['content-length'],
489+
[SemanticAttributes.HTTP_FLAVOR]: res.httpVersion,
490+
[SemanticAttributes.HTTP_USER_AGENT]:
491+
res.request.options.headers['user-agent']
471492
});
472493

473494
if (res.statusCode >= 400) {
474-
span.setStatus({ code: SpanStatusCode.ERROR });
475-
}
476-
if (this.traceConfig?.attributes) {
477-
span.setAttributes(this.traceConfig.attributes);
495+
span.setStatus({
496+
code: SpanStatusCode.ERROR,
497+
message: res.statusMessage
498+
});
478499
}
500+
479501
span.end(endTime || Date.now());
480502
} catch (err) {
481503
// We don't do anything, if error occurs at this point it will be due to us already ending the span in beforeRequest hook in case of an error.

0 commit comments

Comments
 (0)