Refactor CLI initialization (#562)
This cleans up the CLI code significantly. Also makes some of this re-usable for providers who want to do so. This also removes the main.go from the top of the tree of the repro, instead moving it into cmd/virtual-kubelet. This allows us to better utilize the package namespace (and e.g. mv the `vkubelet` package to the top of the tree).
This commit is contained in:
37
trace/opencensus/jaeger.go
Normal file
37
trace/opencensus/jaeger.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// +build !no_jaeger_exporter
|
||||
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
|
||||
"go.opencensus.io/exporter/jaeger"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterTracingExporter("jaeger", NewJaegerExporter)
|
||||
}
|
||||
|
||||
// NewJaegerExporter creates a new opencensus tracing exporter.
|
||||
func NewJaegerExporter(opts TracingExporterOptions) (trace.Exporter, error) {
|
||||
jOpts := jaeger.Options{
|
||||
Endpoint: os.Getenv("JAEGER_ENDPOINT"),
|
||||
AgentEndpoint: os.Getenv("JAEGER_AGENT_ENDPOINT"),
|
||||
Username: os.Getenv("JAEGER_USER"),
|
||||
Password: os.Getenv("JAEGER_PASSWORD"),
|
||||
Process: jaeger.Process{
|
||||
ServiceName: opts.ServiceName,
|
||||
},
|
||||
}
|
||||
|
||||
if jOpts.Endpoint == "" && jOpts.AgentEndpoint == "" {
|
||||
return nil, errors.New("Must specify either JAEGER_ENDPOINT or JAEGER_AGENT_ENDPOINT")
|
||||
}
|
||||
|
||||
for k, v := range opts.Tags {
|
||||
jOpts.Process.Tags = append(jOpts.Process.Tags, jaeger.StringTag(k, v))
|
||||
}
|
||||
return jaeger.NewExporter(jOpts)
|
||||
}
|
||||
37
trace/opencensus/ocagent.go
Normal file
37
trace/opencensus/ocagent.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// +build !no_ocagent_exporter
|
||||
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"contrib.go.opencensus.io/exporter/ocagent"
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterTracingExporter("ocagent", NewOCAgentExporter)
|
||||
}
|
||||
|
||||
// NewOCAgentExporter creates a new opencensus tracing exporter using the opencensus agent forwarder.
|
||||
func NewOCAgentExporter(opts TracingExporterOptions) (trace.Exporter, error) {
|
||||
agentOpts := append([]ocagent.ExporterOption{}, ocagent.WithServiceName(opts.ServiceName))
|
||||
|
||||
if endpoint := os.Getenv("OCAGENT_ENDPOINT"); endpoint != "" {
|
||||
agentOpts = append(agentOpts, ocagent.WithAddress(endpoint))
|
||||
} else {
|
||||
return nil, strongerrors.InvalidArgument(errors.New("must set endpoint address in OCAGENT_ENDPOINT"))
|
||||
}
|
||||
|
||||
switch os.Getenv("OCAGENT_INSECURE") {
|
||||
case "0", "no", "n", "off", "":
|
||||
case "1", "yes", "y", "on":
|
||||
agentOpts = append(agentOpts, ocagent.WithInsecure())
|
||||
default:
|
||||
return nil, strongerrors.InvalidArgument(errors.New("invalid value for OCAGENT_INSECURE"))
|
||||
}
|
||||
|
||||
return ocagent.NewExporter(agentOpts...)
|
||||
}
|
||||
45
trace/opencensus/register.go
Normal file
45
trace/opencensus/register.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
type TracingExporterOptions struct {
|
||||
Tags map[string]string
|
||||
ServiceName string
|
||||
}
|
||||
|
||||
var (
|
||||
tracingExporters = make(map[string]TracingExporterInitFunc)
|
||||
)
|
||||
|
||||
// TracingExporterInitFunc is the function that is called to initialize an exporter.
|
||||
// This is used when registering an exporter and called when a user specifed they want to use the exporter.
|
||||
type TracingExporterInitFunc func(TracingExporterOptions) (trace.Exporter, error)
|
||||
|
||||
// RegisterTracingExporter registers a tracing exporter.
|
||||
// For a user to select an exporter, it must be registered here.
|
||||
func RegisterTracingExporter(name string, f TracingExporterInitFunc) {
|
||||
tracingExporters[name] = f
|
||||
}
|
||||
|
||||
// GetTracingExporter gets the specified tracing exporter passing in the options to the exporter init function.
|
||||
// For an exporter to be availbale here it must be registered with `RegisterTracingExporter`.
|
||||
func GetTracingExporter(name string, opts TracingExporterOptions) (trace.Exporter, error) {
|
||||
f, ok := tracingExporters[name]
|
||||
if !ok {
|
||||
return nil, strongerrors.NotFound(errors.Errorf("tracing exporter %q not found", name))
|
||||
}
|
||||
return f(opts)
|
||||
}
|
||||
|
||||
// AvailableTraceExporters gets the list of registered exporters
|
||||
func AvailableTraceExporters() []string {
|
||||
out := make([]string, 0, len(tracingExporters))
|
||||
for k := range tracingExporters {
|
||||
out = append(out, k)
|
||||
}
|
||||
return out
|
||||
}
|
||||
45
trace/opencensus/register_test.go
Normal file
45
trace/opencensus/register_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cpuguy83/strongerrors"
|
||||
|
||||
"go.opencensus.io/trace"
|
||||
)
|
||||
|
||||
func TestGetTracingExporter(t *testing.T) {
|
||||
defer delete(tracingExporters, "mock")
|
||||
|
||||
mockExporterFn := func(_ TracingExporterOptions) (trace.Exporter, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
_, err := GetTracingExporter("notexist", TracingExporterOptions{})
|
||||
if !strongerrors.IsNotFound(err) {
|
||||
t.Fatalf("expected not found error, got: %v", err)
|
||||
}
|
||||
|
||||
RegisterTracingExporter("mock", mockExporterFn)
|
||||
|
||||
if _, err := GetTracingExporter("mock", TracingExporterOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAvailableExporters(t *testing.T) {
|
||||
defer delete(tracingExporters, "mock")
|
||||
|
||||
mockExporterFn := func(_ TracingExporterOptions) (trace.Exporter, error) {
|
||||
return nil, nil
|
||||
}
|
||||
RegisterTracingExporter("mock", mockExporterFn)
|
||||
|
||||
for _, e := range AvailableTraceExporters() {
|
||||
if e == "mock" {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
t.Fatal("could not find mock exporter in list of registered exporters")
|
||||
}
|
||||
Reference in New Issue
Block a user