Opentelemetry——Instrumentation-Code-based

2024-05-24 19:23:55 浏览数 (1)

大纲
  • *Code-based*
  • *Import the OpenTelemetry API and SDK*
  • *Configure the OpenTelemetry API*
  • *Configure the OpenTelemetry SDK*
  • *Create Telemetry Data*
  • *Export Data*

Code-based

Learn the essential steps in setting up code-based instrumentation 了解设置基于代码的测量装置的基本步骤

Import the OpenTelemetry API and SDK

导入 OpenTelemetry API 和 SDK You’ll first need to import OpenTelemetry to your service code. If you’re developing a library or some other component that is intended to be consumed by a runnable binary, then you would only take a dependency on the API. If your artifact is a standalone process or service, then you would take a dependency on the API and the SDK. For more information about the OpenTelemetry API and SDK, see the specification. 您首先需要将 OpenTelemetry 导入到您的服务代码中。如果您正在开发一个旨在由可运行二进制文件使用的库或其他组件,那么您只需依赖 API。如果您的操作对象是独立的进程或服务,那么您将依赖API和SDK。有关OpenTelemetry API和SDK的更多信息,请参阅规范。

Configure the OpenTelemetry API

配置OpenTelemetry API In order to create traces or metrics, you’ll need to first create a tracer and/or meter provider. In general, we recommend that the SDK should provide a single default provider for these objects. You’ll then get a tracer or meter instance from that provider, and give it a name and version. The name you choose here should identify what exactly is being instrumented – if you’re writing a library, for example, then you should name it after your library (for example com.example.myLibrary) as this name will namespace all spans or metric events produced. It is also recommended that you supply a version string (i.e., semver:1.0.0) that corresponds to the current version of your library or service. 为了创建Traces或Metrics,您需要首先创建Tracer Provider或者Meter Provider。一般来说,我们建议SDK应该为这些对象提供单个默认的Provider。然后,您将从该Provider处获取Tracer或Meter实例,并为其指定名称和版本。您在此处选择的名称应该精确表达正在检测的具体内容—— 例如,如果您正在编写一个库,那么您应该以您的库命名它(例如 com.example.myLibrary),因为该名称将为所有生成的Span或Metric Events提供命名空间。还建议您提供与库或服务的当前版本号相对应的字符串作为版本信息(比如semver:1.0.0)。

Configure the OpenTelemetry SDK

配置OpenTelemetry SDK If you’re building a service process, you’ll also need to configure the SDK with appropriate options for exporting your telemetry data to some analysis backend. We recommend that this configuration be handled programmatically through a configuration file or some other mechanism. There are also per-language tuning options you may wish to take advantage of. 如果您正在构建服务进程,您还需要使用适当的选项配置 SDK,以将遥测数据导出到某些分析后端。我们建议通过配置文件或其他机制以自动化处理的方式处理此配置。您可能还希望利用到每种语言的调整选项。

Create Telemetry Data

创建遥测数据 Once you’ve configured the API and SDK, you’ll then be free to create traces and metric events through the tracer and meter objects you obtained from the provider. Make use of Instrumentation Libraries for your dependencies – check out the registry or your language’s repository for more information on these. 配置了API和SDK后,您就可以通过从Provider处获得的Tracer和Meter对象来自由创建Traces和Metric Events。使用 Instrumentation Libraries(测量装置库) 作为您的依赖项——查看注册表或您的语言的存储库以获取有关这些的更多信息。

Export Data

导出数据 Once you’ve created telemetry data, you’ll want to send it somewhere. OpenTelemetry supports two primary methods of exporting data from your process to an analysis backend, either directly from a process or by proxying it through the OpenTelemetry Collector. 一旦您创建了遥测数据后,您就需要将其发送到某个地方。OpenTelemetry 支持两种将数据从您的进程导出到分析后端的方法:直接从进程导出或通过OpenTelemetry Collector进行代理。

In-process export requires you to import and take a dependency on one or more exporters, libraries that translate OpenTelemetry’s in-memory span and metric objects into the appropriate format for telemetry analysis tools like Jaeger or Prometheus. In addition, OpenTelemetry supports a wire protocol known as OTLP, which is supported by all OpenTelemetry SDKs. This protocol can be used to send data to the OpenTelemetry Collector, a standalone binary process that can be run as a proxy or sidecar to your service instances or run on a separate host. The Collector can then be configured to forward and export this data to your choice of analysis tools. 进程内导出数据要求您引入并依赖一个或多个导出器、库,这些库将 OpenTelemetry 的内存中Span和Metric对象转换为适合遥测数据分析工具(如 Jaeger 或 Prometheus)需要的格式。此外,OpenTelemetry 支持一种名为OTLP的传输协议。所有 OpenTelemetry SDK 都支持该协议。该协议可用于将数据发送到 OpenTelemetry Collector,这是一个独立的二进制进程,可以作为代理或sidecar运行在你的服务实例上或在单独的主机上。然后可以配置OpenTelemetry Collector以将此数据转发并导出到您选择的分析工具。

In addition to open source tools such as Jaeger or Prometheus, a growing list of companies support ingesting telemetry data from OpenTelemetry. For details, see Vendors. 除了 Jaeger 或 Prometheus 等开源工具之外,越来越多的公司支持从 OpenTelemetry 获取遥测数据。有关详细信息,请参阅 供应商。

0 人点赞