newrelic / newrelic-ios-agent-spm

This repository hosts the Swift Package Manager deployment of the iOS agent
Apache License 2.0
15 stars 9 forks source link

New Relic headers are not added to the request while using urlSession.data(for: request) #43

Closed subramka closed 2 months ago

subramka commented 5 months ago

Description

When utilising urlSession.data(for: request) in Swift for making network requests, we are encountering an unexpected behaviour where the New Relic headers are not being added to the request as intended. This issue is preventing the proper instrumentation of network requests by the New Relic SDK, impacting our ability to gather performance and monitoring data.

Steps to Reproduce

Use the urlSession.data(for: request) method to perform the network request.

do {
    let (data, response) = try await urlSession.data(for: request)
    // Process the response data
} catch let error {
    print("Error: \(error)")
}

Use proxy tools like proxyman to inspect the actual HTTP requests sent by your application.

Expected Behavior

There is no new relic header added to the request by sdk

X-NewRelic-ID: XXXXX
newrelic: XXXXXX

Relevant Logs / Console output

Your Environment

Apple M1 Max Ventura 13.5.2 (22G91) Xcode 15.2 (15C500b) iOS - 16.7.2 iOS Agent - 7.4.6

Additional context

As a comparison, switching to using the traditional URLSession.dataTask method and observed New Relic headers are included.

let task = urlSession.dataTask(with: request) { data, response, error in
    // Process the response data
}
task.resume()
peter-major-apadmi commented 4 months ago

This still isn't fixed for us as of 7.4.9, even though it was described as being fixed in the 7.4.8 release notes:

Fixes async requests so they appear in distributed tracing.

We can see this because one of the SDK's in our app is making an API call (obviously via a non-async data task) and the x-newrelic-id header is present:

:method: GET
:scheme: https
:path: /content/key/redacted?depth=all&format=inlined
:authority: redacted.cdn.content.amplience.net
accept: application/json
content-type: application/json
x-newrelic-id: Vw4GWVRaABABVVNSBQEEXlQE
user-agent: redacted
accept-language: en-GB,en;q=0.9
accept-encoding: gzip, deflate, br

But our API call, which is using the async data task, does not have the x-newrelic-id header:

:method: GET
:scheme: https
:path: /uat/redacted/v1/api/subscriptions
:authority: redacted
content-type: application/json
accept: */*
authorization: Bearer redacted
x-correlation-id: 0593512A-F4C7-425C-8131-385BD5527AE4
x-firebase-appcheck: Bearer redacted
accept-encoding: gzip, deflate, br
cache-control: no-cache
x-component: ios
accept-language: en-GB,en;q=0.9
user-agent: redacted

We're using the same async syntax to make API calls as the original reporter:

let (data, httpResponse) = try await session.data(for: urlRequest)
sky-tiagoornelas commented 4 months ago

@peter-major-apadmi just out of curiosity, did you try enabling the NRFeatureFlag_SwiftAsyncURLSessionSupport FF that comes with the SDK? I was doing some tests with version 7.4.8 this week and we got the x-newrelic-id present in all requests. The strange thing was, the header was being sent with that Feature Flag both activated and deactivated.

peter-major-apadmi commented 4 months ago

Hi @sky-tiagoornelas. Thanks for your response. Yes I've got the feature flag enabled. Here's how I start the agent:

            NewRelic.enableFeatures([
                NRMAFeatureFlags.NRFeatureFlag_SwiftAsyncURLSessionSupport,
                NRMAFeatureFlags.NRFeatureFlag_NewEventSystem])
            NewRelic.disableFeatures(NRMAFeatureFlags.NRFeatureFlag_AppStartMetrics)
            NewRelic.start(withApplicationToken: environmentConfiguration.newRelicToken)
            NewRelic.addHTTPHeaderTracking(for: [HttpHeaderKey.correlationId.rawValue])
sky-tiagoornelas commented 4 months ago

Strange then how I'm getting that header to go out 🤔

peter-major-apadmi commented 4 months ago

I've just put this bit of code below my async code:

        let task = session.dataTask(with: URLRequest(url: URL(string: "https://www.redacted.co.uk")!))
        task.resume()

and it's working fine:

:method: GET
:scheme: https
:path: /
:authority: www.redacted.co.uk
accept: */*
x-newrelic-id: Vw4GWVRaABABVVNSBQEEXlQE
user-agent: redacted
accept-language: en-GB,en;q=0.9
accept-encoding: gzip, deflate, br

So it's definitely just an async issue.

smalsam-newr commented 4 months ago

Hello. We have a fix for this issue that we are working on. We anticipate releasing it early next week. https://github.com/newrelic/newrelic-ios-agent/pull/211

peter-major-apadmi commented 4 months ago

Thank you for looking into this. I'll keep an eye out for the new release.

peter-major-apadmi commented 4 months ago

Hi @smalsam-newr. Any news on the progress of the next release to fix this issue?

smalsam-newr commented 3 months ago

@peter-major-apadmi We have merged the fix in, which you can see in the PR linked in our new open-source repository. We are targeting a release sometime this week.

smalsam-newr commented 3 months ago

@peter-major-apadmi @subramka Version 7.4.10, which should have a fix for this issue, has been deployed.