Move around some packages (#658)
* Move tracing exporter registration This doesn't belong in the library and should be configured by the consumer of the opencensus package. * Rename `vkublet` package to `node` `vkubelet` does not convey any information to the consumers of the package. Really it would be nice to move this package to the root of the repo, but then you wind up with... interesting... import semantics due to the repo name... and after thinking about it some, a subpackage is really not so bad as long as it has a name that convey's some information. `node` was chosen since this package deals with all the semantics of operating a node in Kubernetes.
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
// +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)
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
// +build !no_ocagent_exporter
|
||||
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"contrib.go.opencensus.io/exporter/ocagent"
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
"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, errdefs.InvalidInput("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, errdefs.InvalidInput("invalid value for OCAGENT_INSECURE")
|
||||
}
|
||||
|
||||
return ocagent.NewExporter(agentOpts...)
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
"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, errdefs.NotFoundf("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
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package opencensus
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/virtual-kubelet/virtual-kubelet/errdefs"
|
||||
"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 !errdefs.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