servers/otlp/
trace.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15pub mod attributes;
16pub mod span;
17pub mod v0;
18pub mod v1;
19
20use api::v1::RowInsertRequests;
21pub use common_catalog::consts::{
22    PARENT_SPAN_ID_COLUMN, SPAN_ID_COLUMN, SPAN_NAME_COLUMN, TRACE_ID_COLUMN,
23};
24use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest;
25use pipeline::{GreptimePipelineParams, PipelineWay};
26use session::context::QueryContextRef;
27
28use crate::error::{NotSupportedSnafu, Result};
29use crate::query_handler::PipelineHandlerRef;
30
31// column names
32pub const SERVICE_NAME_COLUMN: &str = "service_name";
33pub const TIMESTAMP_COLUMN: &str = "timestamp";
34pub const DURATION_NANO_COLUMN: &str = "duration_nano";
35pub const SPAN_KIND_COLUMN: &str = "span_kind";
36pub const SPAN_STATUS_CODE: &str = "span_status_code";
37pub const SPAN_STATUS_MESSAGE_COLUMN: &str = "span_status_message";
38pub const SPAN_ATTRIBUTES_COLUMN: &str = "span_attributes";
39pub const SPAN_EVENTS_COLUMN: &str = "span_events";
40pub const SCOPE_NAME_COLUMN: &str = "scope_name";
41pub const SCOPE_VERSION_COLUMN: &str = "scope_version";
42pub const RESOURCE_ATTRIBUTES_COLUMN: &str = "resource_attributes";
43pub const TRACE_STATE_COLUMN: &str = "trace_state";
44
45// const keys
46pub const KEY_SERVICE_NAME: &str = "service.name";
47pub const KEY_SERVICE_INSTANCE_ID: &str = "service.instance.id";
48pub const KEY_SPAN_KIND: &str = "span.kind";
49
50// jaeger const keys, not sure if they are general
51pub const KEY_OTEL_SCOPE_NAME: &str = "otel.scope.name";
52pub const KEY_OTEL_SCOPE_VERSION: &str = "otel.scope.version";
53pub const KEY_OTEL_STATUS_CODE: &str = "otel.status_code";
54pub const KEY_OTEL_STATUS_MESSAGE: &str = "otel.status_description";
55pub const KEY_OTEL_STATUS_ERROR_KEY: &str = "error";
56pub const KEY_OTEL_TRACE_STATE: &str = "w3c.tracestate";
57
58/// The span kind prefix in the database.
59/// If the span kind is `server`, it will be stored as `SPAN_KIND_SERVER` in the database.
60pub const SPAN_KIND_PREFIX: &str = "SPAN_KIND_";
61
62// The span status code prefix in the database.
63pub const SPAN_STATUS_PREFIX: &str = "STATUS_CODE_";
64pub const SPAN_STATUS_UNSET: &str = "STATUS_CODE_UNSET";
65pub const SPAN_STATUS_ERROR: &str = "STATUS_CODE_ERROR";
66
67/// Convert SpanTraces to GreptimeDB row insert requests.
68/// Returns `InsertRequests` and total number of rows to ingest
69pub fn to_grpc_insert_requests(
70    request: ExportTraceServiceRequest,
71    pipeline: PipelineWay,
72    pipeline_params: GreptimePipelineParams,
73    table_name: String,
74    query_ctx: &QueryContextRef,
75    pipeline_handler: PipelineHandlerRef,
76) -> Result<(RowInsertRequests, usize)> {
77    match pipeline {
78        PipelineWay::OtlpTraceDirectV0 => v0::v0_to_grpc_insert_requests(
79            request,
80            pipeline,
81            pipeline_params,
82            table_name,
83            query_ctx,
84            pipeline_handler,
85        ),
86        PipelineWay::OtlpTraceDirectV1 => v1::v1_to_grpc_insert_requests(
87            request,
88            pipeline,
89            pipeline_params,
90            table_name,
91            query_ctx,
92            pipeline_handler,
93        ),
94        _ => NotSupportedSnafu {
95            feat: "Unsupported pipeline for trace",
96        }
97        .fail(),
98    }
99}