0
1

More than 3 years have passed since last update.

New Relic Mobile for iOSをインストールしてみる

Posted at

背景

New Relic Mobileを使ったことがなくて、無料枠の仕様が変更されていたので、New Relic agentのインストールをまとめてみました。

New Relicとは

New Relic社が提供しているSaaS型のFSOなパフォーマンス監視プラットフォームになります。今回試すMobile意外にもAPM、Infrastructure、Browser等の様々なサービスに対してパフォーマンスを監視できるようになります。
https://newrelic.com/jp

環境

Xcode: 11.5
Swift: 5.2.4
cocoapod: 1.9.3
New Relic SDK: 5.14.2

New Relic Mobile インストール手順

以下、New Relic公式ドキュメントの手順に従って、cocoapodを用いたインストールを行う。
https://docs.newrelic.co.jp/docs/mobile-monitoring/new-relic-mobile-ios/installation/cocoapods-installation

1.Xcodeでプロジェクトの作成

New Relic Mobileを試すために、プロジェクトを新規作成します。
Screen Shot 2021-01-04 at 18.02.25.png

今回はプロジェクト名はNewAppとします。
Screen Shot 2021-01-04 at 18.05.08.png

2. Podfileのinitとinstall

まず最初にPodfileを作成します。

% pod init
% vim Podfile

作成されたPodfileにpod 'NewRelicAgent', '5.14.2'を追加します。

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'NewApp' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for NewApp
  pod 'NewRelicAgent', '5.14.2'

  target 'NewAppTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'NewAppUITests' do
    # Pods for testing
  end

end

最後にインストールを実行します。

% pod install

3. NewRelicAgentとのインテグレーション

Pod install後に、作成されたNewApp.xcworkspaceを開きます。bridging headerを作成するために、File > New > File > Objective-Cを選択します。

Screen Shot 2021-01-04 at 18.15.22.png
Screen Shot 2021-01-04 at 18.15.35.png
Objective-Cのファイル名をPlaceholder.mとします。Nextを押下後、ポップアップが表示されるので、Create Bridging Headerを選択します。
Screen Shot 2021-01-04 at 18.15.46.png
Placeholder.mNewApp-Bridging-Header.hが作成されています。
Screen Shot 2021-01-04 at 18.16.16.png

NewApp-Bridging-Header.hを以下のように編集します。

NewApp-Bridging-Header.h
//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "NewRelicAgent/NewRelic.h"

AppDelegate.swiftも以下のように、didFinishLaunchingWithOptionsNewRelic.start(withApplicationToken:"<NewRelic Application Token>")を追加します。New Relic Application Tokenが不明な場合は、New Relic One上のWebUIに表示されているので、そこからTokenを取得してきます。

AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // 以下を追加
        NewRelic.start(withApplicationToken:"<NewRelic Application Token>")
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

最後にdSYMを自動的にアップロードするために、Select Build Phase > New Run Script Phaseで追加します。

Screen Shot 2021-01-04 at 18.34.43.png

追加後、以下のScriptを挿入します。

SCRIPT=`/usr/bin/find "${SRCROOT}" -name newrelic_postbuild.sh | head -n 1`

/bin/sh "${SCRIPT}" "<New Relic Application Token>"

Screen Shot 2021-01-04 at 18.35.05.png

4. シュミレータの起動

Xcodeでシュミレータを起動します。

5. New Relic Oneで確認

シュミレータを起動した後、New Relic One上のMobileを確認します。App Launchesのデータが更新されていることがわかり、New Relic agentのインストールが出来ました。他のデータに関しては、HTTP/Networkに関するコーディングをしていないので、何もありません。
Screen Shot 2021-01-04 at 18.41.31.png

所感

せっかくインストールまで出来たので、Alamofireなどを使って、他のデータもどのように表示されるか試してみようかと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1