Skip to content

Commit d7eae37

Browse files
msonnbclaude
andauthored
feat(google-cloud-serverless)!: Use http.client span op for GCP HTTP requests (#22660)
Use `http.client` instead of `http.client.<service>` in `googleCloudHttpIntegration`. The service is now identified by the new `server.address` attribute. Also added `http.request.method`. Part of #22446 > Note: MIGRATION.md will be one combined follow up PR --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 5600cd5 commit d7eae37

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

packages/google-cloud-serverless/src/integrations/google-cloud-http.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import type * as common from '@google-cloud/common';
2+
import { HTTP_REQUEST_METHOD, SENTRY_OP, SERVER_ADDRESS, URL_FULL } from '@sentry/conventions/attributes';
3+
import { WEB_SERVER_HTTP_CLIENT_SPAN_OP } from '@sentry/conventions/op';
24
import type { Client, IntegrationFn } from '@sentry/core';
35
import {
46
defineIntegration,
57
fill,
68
getClient,
9+
isURLObjectRelative,
10+
parseStringToURLObject,
711
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
812
SentryNonRecordingSpan,
913
} from '@sentry/core';
@@ -54,9 +58,12 @@ function wrapRequestFunction(orig: RequestFunction): RequestFunction {
5458
? startInactiveSpan({
5559
name: `${httpMethod} ${reqOpts.uri}`,
5660
onlyIfParent: true,
57-
op: `http.client.${identifyService(this.apiEndpoint)}`,
5861
attributes: {
62+
[SENTRY_OP]: WEB_SERVER_HTTP_CLIENT_SPAN_OP,
5963
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.serverless',
64+
[HTTP_REQUEST_METHOD]: httpMethod,
65+
[SERVER_ADDRESS]: getServerAddress(this.apiEndpoint),
66+
[URL_FULL]: reqOpts.uri,
6067
},
6168
})
6269
: new SentryNonRecordingSpan();
@@ -67,8 +74,8 @@ function wrapRequestFunction(orig: RequestFunction): RequestFunction {
6774
};
6875
}
6976

70-
/** Identifies service by its base url */
71-
function identifyService(apiEndpoint: string): string {
72-
const match = apiEndpoint.match(/^https:\/\/(\w+)\.googleapis.com$/);
73-
return match?.[1] || apiEndpoint.replace(/^(http|https)?:\/\//, '');
77+
/** Extracts the host of the API endpoint the request is sent to */
78+
function getServerAddress(apiEndpoint: string): string {
79+
const url = parseStringToURLObject(apiEndpoint);
80+
return url && !isURLObjectRelative(url) ? url.host : apiEndpoint;
7481
}

packages/google-cloud-serverless/test/integrations/google-cloud-http.test.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { BigQuery } from '@google-cloud/bigquery';
2+
import { HTTP_REQUEST_METHOD, SENTRY_OP, SERVER_ADDRESS, URL_FULL } from '@sentry/conventions/attributes';
3+
import { WEB_SERVER_HTTP_CLIENT_SPAN_OP } from '@sentry/conventions/op';
24
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
35
import { createTransport, NodeClient, setCurrentClient } from '@sentry/node';
46
import * as fs from 'fs';
@@ -76,19 +78,25 @@ describe('GoogleCloudHttp tracing', () => {
7678
const resp = await bigquery.query('SELECT true AS foo');
7779
expect(resp).toEqual([[{ foo: true }]]);
7880
expect(mockStartInactiveSpan).toBeCalledWith({
79-
op: 'http.client.bigquery',
8081
name: 'POST /jobs',
8182
onlyIfParent: true,
8283
attributes: {
84+
[SENTRY_OP]: WEB_SERVER_HTTP_CLIENT_SPAN_OP,
8385
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.serverless',
86+
[HTTP_REQUEST_METHOD]: 'POST',
87+
[SERVER_ADDRESS]: 'bigquery.googleapis.com',
88+
[URL_FULL]: '/jobs',
8489
},
8590
});
8691
expect(mockStartInactiveSpan).toBeCalledWith({
87-
op: 'http.client.bigquery',
8892
name: expect.stringMatching(/^GET \/queries\/.+/),
8993
onlyIfParent: true,
9094
attributes: {
95+
[SENTRY_OP]: WEB_SERVER_HTTP_CLIENT_SPAN_OP,
9196
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.serverless',
97+
[HTTP_REQUEST_METHOD]: 'GET',
98+
[SERVER_ADDRESS]: 'bigquery.googleapis.com',
99+
[URL_FULL]: expect.stringMatching(/^\/queries\/.+/),
92100
},
93101
});
94102
});

0 commit comments

Comments
 (0)