OpenCensus と Azure Application Insights を使用して Python でメトリックを送信しようとしています。
理想的には、任意の構造を持ついくつかの Python 辞書を送信したいのですが、OpenCensus は「自動的にログ/印刷ステートメントをリッスン」しているようですが、これらを検索しても Azure portal でその証拠は見つかりません。
OpenCensusprint(...)
にとって何か特別なことはありますか? printステートメントの内容をどのようにキャプチャしますか?
私は2つの異なることを試しました(コードについては以下を参照してください):
- 「Azure メトリック」の送信 ( https://pypi.org/project/opencensus-ext-azure/を参照してから、「メトリック」の段落を参照): これまでのところ、Azure ポータルに何も表示されず、アプリをクリックしてアプリケーション インサイト。「検索」タブで過去 24 時間監視しました。
- Azure 実装を介してある種の「スパン」を送信する ( https://github.com/census-instrumentation/opencensus-python/tree/master/contrib/opencensus-ext-azure#traceを参照): 「検索」タブで過去 24 時間をチェックすると、実際にはスパンを表すいくつかのイベントが (スパン名で) そこに表示されますが、そのキー/値属性などのメトリックは関連付けられていません。
原則としてAFAIK:
- 「スパン」を管理する「トレーサー」が必要です
- 親/子トレーサー/スパンが存在する可能性があります
- 各スパンは、「コレクター」にいくつかのメトリック (HTTP のもの、任意の辞書/JSON など) を送信できるようにする必要があります。
- メトリクス/メッセージが添付されたタイムラインにこれらの親/子スパンを表示するダッシュボード (Azure Application Insights など) が必要です。
私は理解したい:
- OpenCensus で任意の辞書を「メトリクス」として送信するにはどうすればよいですか? Application Insights 用のアプリを使用している場合、Azure ポータルにはどのように表示されますか?
- OpenCensusの
print(...)
(または) および HTTP 要求の特別な点は何ですか?logging.info(...)
その情報は、Application Insights 用アプリの Azure ポータルでどのように役立つでしょうか? - 上記はどういうわけかトレーサー/スパンにとらわれませんか、それともメトリクスを送信する必要があるときにスパンは必須ですか?
import json
import psutil
from opencensus.trace.samplers import AlwaysOnSampler
from opencensus.trace.tracer import Tracer
from opencensus.ext.azure import metrics_exporter
from opencensus.ext.azure.trace_exporter import AzureExporter
if __name__ == "__main__":
# loading the instrumentation key (for the Azure Application Insights app) from a JSON file
azure_conf = json.loads(open("tf/ai_details.json", 'r').read())
ai_instrumentation_key = azure_conf['instrumentation_key']['value']
# print(ai_instrumentation_key)
# test 1: trying to "send a metric", does that mean that the metric exporter listens to "print(...)"?
_me = metrics_exporter.new_metrics_exporter(connection_string='InstrumentationKey={}'.format(ai_instrumentation_key))
print(psutil.virtual_memory())
print("Done recording metrics")
# test 2: trying to "send a metric", how can I make the "span" to send a dictionary?
azure_exporter = AzureExporter(connection_string='InstrumentationKey={}'.format(ai_instrumentation_key))
# https://opencensus.io/api/python/trace/api/tracer.html
tracer = Tracer(exporter=azure_exporter, sampler=AlwaysOnSampler())
# https://opencensus.io/api/python/trace/api/span.html#opencensus.trace.span.Span
with tracer.span(name='TestSpan') as span:
print('Hello, World!') # is the span only listening to "print(...)"?
span.add_attribute("foo-span-key", "foo-span-value") # this does not seem to do anything