OpenCensus azure监视器导出程序
opencensus-ext-azure的Python项目详细描述
安装
pip install opencensus-ext-azure
用法
对数
azure监视日志处理程序允许您将python日志导出到Azure Monitor。
此示例演示如何将警告级别日志发送到azure监视器。
- 创建azure监视器资源并获取检测密钥,可以找到更多信息here。
- 将检测键放入APPINSIGHTS_INSTRUMENTATIONKEY环境变量中。
- 您还可以在代码中显式指定检测键,它将优先于set环境变量。
importloggingfromopencensus.ext.azure.log_exporterimportAzureLogHandlerlogger=logging.getLogger(__name__)logger.addHandler(AzureLogHandler())logger.warning('Hello, World!')
您可以使用logging integration使用跟踪id和跨度id来丰富日志。
- 创建azure监视器资源并获取检测密钥,可以找到更多信息here。
- 使用pip install opencensus-ext-logging安装logging integration package。
- 将检测键放入APPINSIGHTS_INSTRUMENTATIONKEY环境变量中。
- 您还可以在代码中显式指定检测键,它将优先于set环境变量。
importloggingfromopencensus.ext.azure.log_exporterimportAzureLogHandlerfromopencensus.ext.azure.trace_exporterimportAzureExporterfromopencensus.traceimportconfig_integrationfromopencensus.trace.samplersimportProbabilitySamplerfromopencensus.trace.tracerimportTracerconfig_integration.trace_integrations(['logging'])logger=logging.getLogger(__name__)handler=AzureLogHandler()handler.setFormatter(logging.Formatter('%(traceId)s%(spanId)s%(message)s'))logger.addHandler(handler)tracer=Tracer(exporter=AzureExporter(),sampler=ProbabilitySampler(1.0))logger.warning('Before the span')withtracer.span(name='test'):logger.warning('In the span')logger.warning('After the span')
指标
azure监视器度量导出器允许您将度量导出到Azure Monitor。
- 创建azure监视器资源并获取检测密钥,可以找到更多信息here。
- 将检测键放入APPINSIGHTS_INSTRUMENTATIONKEY环境变量中。
- 您还可以在代码中显式指定检测键,它将优先于set环境变量。
importtimefromopencensus.ext.azureimportmetrics_exporterfromopencensus.statsimportaggregationasaggregation_modulefromopencensus.statsimportmeasureasmeasure_modulefromopencensus.statsimportstatsasstats_modulefromopencensus.statsimportviewasview_modulefromopencensus.tagsimporttag_mapastag_map_modulestats=stats_module.statsview_manager=stats.view_managerstats_recorder=stats.stats_recorderCARROTS_MEASURE=measure_module.MeasureInt("carrots","number of carrots","carrots")CARROTS_VIEW=view_module.View("carrots_view","number of carrots",[],CARROTS_MEASURE,aggregation_module.CountAggregation())defmain():# Enable metrics# Set the interval in seconds in which you want to send metricsexporter=metrics_exporter.new_metrics_exporter()view_manager.register_exporter(exporter)view_manager.register_view(CARROTS_VIEW)mmap=stats_recorder.new_measurement_map()tmap=tag_map_module.TagMap()mmap.measure_int_put(CARROTS_MEASURE,1000)mmap.record(tmap)# Default export interval is every 15.0s# Your application should run for at least this amount# of time so the exporter will meet this interval# Sleep can fulfill thistime.sleep(60)print("Done recording metrics")if__name__=="__main__":main()
导出器还包括一组默认导出到azure monitor的标准度量。
importpsutilimporttimefromopencensus.ext.azureimportmetrics_exporterdefmain():# All you need is the next line. You can disable standard metrics by# passing in enable_standard_metrics=False into the constructor of# new_metrics_exporter()_exporter=metrics_exporter.new_metrics_exporter()foriinrange(100):print(psutil.virtual_memory())time.sleep(5)print("Done recording metrics")if__name__=="__main__":main()
下面是当前可用的标准度量列表:
- 可用内存(字节)
- CPU处理器时间(百分比)
- 传入请求速率(每秒)
- 传出请求速率(每秒)
- 进程CPU使用率(百分比)
- 处理专用字节(字节)
痕量
azure监视器跟踪导出器允许您将OpenCensus跟踪导出到Azure Monitor。
此示例演示如何向azure monitor发送一个span“hello”。
- 创建azure监视器资源并获取检测密钥,可以找到更多信息here。
- 将检测键放入APPINSIGHTS_INSTRUMENTATIONKEY环境变量中。
- 您还可以在代码中显式指定检测键,它将优先于set环境变量。
fromopencensus.ext.azure.trace_exporterimportAzureExporterfromopencensus.trace.samplersimportProbabilitySamplerfromopencensus.trace.tracerimportTracertracer=Tracer(exporter=AzureExporter(),sampler=ProbabilitySampler(1.0))withtracer.span(name='hello'):print('Hello, World!')
也可以在代码中显式指定检测键。
- 创建azure监视器资源并获取检测密钥,可以找到更多信息here。
- 使用pip install opencensus-ext-requests安装requests integration package。
- 将检测键放入APPINSIGHTS_INSTRUMENTATIONKEY环境变量中。
- 您还可以在代码中显式指定检测键,它将优先于set环境变量。
importrequestsfromopencensus.ext.azure.trace_exporterimportAzureExporterfromopencensus.traceimportconfig_integrationfromopencensus.trace.samplersimportProbabilitySamplerfromopencensus.trace.tracerimportTracerconfig_integration.trace_integrations(['requests'])tracer=Tracer(exporter=AzureExporter(# TODO: replace this with your own instrumentation key.instrumentation_key='00000000-0000-0000-0000-000000000000',),sampler=ProbabilitySampler(1.0),)withtracer.span(name='parent'):response=requests.get(url='https://www.wikipedia.org/wiki/Rabbit')