This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
forked from knative/eventing
/
setup.go
125 lines (111 loc) · 4.29 KB
/
setup.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
/*
Copyright 2019 The Knative Authors
Licensed 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 tracing
import (
"fmt"
eventingconfigmap "github.com/knative/eventing/pkg/configmap"
"github.com/knative/pkg/configmap"
"github.com/knative/pkg/tracing"
tracingconfig "github.com/knative/pkg/tracing/config"
zipkin "github.com/openzipkin/zipkin-go"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// TODO Move this to knative/pkg.
var (
// OnePercentSampling is a configuration that samples 1% of the requests.
OnePercentSampling = &tracingconfig.Config{
Enable: true,
Debug: false,
SampleRate: 0.01,
ZipkinEndpoint: "http://zipkin.istio-system.svc.cluster.local:9411/api/v2/spans",
}
)
// setupZipkinPublishing sets up Zipkin trace publishing for the process. Note that other pieces
// still need to generate the traces, this just ensures that if generated, they are collected
// appropriately. This is normally done by using tracing.HTTPSpanMiddleware as a middleware HTTP
// handler.
func setupZipkinPublishing(serviceName string) (*tracing.OpenCensusTracer, error) {
// TODO Should we fill in the hostPort?
zipkinEndpoint, err := zipkin.NewEndpoint(serviceName, "")
if err != nil {
return nil, fmt.Errorf("unable to create tracing endpoint: %v", err)
}
oct := tracing.NewOpenCensusTracer(tracing.WithZipkinExporter(tracing.CreateZipkinReporter, zipkinEndpoint))
return oct, nil
}
// SetupStaticZipkinPublishing sets up Zipkin trace publishing for the process. Note that other
// pieces still need to generate the traces, this just ensures that if generated, they are collected
// appropriately. This is normally done by using tracing.HTTPSpanMiddleware as a middleware HTTP
// handler. The configuration will not be dynamically updated.
func SetupStaticZipkinPublishing(serviceName string, cfg *tracingconfig.Config) error {
oct, err := setupZipkinPublishing(serviceName)
if err != nil {
return err
}
err = oct.ApplyConfig(cfg)
if err != nil {
return fmt.Errorf("unable to set OpenCensusTracing config: %v", err)
}
return nil
}
// SetupDynamicZipkinPublishing sets up Zipkin trace publishing for the process, by watching a
// ConfigMap for the configuration. Note that other pieces still need to generate the traces, this
// just ensures that if generated, they are collected appropriately. This is normally done by using
// tracing.HTTPSpanMiddleware as a middleware HTTP handler. The configuration will be dynamically
// updated when the ConfigMap is updated.
func SetupDynamicZipkinPublishing(logger *zap.SugaredLogger, configMapWatcher *configmap.InformedWatcher, serviceName string) error {
oct, err := setupZipkinPublishing(serviceName)
if err != nil {
return err
}
tracerUpdater := func(name string, value interface{}) {
if name == tracingconfig.ConfigName {
cfg := value.(*tracingconfig.Config)
logger.Debugw("Updating tracing config", zap.Any("cfg", cfg))
err = oct.ApplyConfig(cfg)
if err != nil {
logger.Errorw("Unable to apply open census tracer config", zap.Error(err))
return
}
}
}
// Set up our config store.
configStore := eventingconfigmap.NewDefaultUntypedStore(
"tracing-config",
logger,
[]eventingconfigmap.DefaultConstructor{
{
Default: enableZeroSamplingCM(configMapWatcher.Namespace),
Constructor: tracingconfig.NewTracingConfigFromConfigMap,
},
},
tracerUpdater)
configStore.WatchConfigs(configMapWatcher)
return nil
}
func enableZeroSamplingCM(ns string) corev1.ConfigMap {
return corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: tracingconfig.ConfigName,
Namespace: ns,
},
Data: map[string]string{
"enable": "True",
"debug": "False",
"sample-rate": "0",
"zipkin-endpoint": "http://zipkin.istio-system.svc.cluster.local:9411/api/v2/spans",
},
}
}