-
Notifications
You must be signed in to change notification settings - Fork 345
/
telemetry.go
202 lines (179 loc) · 7.25 KB
/
telemetry.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package telemetry
import (
"fmt"
"k8s.io/utils/pointer"
corev1 "k8s.io/api/core/v1"
"github.com/apache/camel-k/v2/addons/telemetry/discovery"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/trait"
"github.com/apache/camel-k/v2/pkg/util"
)
// The Telemetry trait can be used to automatically publish tracing information to an OTLP compatible collector.
//
// The trait is able to automatically discover the telemetry OTLP endpoint available in the namespace (supports **Jaerger** in version 1.35+).
//
// The Telemetry trait is disabled by default.
//
// WARNING: The Telemetry trait can't be enabled at the same time as the Tracing trait.
//
// +camel-k:trait=telemetry.
type Trait struct {
traitv1.Trait `property:",squash" json:",inline"`
// Enables automatic configuration of the trait, including automatic discovery of the telemetry endpoint.
Auto *bool `property:"auto" json:"auto,omitempty"`
// The name of the service that publishes telemetry data (defaults to the integration name)
ServiceName string `property:"service-name" json:"serviceName,omitempty"`
// The target endpoint of the Telemetry service (automatically discovered by default)
Endpoint string `property:"endpoint" json:"endpoint,omitempty"`
// The sampler of the telemetry used for tracing (default "on")
Sampler string `property:"sampler" json:"sampler,omitempty"`
// The sampler ratio of the telemetry used for tracing
SamplerRatio string `property:"sampler-ratio" json:"sampler-ratio,omitempty"`
// The sampler of the telemetry used for tracing is parent based (default "true")
SamplerParentBased *bool `property:"sampler-parent-based" json:"sampler-parent-based,omitempty"`
}
type telemetryTrait struct {
trait.BaseTrait
Trait `property:",squash"`
}
const (
propEnabled = "propEnabled"
propEndpoint = "propEndpoint"
propServiceName = "propServiceName"
propSampler = "propSampler"
propSamplerRatio = "propSamplerRatio"
propSamplerParentBased = "propSamplerParentBased"
)
var (
telemetryProperties = map[v1.RuntimeProvider]map[string]string{
v1.RuntimeProviderQuarkus: {
propEndpoint: "quarkus.opentelemetry.tracer.exporter.otlp.endpoint",
propServiceName: "quarkus.opentelemetry.tracer.resource-attributes",
propSampler: "quarkus.opentelemetry.tracer.sampler",
propSamplerRatio: "quarkus.opentelemetry.tracer.sampler.ratio",
propSamplerParentBased: "quarkus.opentelemetry.tracer.sampler.parent-based",
},
}
)
// NewTelemetryTrait instance the telemetry trait as a BaseTrait capable to inject quarkus properties.
func NewTelemetryTrait() trait.Trait {
return &telemetryTrait{
BaseTrait: trait.NewBaseTrait("telemetry", trait.TraitOrderBeforeControllerCreation),
}
}
func (t *telemetryTrait) Configure(e *trait.Environment) (bool, *trait.TraitCondition, error) {
if e.Integration == nil || !pointer.BoolDeref(t.Enabled, false) {
return false, nil, nil
}
if e.CamelCatalog == nil {
return false, trait.NewIntegrationConditionPlatformDisabledCatalogMissing(), nil
}
var condition *trait.TraitCondition
if pointer.BoolDeref(t.Auto, true) {
if t.Endpoint == "" {
for _, locator := range discovery.TelemetryLocators {
endpoint, err := locator.FindEndpoint(e.Ctx, t.Client, t.L, e)
if err != nil {
return false, nil, err
}
if endpoint != "" {
t.L.Infof("Using tracing endpoint: %s", endpoint)
condition = trait.NewIntegrationCondition(
"Telemetry",
v1.IntegrationConditionTraitInfo,
corev1.ConditionTrue,
"TracingEndpoint",
endpoint,
)
t.Endpoint = endpoint
break
}
}
}
if t.ServiceName == "" {
t.ServiceName = e.Integration.Name
}
if t.Sampler == "" {
t.Sampler = "on"
}
}
return true, condition, nil
}
func (t *telemetryTrait) Apply(e *trait.Environment) error {
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, v1.CapabilityTelemetry)
if e.CamelCatalog.Runtime.Capabilities["telemetry"].RuntimeProperties != nil {
t.setCatalogConfiguration(e)
} else {
t.setProperties(e)
}
return nil
}
func (t *telemetryTrait) setCatalogConfiguration(e *trait.Environment) {
if e.ApplicationProperties == nil {
e.ApplicationProperties = make(map[string]string)
}
if t.Endpoint != "" {
e.ApplicationProperties["camel.k.telemetry.endpoint"] = t.Endpoint
}
if t.ServiceName != "" {
e.ApplicationProperties["camel.k.telemetry.serviceName"] = fmt.Sprintf("service.name=%s", t.ServiceName)
}
if t.Sampler != "" {
e.ApplicationProperties["camel.k.telemetry.sampler"] = t.Sampler
}
if t.SamplerRatio != "" {
e.ApplicationProperties["camel.k.telemetry.samplerRatio"] = t.SamplerRatio
}
if pointer.BoolDeref(t.SamplerParentBased, true) {
e.ApplicationProperties["camel.k.telemetry.samplerParentBased"] = "true"
} else {
e.ApplicationProperties["camel.k.telemetry.samplerParentBased"] = "false"
}
for _, cp := range e.CamelCatalog.Runtime.Capabilities["telemetry"].RuntimeProperties {
e.ApplicationProperties[trait.CapabilityPropertyKey(cp.Key, e.ApplicationProperties)] = cp.Value
}
}
// Deprecated: to be removed in future release in favor of func setCatalogConfiguration().
func (t *telemetryTrait) setProperties(e *trait.Environment) {
if e.CamelCatalog != nil {
provider := e.CamelCatalog.CamelCatalogSpec.Runtime.Provider
properties := telemetryProperties[provider]
if appPropEnabled := properties[propEnabled]; appPropEnabled != "" {
e.ApplicationProperties[appPropEnabled] = "true"
}
if appPropEndpoint := properties[propEndpoint]; appPropEndpoint != "" && t.Endpoint != "" {
e.ApplicationProperties[appPropEndpoint] = t.Endpoint
}
if appPropServiceName := properties[propServiceName]; appPropServiceName != "" && t.ServiceName != "" {
e.ApplicationProperties[appPropServiceName] = "service.name=" + t.ServiceName
}
if appPropSampler := properties[propSampler]; appPropSampler != "" && t.Sampler != "" {
e.ApplicationProperties[appPropSampler] = t.Sampler
}
if appPropSamplerRatio := properties[propSamplerRatio]; appPropSamplerRatio != "" && t.SamplerRatio != "" {
e.ApplicationProperties[appPropSamplerRatio] = t.SamplerRatio
}
if appPropSamplerParentBased := properties[propSamplerParentBased]; appPropSamplerParentBased != "" {
if pointer.BoolDeref(t.SamplerParentBased, true) {
e.ApplicationProperties[appPropSamplerParentBased] = "true"
} else {
e.ApplicationProperties[appPropSamplerParentBased] = "false"
}
}
}
}