Skip to content

Commit

Permalink
fix: add http.response.status_code
Browse files Browse the repository at this point in the history
  • Loading branch information
amertak committed Jan 17, 2025
1 parent ca0ce8b commit 4fcccaa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 6 additions & 1 deletion packages/integration-tests/src/tests/docload/docload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test.describe('docload', () => {
}
})

test('documentFetch, resourceFetch, and documentLoad spans', async ({ recordPage }) => {
test('documentFetch, resourceFetch, and documentLoad spans', async ({ recordPage, browserName }) => {
await recordPage.goTo('/docload/docload.ejs')

await recordPage.waitForSpans((spans) => spans.filter((span) => span.name === 'documentLoad').length === 1)
Expand Down Expand Up @@ -96,6 +96,11 @@ test.describe('docload', () => {

expect(docFetchSpans[0].tags['link.traceId']).toBeDefined()
expect(docFetchSpans[0].tags['link.spanId']).toBeDefined()
if (browserName !== 'webkit') {
// Webkit does not support https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/responseStatus
expect(parseInt(docFetchSpans[0].tags['http.response.status_code'] as string)).toBeGreaterThan(0)
}

expect(parseInt(scriptFetchSpans[0].tags['http.response_content_length'] as string)).toBeGreaterThan(0)

expect(docLoadSpans[0].tags['component']).toBe('document-load')
Expand Down
18 changes: 9 additions & 9 deletions packages/web/src/SplunkDocumentLoadInstrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import { InstrumentationConfig } from '@opentelemetry/instrumentation'
import { DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load'
import { AttributeNames, DocumentLoadInstrumentation } from '@opentelemetry/instrumentation-document-load'
import * as api from '@opentelemetry/api'
import { captureTraceParentFromPerformanceEntries } from './servertiming'
import { PerformanceEntries } from '@opentelemetry/sdk-trace-web'
Expand Down Expand Up @@ -65,27 +65,27 @@ export class SplunkDocumentLoadInstrumentation extends DocumentLoadInstrumentati
span.setAttribute('component', this.component)
}

if (span && exposedSpan.name !== 'documentLoad') {
if (span && exposedSpan.name !== AttributeNames.DOCUMENT_LOAD) {
// only apply links to document/resource fetch
// To maintain compatibility, getEntries copies out select items from
// different versions of the performance API into its own structure for the
// initial document load (but leaves the entries undisturbed for resource loads).
if (
exposedSpan.name === 'documentFetch' &&
!(entries as PerformanceEntriesWithServerTiming).serverTiming &&
performance.getEntriesByType
) {
if (exposedSpan.name === AttributeNames.DOCUMENT_FETCH && performance.getEntriesByType) {
const navEntries = performance.getEntriesByType('navigation')
if (navEntries[0]?.serverTiming) {
if (!(entries as PerformanceEntriesWithServerTiming).serverTiming && navEntries[0]?.serverTiming) {
;(entries as PerformanceEntriesWithServerTiming).serverTiming = navEntries[0].serverTiming
}

if (navEntries[0]?.responseStatus) {
span.setAttribute('http.response.status_code', navEntries[0].responseStatus)
}
}

captureTraceParentFromPerformanceEntries(entries, span)
span.setAttribute(SemanticAttributes.HTTP_METHOD, 'GET')
}

if (span && exposedSpan.name === 'documentLoad') {
if (span && exposedSpan.name === AttributeNames.DOCUMENT_LOAD) {
addExtraDocLoadTags(span)
}

Expand Down

0 comments on commit 4fcccaa

Please sign in to comment.