open-telemetry / opentelemetry-swift

OpenTelemetry API for Swift
https://opentelemetry.io/docs/instrumentation/swift/
Apache License 2.0
207 stars 124 forks source link

cant produce api request log data #553

Open Cuizhangqiang opened 1 month ago

Cuizhangqiang commented 1 month ago

cant produce api request log data

let networkInstrumentation = URLSessionInstrumentation(configuration: URLSessionInstrumentationConfiguration())

we use Alamofire Make network request

Cuizhangqiang commented 1 month ago

URLSessionLogger.runningSpans always 0 elements

Cuizhangqiang commented 1 month ago

截屏2024-07-12 17 17 03

nachoBonafonte commented 1 month ago

The library is probably not identifying the Alamofire created URL session, I think it did in the past so probably Alamofire has updated something and we have lost visibility.

In URLSessionInstrumentation line 329 there is some code to handle it, explicitly in line 341, can you check if it is properly entering there and af_resume is still the internal name the library uses?

If not we would need a basic sample app reproducing the issue in order to try to fix it.

Cuizhangqiang commented 1 month ago

截屏2024-07-12 17 57 43 AFURLSessionManager and af_resume both not found in the library - Alamofire (= 5.9.1)

nachoBonafonte commented 1 month ago

The library is then clearly outdated, we would need a sample app that reproduces the issue in order to add support. We are also open to contributions 😉

Cuizhangqiang commented 1 month ago

In Alamofire 5, AFURLSessionManager and many of the classes and methods related to it have been refactored or removed. AFURLSessionManager is a core class in Alamofire 4 that handles all URLSession related tasks. However, with the release of Alamofire 5, the codebase has been simplified and refactored to better support Swift language features and a more modern API design.

In Alamofire 5, AFURLSessionManager was replaced by the Session class, which manages all request and response handling and underlying URLSession tasks.

In Alamofire 5, the af_resume method is deprecated because Alamofire's design has been refactored to better support Swift language features and simplify the management of network requests. In Alamofire 4, af_resume is a method in the AFURLSessionManager class that initiates a pending network request.

In Alamofire 5, it is no longer necessary to explicitly call the af_resume method to initiate network requests because all requests are initiated immediately upon creation. The new Session class manages the creation and execution of all requests, simplifying the API and improving ease of use.

Cuizhangqiang commented 1 month ago

I don't know how to fix the code to solve the problem I am currently experiencing, if there is an update on openmetry-swift, please let me know here, thank you very much 🙏

nachoBonafonte commented 1 month ago

As said previously, if you can provide a sample project with a minimum setup that reproduces the issue it will help a lot to have it solved, because it removes the barrier of setting up a project to anyone willing to help.

Cuizhangqiang commented 1 month ago

URLSessionInstrumentation+Alamofire.zip Currently, I created a new project and only imported Alamofire and opentelemetry-swift. I found that the console kept outputting logs after starting the project. Please help me see if this is a bug.Open and run the above file to reproduce,Thank you!🙏 截屏2024-07-17 18 40 17

bryce-b commented 1 week ago

This is because you are using the SimpleSpanProcessor, which will trigger an export as soon as it processes any spans, combining this with the instrumented http exporter it causes an infinite loop. The SimpleSpanProcessor isn't intended to be used for anything more than testing. If you swap it out for a BatchSpanProcessor it will behave correctly.

If you used something like this :

      OpenTelemetry.registerTracerProvider(tracerProvider: TracerProviderBuilder()
          .add(spanProcessor: BatchSpanProcessor(spanExporter: otlpHttpTraceExporter, scheduleDelay: 60)) // 上报至可观测链路 OpenTelemetry 版
            .add(spanProcessor: spanProcessor)
            .with(resource: resource)
            .build())

It will prevent the repeated trace exports. This will limit the upload of spans to minutely