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 AI Search の 情報をApplication Insightsに送信

Posted at

Azure AI Search の Telemetry data を Python で Application Insights に送りました。以下の内容です。

もともとは、Portalで Azure AI Search リソースを開いて メニュー 設定 -> 検索トラフィックの分析 の内容をしようと思いました。ですが上記のリンクに以下のような記述があり、コードの内容は、"outdated"らしいです。そのため、独自に調べて実装しました。jsとc#しかないが、中身を見るとPythonでもできました。

The Search traffic analytics portal page is currently outdated and references an obsolete client libary. The workaround is to use code snippets from the azure-search-traffic-analytics GitHub repository. This article includes code snippets from the GitHub repository.

そもそもの内容です。普通に検索語句などはログがとれるので、追加で何かをしたい場合のAdvanced scenario向けです(例: 検索結果一覧でのClick Streamを取得するなど)。

The pattern described in this article is for advanced scenarios and clickstream data generated by code you add to your client. In contrast, service logs are easy to set up, provide a range of metrics including search terms, and can be done in the Azure portal with no code required. We recommend that you enable logging for all scenarios. For more information, see Collect and analyze log data.

取れたログ

Application Insights で CustomEventsを見ています。あまりたいした情報付加していないのですが、含めているのはインデックス名、検索語句と検索時のFetch件数くらいです。
image.png
image.png

Python Script

前提

Application Insights リソースを作成済み。
関連するPython部分です。Ubuntu 22.04で動かしています。

project.toml
[tool.poetry.dependencies]
python = "^3.12"
azure-search-documents = "11.6.0b12"
azure-identity = "^1.24.0"
azure-monitor-opentelemetry = "^1.7.0"
azure-monitor-events-extension = "^0.1.0"

Sciript

大事なのはtrack_event関数呼んでいる部分です。

import uuid

from azure.identity import DefaultAzureCredential
from azure.monitor.events.extension import track_event
from azure.monitor.opentelemetry import configure_azure_monitor
from azure.search.documents import SearchClient

INDEX = <インデックス名>
service_endpoint = "https://<AI Search リソース名>.search.windows.net"
credential = DefaultAzureCredential()
client = SearchClient(endpoint=service_endpoint, 
                           index_name=INDEX,
                           credential=credential)


# Configure OpenTelemetry to use Azure Monitor with the 
# APPLICATIONINSIGHTS_CONNECTION_STRING environment variable.
configure_azure_monitor(
    connection_string="<Application Insights 接続子>",
)

search_id = str(uuid.uuid4())
search_text = "だるい"
results =  client.search(query_type='simple',
                         headers={"x-ms-client-request-id": search_id,
                                  "x-ms-azs-return-searchid": "true",
                                  "Access-Control-Expose-Headers": "x-ms-azs-searchid" 
                                  }, # Header追加
                         search_text=search_text ,
                         include_total_count=True)

print ('Total Documents Matching Query:', results.get_count())

# Indexが持つ項目名に合わせる
for result in results:
    print(result["@search.score"])
    print(f"document_title: {result['document_title']}")
    print(f"content: {result["content_text"]}")

# ここでCustom Eventsを送信
track_event("search", {"searchId": search_id, 
                       "serviceName": "ais-rag-test", 
                       "indexName": INDEX,
                       "serchText": search_text,
                       "resultsCount": results.get_count()})    

参考リンク

C#とjavascript向けのサンプル

Azure Monitor OpenTelemetryの基本

たくさんのAzure Monitor関連Pythonサンプル

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?