Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@ worker:
# linuxDo
linuxDo:
api_key: "<LINUX_DO_API_KEY>"

# OpenTelemetry 配置
otel:
sampling_rate: 0.1 # 采样率 0.0-1.0
6 changes: 6 additions & 0 deletions internal/config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type configModel struct {
Worker workerConfig `mapstructure:"worker"`
ClickHouse clickHouseConfig `mapstructure:"clickhouse"`
LinuxDo linuxDoConfig `mapstructure:"linuxdo"`
Otel otelConfig `mapstructure:"otel"`
}

// appConfig 应用基本配置
Expand Down Expand Up @@ -141,3 +142,8 @@ type workerConfig struct {
type linuxDoConfig struct {
ApiKey string `mapstructure:"api_key"`
}

// otelConfig OpenTelemetry 配置
type otelConfig struct {
SamplingRate float64 `mapstructure:"sampling_rate"`
}
39 changes: 39 additions & 0 deletions internal/otel_trace/sampler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2025 linux.do
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package otel_trace

import (
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

// ParentBasedErrorAwareSampler 创建父级感知的概率采样器
// - 如果父 Span 已采样,则子 Span 也采样
// - 如果父 Span 未采样,则子 Span 也不采样
// - 如果是根 Span,按 samplingRate 概率采样
func ParentBasedErrorAwareSampler(samplingRate float64) sdktrace.Sampler {
return sdktrace.ParentBased(
sdktrace.TraceIDRatioBased(samplingRate),
)
}
2 changes: 2 additions & 0 deletions internal/otel_trace/trace_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package otel_trace

import (
"context"

"github.com/linux-do/cdk/internal/config"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
Expand Down Expand Up @@ -56,6 +57,7 @@ func newTracerProvider() (*sdktrace.TracerProvider, error) {
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(traceExporter),
sdktrace.WithResource(r),
sdktrace.WithSampler(ParentBasedErrorAwareSampler(config.Config.Otel.SamplingRate)),
)
return tracerProvider, nil
}
Loading