1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Functions (Python) の HTTP トリガーを New Relic Agent で監視する方法

1
Last updated at Posted at 2026-07-09

概要

Azure Functions における Python の HTTP トリガー処理を、New Relic Agent 経由で New Relic 上で監視する方法を紹介します。

New Relic の下記のドキュメントにて紹介されている手順を参考にしています。

手順

1. New Relic にて接続情報を取得

NEW_RELIC_LICENSE_KEY環境変数に設定するキーを取得

New Relic のページにアクセスし、右上の自分のアカウント -> API Keysを選択します。

image.png

Create a keyを選択します。

image.png

Key typeIngest - Licenseを選択後、Nameに任意の名称を入力し、Create a keyを選択します。

image.png

表示されたキーの値を控えます。

image.png

2. Azure Functions にデプロイ

ローカル環境にて Azure Functions の開発環境の準備

image.png

requirements.txt の準備

azure-functions
newrelic

image.png

関数の処理のコードを function_app.py に記述

New Relic の初期化と登録を実施しています。

image.png

処理内容は現在時刻を表示するシンプルなものです。

image.png

image.png

コードの詳細はこちらを選択
import os
import logging
import time
from datetime import datetime, timezone

import azure.functions as func
import newrelic.agent

# New Relic エージェント初期化
newrelic.agent.initialize()

# New Relic アプリケーションを登録
APP_NAME = os.environ.get("NEW_RELIC_APP_NAME")
NR_APP = newrelic.agent.register_application(name=APP_NAME)

# Azure Functions アプリケーションインスタンス
app = func.FunctionApp()

# トリガータイプに応じた共通処理を実行する関数
def run_test(trigger_type: str) -> str:
    # 現在時刻(UTC)を ISO 8601 形式で取得
    now_utc = datetime.now(timezone.utc).isoformat()

    # トリガー開始をログに記録
    logging.info("%s trigger started. current_time_utc=%s", trigger_type, now_utc)

    # New Relic にトリガータイプをカスタム属性として送信
    newrelic.agent.add_custom_attribute("trigger.type", trigger_type)

    # 処理をシミュレートするための遅延 (2秒)
    time.sleep(2)

    # 完了時刻(UTC)を ISO 8601 形式で取得
    finished_at = datetime.now(timezone.utc).isoformat()
    # トリガー完了をログに記録
    logging.info("%s trigger finished. current_time_utc=%s", trigger_type, finished_at)

    return finished_at


# HTTP トリガー: New Relic 動作確認用エンドポイント
@app.function_name(name="http_newrelic_test")
@app.route(
    route="newrelic-test",
    methods=["GET"],
    auth_level=func.AuthLevel.FUNCTION,
)
def http_newrelic_test(req: func.HttpRequest) -> func.HttpResponse:
    # 共通処理を実行し、完了時刻を取得
    finished_at = run_test("http")

    # レスポンスを返す
    return func.HttpResponse(
        body=f"OK. finished_at_utc={finished_at}",
        status_code=200,
    )

Azure Function App に環境変数を設定

下記の環境変数を追加します。

  1. NEW_RELIC_LICENSE_KEY: New Relic から取得した API キーをセット
  2. NEW_RELIC_APP_NAME: New Relic 上で表示する Azure Functions アプリ名

image.png

Azure Function App にコードをデプロイ

image.png

3. Azure Function App の関数の実行と計測

Azure ポータルにてデプロイした関数を表示

デプロイしたhttp_newrelic_test関数を選択します。

image.png

テスト/実行を選択します。

image.png

HTTP メソッドにてGETを選択し、実行を選択します。

image.png

正常終了することを確認します。

image.png

New Relic にてデータを確認

New Relic にて APM & Services -> NEW_RELIC_APP_NAME環境変数に指定した名称を選択します。

image.png

データが連携されていることを確認します。

image.png

Transactions にて実行した関数であるhttp_newrelic_testを確認できます。

image.png

New Relic 上でデータを確認

APM & Services の Summary

image.png

image.png

image.png

NRQL でデータを確認

Metric

SELECT *
FROM Metric 
WHERE `entity.name` = 'new-relic-test-01'
SINCE 30 minutes ago UNTIL now

image.png

1 行目

項目
Timestamp July 09, 2026 22:58:58
App Name new-relic-test-01
End Timestamp 1783605596069
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity.name new-relic-test-01
Metric Name newrelic.goldenmetrics.apm.application.errorRate
Newrelic.goldenmetrics.apm.application.error Rate {"type":"summary","count":1,"sum":0.0,"min":0.0,"max":0.0}
Newrelic.source goldenMetrics
Value Function average
Newrelic.goldenmetrics.apm.application.throughput
Newrelic.goldenmetrics.apm.application.response Time Ms

2 行目

項目
Timestamp July 09, 2026 22:58:58
App Name new-relic-test-01
End Timestamp 1783605596069
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity.name new-relic-test-01
Metric Name newrelic.goldenmetrics.apm.application.throughput
Newrelic.goldenmetrics.apm.application.error Rate
Newrelic.source goldenMetrics
Value Function count
Newrelic.goldenmetrics.apm.application.throughput {"type":"summary","count":1,"sum":2001.5490055084229,"min":2001.5490055084229,"max":2001.5490055084229}
Newrelic.goldenmetrics.apm.application.response Time Ms

3 行目

項目
Timestamp July 09, 2026 22:58:58
App Name new-relic-test-01
End Timestamp 1783605596069
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity.name new-relic-test-01
Metric Name newrelic.goldenmetrics.apm.application.responseTimeMs
Newrelic.goldenmetrics.apm.application.error Rate
Newrelic.source goldenMetrics
Value Function average
Newrelic.goldenmetrics.apm.application.throughput
Newrelic.goldenmetrics.apm.application.response Time Ms {"type":"summary","count":1,"sum":2001.5490055084229,"min":2001.5490055084229,"max":2001.5490055084229}

Log

SELECT * FROM `Log` SINCE 30 minutes ago UNTIL now

image.png

1 行目

項目
Timestamp July 09, 2026 22:58:58
App Name new-relic-test-01
End Timestamp 1783605596069
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity.name new-relic-test-01
Metric Name newrelic.goldenmetrics.apm.application.errorRate
Newrelic.goldenmetrics.apm.application.error Rate {"type":"summary","count":1,"sum":0.0,"min":0.0,"max":0.0}
Newrelic.source goldenMetrics
Value Function average
Newrelic.goldenmetrics.apm.application.throughput
Newrelic.goldenmetrics.apm.application.response Time Ms

2 行目

項目
Timestamp July 09, 2026 22:58:58
App Name new-relic-test-01
End Timestamp 1783605596069
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity.name new-relic-test-01
Metric Name newrelic.goldenmetrics.apm.application.throughput
Newrelic.goldenmetrics.apm.application.error Rate
Newrelic.source goldenMetrics
Value Function count
Newrelic.goldenmetrics.apm.application.throughput {"type":"summary","count":1,"sum":2001.5490055084229,"min":2001.5490055084229,"max":2001.5490055084229}
Newrelic.goldenmetrics.apm.application.response Time Ms

3 行目

項目
Timestamp July 09, 2026 22:58:58
App Name new-relic-test-01
End Timestamp 1783605596069
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity.name new-relic-test-01
Metric Name newrelic.goldenmetrics.apm.application.responseTimeMs
Newrelic.goldenmetrics.apm.application.error Rate
Newrelic.source goldenMetrics
Value Function average
Newrelic.goldenmetrics.apm.application.throughput
Newrelic.goldenmetrics.apm.application.response Time Ms {"type":"summary","count":1,"sum":2001.5490055084229,"min":2001.5490055084229,"max":2001.5490055084229}

Transaction

SELECT * FROM `Transaction` SINCE 30 minutes ago UNTIL now

image.png

1 行目

項目
Timestamp July 09, 2026 22:59:34
Apdex Perf Zone F
App Id 1460281244
App Name new-relic-test-01
Cloud.resource Id /subscriptions/3c24dcc0-XXXXXX/resourceGroups/flex-fd82eef5aa26943cbe5b768cb7b21b1dd55f6dba324e67443b05bc54726e761c/providers/Microsoft.Web/sites/new-relic-test-01/functions/http_
Duration 2.001549005508423
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity Guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Faas.cold Start true
Faas.invocation Id a51cadfb-386d-4cf3-88f0-6ce4a6bc1c22
Faas.name new-relic-test-01/http_newrelic_test
Faas.trigger http
Guid 88c7f2f3045e0beb
Host 0--4913294d-8c29-4210-8fbf-89a0c8efe3fc
Http.status Code 200
Name WebTransaction/AzureFunction/http_newrelic_test
Priority 2.161228
Real Agent Id 1579015556
Request.headers.accept application/json
Request.headers.content Length 0
Request.headers.content Type application/json
Request.headers.host new-relic-test-01-gcckh6fagsazafaf.japaneast-01.azurewebsites.net
Request.headers.user Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0
Request.method GET
Request.uri https://new-relic-test-01-gcckh6fagsazafaf.japaneast-01.azurewebsites.net/api/newrelic-test?code=********
Response.status 200
Sampled true
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439
Total Time 2.001549005508423
Trace Id 88c7f2f3045e0bebaff31782bf2acb97
Transaction Sub Type AzureFunction
Transaction Type Web
Trigger.type http

TransactionTrace

SELECT * FROM `TransactionTrace` SINCE 60 minutes ago UNTIL now

image.png

1 行目

項目
Timestamp July 09, 2026 22:59:55
Agent Run Id -1432513614
Application Ids :1460281244:
Duration 2.001
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Guid 88c7f2f3045e0beb
Id 770aeac2-7b9e-11f1-9880-ceba72b4138b_0_1211
Path WebTransaction/AzureFunction/http_newrelic_test
Protocol Version 17
Real Agent Id 1579015556
Storage Id 770aeac2-7b9e-11f1-9880-ceba72b4138b_0_1211
Uri https://new-relic-test-01-gcckh6fagsazafaf.japaneast-01.azurewebsites.net/api/newrelic-test?code=********

ApplicationAgentContext

SELECT * FROM `ApplicationAgentContext` SINCE 30 minutes ago UNTIL now

image.png

1 行目

項目
Timestamp July 09, 2026 22:57:54
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--4913294d-8c29-4210-8fbf-89a0c8efe3fc
Host.boot Id a36dc5de-0d75-476b-95d9-d919591a73cc
Host.display Name 0--4913294d-8c29-4210-8fbf-89a0c8efe3fc
Instance Name 0--4913294d-8c29-4210-8fbf-89a0c8efe3fc new-relic-test-01
License Key ********
Real Agent Id 1579015556
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

2 行目

項目
Timestamp July 09, 2026 22:57:54
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--029aaa34-8a2f-4aee-98b7-b2dfa99a390a
Host.boot Id 98d9e24c-14aa-4df0-bf72-7094444c4545
Host.display Name 0--029aaa34-8a2f-4aee-98b7-b2dfa99a390a
Instance Name 0--029aaa34-8a2f-4aee-98b7-b2dfa99a390a new-relic-test-01
License Key ********
Real Agent Id 1579007323
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

3 行目

項目
Timestamp July 09, 2026 22:57:54
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--a3862ecf-3499-4fdb-b6f8-4444686e29f1
Host.boot Id 1c31a1d9-4de2-4b5b-adbe-9066d3321fe3
Host.display Name 0--a3862ecf-3499-4fdb-b6f8-4444686e29f1
Instance Name 0--a3862ecf-3499-4fdb-b6f8-4444686e29f1 new-relic-test-01
License Key ********
Real Agent Id 1579025538
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

4 行目

項目
Timestamp July 09, 2026 22:57:47
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--0804f0ed-bd97-475b-a2b0-1d23d97eb94a
Host.boot Id e5e06185-a4ec-4959-bed0-b2f90752b22a
Host.display Name 0--0804f0ed-bd97-475b-a2b0-1d23d97eb94a
Instance Name 0--0804f0ed-bd97-475b-a2b0-1d23d97eb94a new-relic-test-01
License Key ********
Real Agent Id 1579016483
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

5 行目

項目
Timestamp July 09, 2026 22:57:47
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--9ff32ca1-ffe7-41e0-b572-317f8221feac
Host.boot Id 090de6d7-5f80-4db7-a922-696a8e7fbb0b
Host.display Name 0--9ff32ca1-ffe7-41e0-b572-317f8221feac
Instance Name 0--9ff32ca1-ffe7-41e0-b572-317f8221feac new-relic-test-01
License Key ********
Real Agent Id 1579019648
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

6 行目

項目
Timestamp July 09, 2026 22:57:46
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--88c7560d-6050-454e-a466-5ab8455048c5
Host.boot Id 2fb47049-d35a-488b-860d-bd66faaa6bb0
Host.display Name 0--88c7560d-6050-454e-a466-5ab8455048c5
Instance Name 0--88c7560d-6050-454e-a466-5ab8455048c5 new-relic-test-01
License Key ********
Real Agent Id 1579013754
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

7 行目

項目
Timestamp July 09, 2026 22:48:07
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--78a8761f-4a95-436e-807a-9039eb9e5eb2
Host.boot Id 8ab1eab3-680a-4975-ad81-d90107cb49a4
Host.display Name 0--78a8761f-4a95-436e-807a-9039eb9e5eb2
Instance Name 0--78a8761f-4a95-436e-807a-9039eb9e5eb2 new-relic-test-01
License Key ********
Real Agent Id 1579145335
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

8 行目

項目
Timestamp July 09, 2026 22:47:01
Agent.language python
Agent.version 13.2.0
App Id 1460281244
App Name new-relic-test-01
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Frontend Route unknown
Host 0--7b7233b6-7a72-4fd0-9876-3d944fed15fb
Host.boot Id 49bd22ab-1b97-400e-8759-f15b95b95013
Host.display Name 0--7b7233b6-7a72-4fd0-9876-3d944fed15fb
Instance Name 0--7b7233b6-7a72-4fd0-9876-3d944fed15fb new-relic-test-01
License Key ********
Real Agent Id 1579157374
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439

<追加検証> TransactionError への計測確認

エラーとなる関数のデプロイと実行

@app.function_name(name="http_newrelic_error_test")
@app.route(
    route="newrelic-error-test",
    methods=["GET"],
    auth_level=func.AuthLevel.FUNCTION,
)
def http_newrelic_error_test(req: func.HttpRequest) -> func.HttpResponse:
    try:
        logging.info("HTTP error test started.")
        raise RuntimeError("New Relic TransactionError test")

    except Exception:
        logging.exception("HTTP error test failed.")

        # except 内なので、引数なしで現在処理中の例外を記録できる
        newrelic.agent.notice_error(
            attributes={
                "trigger.type": "http",
                "function.name": "http_newrelic_error_test",
                "error.test": True,
            },
            status_code=500,
        )

        return func.HttpResponse(
            body="Internal Server Error. New Relic error test.",
            status_code=500,
        )

image.png

NRQL で確認

TransactionError

SELECT * FROM `TransactionError` SINCE 5 minutes ago UNTIL now

image.png

最新の CSV は 1 行のみです。行列変換して、1 行を縦表で表示します。

Request.uricode= は Azure Functions のキーに相当するため、記事掲載用にマスクしています。JSON 列はありません。

1 行目

項目
Timestamp July 10, 2026 0:21:19
Aggregate Facet WebTransaction/AzureFunction/http_newrelic_error_test::builtins:RuntimeError
App Id 1460281244
App Name new-relic-test-01
Cloud.resource Id /subscriptions/3c24dcc0-XXX/resourceGroups/flex-fd82eef5aa26943cbe5b768cb7b21b1dd55f6dba324e67443b05bc54726e761c/providers/Microsoft.Web/sites/new-relic-test-01/functions/http_newrelic_error_test
Duration 0.0012698173522949219
Entity.guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Entity Guid ODI1MTQzOXxBUE18QVBQTElDQVRJT058MTQ2MDI4MTI0NA
Error.class builtins:RuntimeError
Error.expected false
Error.message New Relic TransactionError test
Error.test true
Faas.cold Start true
Faas.invocation Id 3fd8116f-f4e0-46d0-8b99-99e1f8374d05
Faas.name new-relic-test-01/http_newrelic_error_test
Faas.trigger http
Function.name http_newrelic_error_test
Guid 9f740a894d29c146
Host 0--0987d128-34cd-4ed1-bae9-19c2f0bf0538
Http.status Code 500
Priority 2.625797
Real Agent Id 1577762481
Request.headers.accept application/json
Request.headers.content Length 0
Request.headers.content Type application/json
Request.headers.host new-relic-test-01-gcckh6fagsazafaf.japaneast-01.azurewebsites.net
Request.headers.referer https://portal.azure.com/
Request.headers.user Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0
Request.method GET
Request.uri https://new-relic-test-01-gcckh6fagsazafaf.japaneast-01.azurewebsites.net/api/newrelic-error-test?code=********
Response.status 500
Sampled true
Span Id 9552fd7546ce4730
Tags.account Account 8251439
Tags.account Id 8251439
Tags.trusted Account Id 8251439
Thread.concurrency 0.9552554923019152
Trace Id 9f740a894d29c1460ad161c54fc1e036
Transaction Name WebTransaction/AzureFunction/http_newrelic_error_test
Transaction Ui Name /http_newrelic_error_test
Trigger.type http
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?