OpenCensus azure监视器导出程序

opencensus-ext-azure的Python项目详细描述


pypi

安装

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')

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JavaPax考试:从非标准Maven存储库解析Karaf特性存储库(XML文件)   java Spring启动Bean创建异常   java中将ArrayList转换为数组的方法   Android Studio的java Unity插件。   java在CheckStyle中从方法计数中排除getter和setter   HibernateJava。sql。SQLSyntaxErrorException:表/视图“序列”不存在   与命令行程序Java vs C通信   java WebView膨胀异常   java在O(n)java8流中寻找两个列表的交集   java使用Gradle运行单元测试时,最大堆大小在哪里设置?   ssl加载java应用程序(CXF)内的jks文件   CI:Jenkins Git:Simple Java项目:希望在特定时间在脚本上发送消息   java根据位置更改数字   java按数值排序字符串数组   macos java版本“1.6.0_65”是否与java 6模棱两可?   Cassandra中的java时间戳