LoginSignup
1
1

More than 5 years have passed since last update.

AzureServiceBusとAzureFunctionsのサービスをAzureApplicationInsightsとSlackで監視する

Posted at

GYAOのtsです。
我々のチームは、オールパブリッククラウドで、Microservice Architectureを採用した次期バックエンドを設計中です。

経緯

前回の投稿でfunctionsを使用したAPIの作成と監視についてチーム内で話し合ったが、チームとして下記の方向で概ね合意が取れているので、この記事で監視も含めてプロトタイプを作成してみる。構成は下記のような形。
zeoliteLatestDiagram (1).png
今回のscopeはPowerBIとLogicAppsの部分は割愛する(別記事で)。

Hands on

今回実現したいことは以下の通り。

■ AzureFunctionsでAPIを2本作成する。

・DB登録用API・・・store
・DB削除用API・・・remove

■ storeAPI、removeAPIはAzureServiceBusの特定のトピック(storeトピック)にpublish。

■ AzureFunctions(subscriber)がAzureServiceBusの特定のトピック(storeトピック)をsubscribe。DB(cosmosDB)に対して処理を行う。

以上、たったこんだけ。
+ 監視は適宜行います。

AzureServiceBus

まずはPublish、 Subscribeするトピックを作成します。
スクリーンショット 2017-07-07 14.34.07.png

スクリーンショット 2017-07-07 14.36.14.png

スクリーンショット 2017-07-07 15.46.41.png

AzureFunctions

store

Http-ServiceBusのPublisherを作成する。

詳しくは前回の投稿を参照。

スクリーンショット 2017-07-07 15.40.33.png

#r "Newtonsoft.Json"

using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, ICollector<string> outputSbQueue, TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    // Get request body
    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);

    if (data == null) {
        return req.CreateResponse(HttpStatusCode.BadRequest, 
            "Please pass a messsage in the request body.");
    }
    else {
        outputSbQueue.Add($"{data}");
        return req.CreateResponse(HttpStatusCode.OK, $"{data}");
    }
}

remove

同じHttp-ServiceBusのPublisherを作成するが、publishする際のデータ構造が変わっただけなので割愛

doStore

subscriber。topicに流れてきたデータをもとに実際にcosmosDBに対して処理を行う。

スクリーンショット 2017-07-07 15.44.10.png

using System;
using System.Threading.Tasks;

public static void Run(string mySbMsg, TraceWriter log, out object outputDocument)
{
    log.Info($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
    outputDocument = mySbMsg;
}

AzureAppInsights

Functions、ServiceBusの監視をApplicationsで一元管理します。
今回はErrorやExceptionがあった場合、slackにサマリーを飛ばします。

Functions

Functions自体の監視は前回の投稿を参照。
今回はそれに加えてErrorsWebhookという関数を作成した。Azureのリソースのメトリックアラートをすべてここに飛ばし、監視する。

using System.Net;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info("Error was occurred...");

    // Get request body
    dynamic data = await req.Content.ReadAsAsync<object>();
    log.Error($"{data}");

    return req.CreateResponse(HttpStatusCode.OK);
}

ServiceBus

servicebusに関しては
アクティビティログアラートで設定する形か?現在調査中。
スクリーンショット 2017-07-09 23.54.38.png

スクリーンショット 2017-07-09 23.51.05.png

webhookで先程作成したfunctions(ErrorsWebhookを指定する)

ApplicationInsightsとSlack

ApplicationInsightsで拾ったErrorについてはSlackに送信したいので下記を使用する。
cloudbot
定期便とエラー毎のレポートを送ってくれる

定期便
スクリーンショット 2017-07-10 0.04.28.png

エラーpush
スクリーンショット 2017-07-10 0.05.17.png

1
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
1
1