11import * as Core from "@actions/core" ;
2+ import {
3+ spanToTraceHeader ,
4+ spanToBaggageHeader ,
5+ startSpan ,
6+ type Scope ,
7+ type Span ,
8+ } from "@sentry/core" ;
29import { z } from "zod" ;
310import { FailedFetchError } from "../errors/FailedFetchError.ts" ;
411import { UploadLimitReachedError } from "../errors/UploadLimitReachedError.ts" ;
@@ -10,6 +17,7 @@ import { findGitService } from "./findGitService.ts";
1017import { UndefinedGitServiceError } from "../errors/UndefinedGitServiceError.ts" ;
1118import { FailedOIDCFetchError } from "../errors/FailedOIDCFetchError.ts" ;
1219import { BadOIDCServiceError } from "../errors/BadOIDCServiceError.ts" ;
20+ import { DEFAULT_API_URL } from "./normalizeOptions.ts" ;
1321
1422interface GetPreSignedURLArgs {
1523 apiUrl : string ;
@@ -21,6 +29,8 @@ interface GetPreSignedURLArgs {
2129 useGitHubOIDC : boolean ;
2230 gitHubOIDCTokenAudience : string ;
2331 } ;
32+ sentryScope ?: Scope ;
33+ sentrySpan ?: Span ;
2434}
2535
2636type RequestBody = Record < string , string | null | undefined > ;
@@ -38,6 +48,8 @@ export const getPreSignedURL = async ({
3848 retryCount,
3949 gitService,
4050 oidc,
51+ sentryScope,
52+ sentrySpan,
4153} : GetPreSignedURLArgs ) => {
4254 const headers = new Headers ( {
4355 "Content-Type" : "application/json" ,
@@ -84,22 +96,85 @@ export const getPreSignedURL = async ({
8496 }
8597 }
8698
99+ // Add Sentry headers if the API URL is the default i.e. Codecov itself
100+ if ( sentrySpan && apiUrl === DEFAULT_API_URL ) {
101+ // Create `sentry-trace` header
102+ const sentryTraceHeader = spanToTraceHeader ( sentrySpan ) ;
103+
104+ // Create `baggage` header
105+ const sentryBaggageHeader = spanToBaggageHeader ( sentrySpan ) ;
106+
107+ if ( sentryTraceHeader && sentryBaggageHeader ) {
108+ headers . set ( "sentry-trace" , sentryTraceHeader ) ;
109+ headers . set ( "baggage" , sentryBaggageHeader ) ;
110+ }
111+ }
112+
87113 let response : Response ;
88114 try {
89- const body = preProcessBody ( requestBody ) ;
90- response = await fetchWithRetry ( {
91- retryCount,
92- url : `${ apiUrl } ${ API_ENDPOINT } ` ,
93- name : "`get-pre-signed-url`" ,
94- requestData : {
95- method : "POST" ,
96- headers : headers ,
97- body : JSON . stringify ( body ) ,
115+ response = await startSpan (
116+ {
117+ name : "Fetching Pre-Signed URL" ,
118+ op : "http.client" ,
119+ scope : sentryScope ,
120+ parentSpan : sentrySpan ,
98121 } ,
99- } ) ;
122+ async ( getPreSignedURLSpan ) => {
123+ let wrappedResponse : Response ;
124+ const HTTP_METHOD = "POST" ;
125+ const URL = `${ apiUrl } ${ API_ENDPOINT } ` ;
126+
127+ if ( getPreSignedURLSpan ) {
128+ getPreSignedURLSpan . setAttribute ( "http.request.method" , HTTP_METHOD ) ;
129+ }
130+
131+ // we only want to set the URL attribute if the API URL is the default i.e. Codecov itself
132+ if ( getPreSignedURLSpan && apiUrl === DEFAULT_API_URL ) {
133+ getPreSignedURLSpan . setAttribute ( "http.request.url" , URL ) ;
134+ }
135+
136+ try {
137+ const body = preProcessBody ( requestBody ) ;
138+ wrappedResponse = await fetchWithRetry ( {
139+ retryCount,
140+ url : URL ,
141+ name : "`get-pre-signed-url`" ,
142+ requestData : {
143+ method : HTTP_METHOD ,
144+ headers : headers ,
145+ body : JSON . stringify ( body ) ,
146+ } ,
147+ } ) ;
148+ } catch ( e ) {
149+ red ( "Failed to fetch pre-signed URL" ) ;
150+ throw new FailedFetchError ( "Failed to fetch pre-signed URL" , {
151+ cause : e ,
152+ } ) ;
153+ }
154+
155+ // Add attributes only if the span is present
156+ if ( getPreSignedURLSpan ) {
157+ // Set attributes for the response
158+ getPreSignedURLSpan . setAttribute (
159+ "http.response.status_code" ,
160+ wrappedResponse . status ,
161+ ) ;
162+ getPreSignedURLSpan . setAttribute (
163+ "http.response_content_length" ,
164+ Number ( wrappedResponse . headers . get ( "content-length" ) ) ,
165+ ) ;
166+ getPreSignedURLSpan . setAttribute (
167+ "http.response.status_text" ,
168+ wrappedResponse . statusText ,
169+ ) ;
170+ }
171+
172+ return wrappedResponse ;
173+ } ,
174+ ) ;
100175 } catch ( e ) {
101- red ( "Failed to fetch pre-signed URL" ) ;
102- throw new FailedFetchError ( "Failed to fetch pre-signed URL" , { cause : e } ) ;
176+ // re-throwing the error here
177+ throw e ;
103178 }
104179
105180 if ( response . status === 429 ) {
0 commit comments