-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtrace.go
54 lines (45 loc) · 1.48 KB
/
trace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2021-2024 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package restful
import (
"net/http"
"github.com/nokia/restful/trace/tracer"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
var isTraced = true
var serverName = ""
// SetOTel enables/disables Open Telemetry. By default it is disabled.
// Trace provider can be set, when enabling.
func SetOTel(enabled bool, tp *sdktrace.TracerProvider) {
tracer.SetOTel(enabled, tp)
defaultClient = NewClient()
}
// SetOTelGrpc enables Open Telemetry.
// Activates trace export to the OTLP gRPC collector target address defined.
// Port is 4317, unless defined otherwise in provided target string.
//
// Fraction tells the fraction of spans to report, unless parent is sampled.
//
// - Less or equal 0 means no sampling, unless parent is sampled.
// - Greater or equal 1 means always sampled.
// - Else the sampling fraction, e.g. 0.01 for 1%.
func SetOTelGrpc(target string, fraction float64) error {
return tracer.SetOTelGrpc(target, fraction)
}
// SetTrace can enable/disable HTTP tracing.
// By default trace header generation and propagation is enabled.
func SetTrace(b bool) {
isTraced = b
}
// SetServerName allows settings a server name.
// That can be used at span name formatting.
func SetServerName(s string) {
serverName = s
}
func spanNameFormatter(operation string, req *http.Request) string {
if serverName != "" {
return serverName + ":" + req.URL.Path
}
return req.URL.Path
}