OpenTelemetry Protocol (OTLP)
OpenTelemetry 是一个供应商中立的开源可观测性框架,用于检测、生成、收集和导出观测数据,例如 traces, metrics 和 logs。 OpenTelemetry Protocol (OTLP) 定义了观测数据在观测源和中间进程(例如收集器和观测后端)之间的编码、传输机制。
OpenTelemetry Collectors
你可以很简单地将 GreptimeDB 配置为 OpenTelemetry 采集器写入的目标。 有关更多信息,请参阅 OTel Collector 和Grafana Alloy 示例。
HTTP 基础端点
适用于所有信号类型的HTTP 基础端点 URL:http{s}://<host>/v1/otlp
当需要将多种信号类型(指标、日志和链路 追踪)发送到同一目标数据库时,这个统一端点非常有用,可以简化你的 OpenTelemetry 配置。
Metrics
GreptimeDB 通过原生支持 OTLP/HTTP 协议,可以作为后端存储服务来接收 OpenTelemetry 指标数据。
OTLP/HTTP API
使用下面的信息通过 Opentelemetry SDK 库发送 Metrics 到 GreptimeDB:
- URL:
https://<host>/v1/otlp/v1/metrics - Headers:
X-Greptime-DB-Name:<dbname>
Authorization:Basic认证,是<username>:<password>的 Base64 编码字符串。更多信息请参考 鉴权 和 HTTP API。
请求中使用 binary protobuf 编码 payload,因此你需要使用支持 HTTP/protobuf 的包。例如,在 Node.js 中,可以使用 exporter-trace-otlp-proto;在 Go 中,可以使用 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp;在 Java 中,可以使用 io.opentelemetry:opentelemetry-exporter-otlp;在 Python 中,可以使用 opentelemetry-exporter-otlp-proto-http。
包名可能会根据 OpenTelemetry 的发展发生变化,因此建议你参考 OpenTelemetry 官方文档以获取最新信息。
请参考 Opentelementry 的官方文档获取它所支持的编程语言的更多信息。
示例代码
下面是一些编程语言设置请求的示例代码:
- TypeScript
- Go
- Java
- Python
const auth = Buffer.from(`${username}:${password}`).toString('base64')
const exporter = new OTLPMetricExporter({
url: `https://${dbHost}/v1/otlp/v1/metrics`,
headers: {
Authorization: `Basic ${auth}`,
'X-Greptime-DB-Name': db,
},
timeoutMillis: 5000,
})
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", *username, *password)))
exporter, err := otlpmetrichttp.New(
context.Background(),
otlpmetrichttp.WithEndpoint(*dbHost),
otlpmetrichttp.WithURLPath("/v1/otlp/v1/metrics"),
otlpmetrichttp.WithHeaders(map[string]string{
"X-Greptime-DB-Name": *dbName,
"Authorization": "Basic " + auth,
}),
otlpmetrichttp.WithTimeout(time.Second*5),
)
String endpoint = String.format("https://%s/v1/otlp/v1/metrics", dbHost);
String auth = username + ":" + password;
String b64Auth = new String(Base64.getEncoder().encode(auth.getBytes()));
OtlpHttpMetricExporter exporter = OtlpHttpMetricExporter.builder()
.setEndpoint(endpoint)
.addHeader("X-Greptime-DB-Name", db)
.addHeader("Authorization", String.format("Basic %s", b64Auth))
.setTimeout(Duration.ofSeconds(5))
.build();
auth = f"{username}:{password}"
b64_auth = base64.b64encode(auth.encode()).decode("ascii")
endpoint = f"https://{host}/v1/otlp/v1/metrics"
exporter = OTLPMetricExporter(
endpoint=endpoint,
headers={"Authorization": f"Basic {b64_auth}", "X-Greptime-DB-Name": db},
timeout=5)
你可以在 Github 中找到可执行的 Demo:Go, Java, Python, and Node.js.
示例代码可能会根据 OpenTelemetry 的发展发生变化,因此建议你参考 OpenTelemetry 官方文档以获取最新信息。
关于示例代码,请参考 Opentelementry 的官方文档获取它所支持的编程语言获取更多信息。
兼容 Prometheus
从 v0.16 开始,GreptimeDB 为 OTLP 指标写入引入了一种 Prometheus 兼容模式。
如果指标以这种兼容模式写入,你可以像查询 Prometheus 原生指标一样使用 PromQL 直接查询这些指标。
如果你之前没有使用过 OTLP 指标写入,那么 GreptimeDB 会默认使用新的兼容模式。 否则,GreptimeDB 会对已经存在的表保留原有的数据模型,只有对新创建的指标表使用兼容模式写入。
GreptimeDB 会首先对数据进行预处理,包括:
- 将指标名(表名)和 标签名转换成 Prometheus 风格的命名(例如:将
.替换为_)。具体信息请参考这里 - 默认丢弃一些 resource 属性和全部的 scope 属性。默认保存的 resource 属性列表可以参考这里。你可以通过配置项对这个行为进行调整
注意: OTLP 的 Sum 和 Histogram 指标的数据可能是增量时序(delta temporality)类型的。
GreptimeDB 将会直接保存它们,不会进行累计值(cumulative value)的计算。
参考这里获取更多背景信息。
你可以通过设置 HTTP 请求头来调整预处理的行为。以下是选项列表: You can set the HTTP headers to configure the pre-process behaviors. Here are the options:
x-greptime-otlp-metric-promote-all-resource-attrs: 保存所有 resource 资源。默认是false。x-greptime-otlp-metric-promote-resource-attrs: 如果不保存所有 resource 资源,需要保存的资源名称列表,用;连接。x-greptime-otlp-metric-ignore-resource-attrs: 如果保存所有的 resource 资源,需要丢弃的资源名称列表,用;连接。x-greptime-otlp-metric-promote-scope-attrs: 是否需要保存 scope 资源。默认是false。