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_ATTRIBUTES_COLUMN: &str = "span_attributes";
38pub const SPAN_EVENTS_COLUMN: &str = "span_events";
39pub const SCOPE_NAME_COLUMN: &str = "scope_name";
40pub const SCOPE_VERSION_COLUMN: &str = "scope_version";
41pub const RESOURCE_ATTRIBUTES_COLUMN: &str = "resource_attributes";
42
43// const keys
44pub const KEY_SERVICE_NAME: &str = "service.name";
45pub const KEY_SERVICE_INSTANCE_ID: &str = "service.instance.id";
46pub const KEY_SPAN_KIND: &str = "span.kind";
47
48// jaeger const keys, not sure if they are general
49pub const KEY_OTEL_SCOPE_NAME: &str = "otel.scope.name";
50pub const KEY_OTEL_SCOPE_VERSION: &str = "otel.scope.version";
51pub const KEY_OTEL_STATUS_CODE: &str = "otel.status_code";
52
53/// The span kind prefix in the database.
54/// If the span kind is `server`, it will be stored as `SPAN_KIND_SERVER` in the database.
55pub const SPAN_KIND_PREFIX: &str = "SPAN_KIND_";
56
57// The span status code prefix in the database.
58pub const SPAN_STATUS_PREFIX: &str = "STATUS_CODE_";
59pub const SPAN_STATUS_UNSET: &str = "STATUS_CODE_UNSET";
60
61/// Convert SpanTraces to GreptimeDB row insert requests.
62/// Returns `InsertRequests` and total number of rows to ingest
63pub fn to_grpc_insert_requests(
64    request: ExportTraceServiceRequest,
65    pipeline: PipelineWay,
66    pipeline_params: GreptimePipelineParams,
67    table_name: String,
68    query_ctx: &QueryContextRef,
69    pipeline_handler: PipelineHandlerRef,
70) -> Result<(RowInsertRequests, usize)> {
71    match pipeline {
72        PipelineWay::OtlpTraceDirectV0 => v0::v0_to_grpc_insert_requests(
73            request,
74            pipeline,
75            pipeline_params,
76            table_name,
77            query_ctx,
78            pipeline_handler,
79        ),
80        PipelineWay::OtlpTraceDirectV1 => v1::v1_to_grpc_insert_requests(
81            request,
82            pipeline,
83            pipeline_params,
84            table_name,
85            query_ctx,
86            pipeline_handler,
87        ),
88        _ => NotSupportedSnafu {
89            feat: "Unsupported pipeline for trace",
90        }
91        .fail(),
92    }
93}