forked from DataDog/dd-sdk-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatadogAlamofireExtension.swift
39 lines (31 loc) · 1.68 KB
/
DatadogAlamofireExtension.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2019-Present Datadog, Inc.
*/
import class Datadog.URLSessionInterceptor
import Alamofire
/// An `Alamofire.EventMonitor` which instruments `Alamofire.Session` with Datadog RUM and Tracing.
public class DDEventMonitor: EventMonitor {
public init() {}
public func request(_ request: Request, didCreateTask task: URLSessionTask) {
URLSessionInterceptor.shared?.taskCreated(task: task)
}
public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
URLSessionInterceptor.shared?.taskMetricsCollected(task: task, metrics: metrics)
}
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
URLSessionInterceptor.shared?.taskCompleted(task: task, error: error)
}
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
URLSessionInterceptor.shared?.taskReceivedData(task: dataTask, data: data)
}
}
/// An `Alamofire.RequestInterceptor` which instruments `Alamofire.Session` with Datadog RUM and Tracing.
public class DDRequestInterceptor: RequestInterceptor {
public init() {}
public func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) {
let instrumentedRequest = URLSessionInterceptor.shared?.modify(request: urlRequest)
completion(.success(instrumentedRequest ?? urlRequest))
}
}