Skip to content

Commit

Permalink
* Datadog 지원 (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
devMinseok authored Sep 8, 2024
1 parent a67261a commit e85ec21
Show file tree
Hide file tree
Showing 29 changed files with 157 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@ Tuist/Dependencies/graph.json

## XCFramework ##
XCFramework/Cartfile.resolved
XCFramework/Carthage
XCFramework/Carthage

## Env ##
XCConfig/Env.xcconfig
27 changes: 27 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "1.0.0"
}
},
{
"identity" : "dd-sdk-ios",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Datadog/dd-sdk-ios.git",
"state" : {
"revision" : "c4cb52b90dbddde8e0ffc7d89f2b6e5680acce7e",
"version" : "2.16.0"
}
},
{
"identity" : "lottie-spm",
"kind" : "remoteSourceControl",
Expand All @@ -18,6 +27,24 @@
"version" : "4.5.0"
}
},
{
"identity" : "opentelemetry-swift-packages",
"kind" : "remoteSourceControl",
"location" : "https://github.com/DataDog/opentelemetry-swift-packages.git",
"state" : {
"revision" : "adcfc3d7fc6ad411bb576cfa4512c2d3f8b2dbc7",
"version" : "1.6.0"
}
},
{
"identity" : "plcrashreporter",
"kind" : "remoteSourceControl",
"location" : "https://github.com/microsoft/plcrashreporter.git",
"state" : {
"revision" : "6752f71de206f6a53fa6a758c3660fd9a7fe7527",
"version" : "1.11.2"
}
},
{
"identity" : "rive-ios",
"kind" : "remoteSourceControl",
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let package: Package = .init(
.package(url: "https://github.com/pointfreeco/swift-composable-architecture.git", exact: "1.11.2"),
.package(url: "https://github.com/pointfreeco/swift-dependencies.git", exact: "1.3.3"),
.package(url: "https://github.com/rive-app/rive-ios.git", exact: "5.15.1"),
.package(url: "https://github.com/airbnb/lottie-spm.git", exact: "4.5.0")
.package(url: "https://github.com/airbnb/lottie-spm.git", exact: "4.5.0"),
.package(url: "https://github.com/Datadog/dd-sdk-ios.git", exact: "2.16.0")
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public extension DEP.SPMTarget {
static let dependencies: TargetDependency = .external(name: "Dependencies")
static let riveRuntime: TargetDependency = .external(name: "RiveRuntime")
static let lottie: TargetDependency = .external(name: "Lottie")
static let datadogCore: TargetDependency = .external(name: "DatadogCore")
static let datadogRUM: TargetDependency = .external(name: "DatadogRUM")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public protocol APIBaseRequest {
}

public enum API {
public static var apiBaseURL: String {
public static var apiBaseHost: String {
guard let baseURL = Bundle.main.object(forInfoDictionaryKey: "BASE_URL") as? String else {
fatalError("url missing")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// APIClientURLSessionDelegate.swift
// APIClientInterface
//
// Created by devMinseok on 9/8/24.
// Copyright © 2024 PomoNyang. All rights reserved.
//

import Foundation

final public class APIClientURLSessionDelegate: NSObject, URLSessionDataDelegate {
}
30 changes: 17 additions & 13 deletions Projects/Core/APIClient/Sources/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,27 @@ import Dependencies

extension APIClient: DependencyKey {
public static let liveValue: APIClient = .live()

public static func live() -> Self {

actor Session {
nonisolated let tokenInterceptor: TokenInterceptor

let session: URLSession = {
let config = URLSessionConfiguration.default
return URLSession(configuration: config)
return URLSession(
configuration: config,
delegate: APIClientURLSessionDelegate(),
delegateQueue: nil
)
}()

let decoder: JSONDecoder = JSONDecoder()

init(tokenInterceptor: TokenInterceptor) {
self.tokenInterceptor = tokenInterceptor
}

func sendRequest(
_ request: APIBaseRequest,
isWithInterceptor: Bool,
Expand All @@ -40,21 +44,21 @@ extension APIClient: DependencyKey {
guard retryCnt < 3 else {
throw throwNetworkErr(.timeOutError, statusCode: -1)
}

var urlRequest = try await request.asURLRequest()
if isWithInterceptor {
urlRequest = try await tokenInterceptor.adapt(urlRequest)
}
Logger.shared.log(category: .network, urlRequest.asRequestLog())

let (data, response) = try await session.data(for: urlRequest)

guard let httpResponse = response as? HTTPURLResponse else {
let error = NetworkError.noResponseError
Logger.shared.log(level: .error, category: .network, "API Error:\n\(dump(error))")
throw error
}

switch httpResponse.statusCode {
case 200..<300:
Logger.shared.log(category: .network, urlRequest.asResponseLog(data, httpResponse))
Expand All @@ -76,10 +80,10 @@ extension APIClient: DependencyKey {
return error
}
}

let tokenInterceptor = TokenInterceptor()
let session = Session(tokenInterceptor: tokenInterceptor)

return .init(
apiRequest: { request, isWithInterceptor in
return try await session.sendRequest(request, isWithInterceptor: isWithInterceptor)
Expand Down
2 changes: 1 addition & 1 deletion Projects/Core/APIClient/Sources/TokenInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct TokenInterceptor {
throw NetworkError.authorizationError
}

var urlRequest = URLRequest(url: URL(string: "https://" + API.apiBaseURL)!)
var urlRequest = URLRequest(url: URL(string: "https://" + API.apiBaseHost)!)
urlRequest.httpMethod = HTTPMethod.post.rawValue
urlRequest.setValue(
ContentType.json.rawValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum AuthAPIRequest {

extension AuthAPIRequest: APIBaseRequest {
public var baseURL: String {
return API.apiBaseURL
return API.apiBaseHost
}

public var path: String {
Expand Down
2 changes: 1 addition & 1 deletion Projects/Domain/CatService/Interface/API/CatAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum CatAPI {

extension CatAPI: APIBaseRequest {
public var baseURL: String {
return API.apiBaseURL
return API.apiBaseHost
}

public var path: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum CategoryAPI {

extension CategoryAPI: APIBaseRequest {
public var baseURL: String {
return API.apiBaseURL
return API.apiBaseHost
}

public var path: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum FocusTimeAPI {

extension FocusTimeAPI: APIBaseRequest {
public var baseURL: String {
return API.apiBaseURL
return API.apiBaseHost
}

public var path: String {
Expand Down
2 changes: 1 addition & 1 deletion Projects/Domain/UserService/Interface/API/UserAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public enum UserAPIrequest {

extension UserAPIrequest: APIBaseRequest {
public var baseURL: String {
return API.apiBaseURL
return API.apiBaseHost
}

public var path: String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import DesignSystem

import ComposableArchitecture
import RiveRuntime
import DatadogRUM

public struct NamingCatView: View {
@Bindable var store: StoreOf<NamingCatCore>
Expand Down Expand Up @@ -66,5 +67,6 @@ public struct NamingCatView: View {
.onAppear {
store.send(.onAppear)
}
.trackRUMView(name: "고양이 이름짓기")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import DesignSystem

import ComposableArchitecture
import RiveRuntime
import DatadogRUM

public struct SelectCatView: View {
@Bindable var store: StoreOf<SelectCatCore>
Expand Down Expand Up @@ -85,7 +86,10 @@ public struct SelectCatView: View {
) { store in
NamingCatView(store: store)
}
.onAppear { store.send(.onAppear) }
.onAppear {
store.send(.onAppear)
}
.trackRUMView(name: "고양이 선택")
}
}

Expand Down
54 changes: 52 additions & 2 deletions Projects/Feature/Feature/Sources/AppDelegateCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import UserNotificationClientInterface
import KeychainClientInterface
import DatabaseClientInterface
import AppService
import APIClientInterface

import ComposableArchitecture
import FirebaseCore
import FirebaseMessaging
import DatadogCore
import DatadogRUM

@Reducer
public struct AppDelegateCore {
Expand Down Expand Up @@ -48,11 +51,13 @@ public struct AppDelegateCore {
switch action {
case .didFinishLaunching:
UIApplication.shared.applicationIconBadgeNumber = 0
FirebaseApp.configure()
let userNotificationEventStream = userNotificationClient.delegate()
firebaseInitilize()
datadogInitilize()

Logger.shared.log("FCMToken: \(Messaging.messaging().fcmToken ?? "not generated")")

let userNotificationEventStream = userNotificationClient.delegate()

return .run { send in
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
Expand Down Expand Up @@ -93,4 +98,49 @@ public struct AppDelegateCore {
return .none
}
}

private func firebaseInitilize() {
FirebaseApp.configure()
}

private func datadogInitilize() {
// TODO: - 환경 값 가져오는 부분 개선하기
guard let appID = Bundle.main.object(forInfoDictionaryKey: "DATADOG_APP_ID") as? String,
let clientToken = Bundle.main.object(forInfoDictionaryKey: "DATADOG_TOKEN") as? String
else { return }

#if DEV
let environment = "dev"
#else
let environment = "prod"
#endif

Datadog.initialize(
with: Datadog.Configuration(
clientToken: clientToken,
env: environment,
site: .us5
),
trackingConsent: .granted
)

RUM.enable(
with: RUM.Configuration(
applicationID: appID,
uiKitViewsPredicate: DefaultUIKitRUMViewsPredicate(),
uiKitActionsPredicate: DefaultUIKitRUMActionsPredicate(),
urlSessionTracking: RUM.Configuration.URLSessionTracking(
firstPartyHostsTracing: .trace(hosts: [API.apiBaseHost], sampleRate: 20)
),
trackBackgroundEvents: true
)
)

URLSessionInstrumentation.enable(
with: URLSessionInstrumentation.Configuration(
delegateClass: APIClientURLSessionDelegate.self,
firstPartyHostsTracing: .trace(hosts: [API.apiBaseHost])
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
import DesignSystem

import ComposableArchitecture
import DatadogRUM

public struct CategorySelectView: View {
@Bindable var store: StoreOf<CategorySelectCore>
Expand Down Expand Up @@ -59,5 +60,6 @@ public struct CategorySelectView: View {
.onAppear {
store.send(.onAppear)
}
.trackRUMView(name: "카테고리 변경")
}
}
2 changes: 2 additions & 0 deletions Projects/Feature/HomeFeature/Sources/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DesignSystem
import Utils

import ComposableArchitecture
import DatadogRUM

public struct HomeView: View {
@Bindable var store: StoreOf<HomeCore>
Expand Down Expand Up @@ -171,5 +172,6 @@ public struct HomeView: View {
.onAppear {
store.send(.onAppear)
}
.trackRUMView(name: "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUI
import DesignSystem

import ComposableArchitecture
import DatadogRUM

public struct TimeSelectView: View {
@Bindable var store: StoreOf<TimeSelectCore>
Expand Down Expand Up @@ -51,5 +52,6 @@ public struct TimeSelectView: View {
.onAppear {
store.send(.onAppear)
}
.trackRUMView(name: "시간 변경")
}
}
2 changes: 2 additions & 0 deletions Projects/Feature/MyPageFeature/Sources/MyCat/MyCatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CatFeature
import DesignSystem

import ComposableArchitecture
import DatadogRUM

public struct MyCatView: View {
@Bindable var store: StoreOf<MyCatCore>
Expand Down Expand Up @@ -67,5 +68,6 @@ public struct MyCatView: View {
.onAppear {
store.send(.onAppear)
}
.trackRUMView(name: "나의 고양이")
}
}
Loading

0 comments on commit e85ec21

Please sign in to comment.