Skip to content

Commit

Permalink
tracing: create exported based on agent configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
refs committed Sep 1, 2021
1 parent e2f89ab commit 39afba6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
5 changes: 4 additions & 1 deletion cmd/revad/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ func initServers(mainConf map[string]interface{}, log *zerolog.Logger) map[strin
}

func initTracing(conf *coreConf) {
rtrace.SetTraceProvider(conf.TracingCollector)
if conf.TracingEndpoint != "" {

}
rtrace.SetTraceProvider(conf.TracingCollector, conf.TracingEndpoint)
}

func initCPUCount(conf *coreConf, log *zerolog.Logger) {
Expand Down
58 changes: 54 additions & 4 deletions pkg/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
package trace

import (
"fmt"
"net/url"
"strings"

"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
Expand All @@ -36,10 +40,32 @@ var (
)

// SetTraceProvider sets the TracerProvider at a package level.
func SetTraceProvider(url string) {
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(url)))
if err != nil {
panic(err)
func SetTraceProvider(collectorEndpoint string, agentEndpoint string) {
var exp *jaeger.Exporter
var err error

if agentEndpoint != "" {
var agentHost string
var agentPort string

agentHost, agentPort, err = parseAgentConfig(agentEndpoint)
if err != nil {
panic(err)
}

exp, err = jaeger.New(
jaeger.WithAgentEndpoint(
jaeger.WithAgentHost(agentHost),
jaeger.WithAgentPort(agentPort),
),
)
}

if collectorEndpoint != "" {
exp, err = jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(collectorEndpoint)))
if err != nil {
panic(err)
}
}

Provider = sdktrace.NewTracerProvider(
Expand All @@ -50,3 +76,27 @@ func SetTraceProvider(url string) {
)),
)
}

func parseAgentConfig(ae string) (string, string, error) {
u, err := url.Parse(ae)
// as per url.go:
// [...] Trying to parse a hostname and path
// without a scheme is invalid but may not necessarily return an
// error, due to parsing ambiguities.
if err == nil && u.Hostname() != "" && u.Port() != "" {
return u.Hostname(), u.Port(), nil
}

p := strings.Split(ae, ":")
if len(p) != 2 {
return "", "", fmt.Errorf(fmt.Sprintf("invalid agent endpoint `%s`. expected format: `hostname:port`", ae))
}

switch {
case p[0] == "" && p[1] == "": // case ae = ":"
return "", "", fmt.Errorf(fmt.Sprintf("invalid agent endpoint `%s`. expected format: `hostname:port`", ae))
case p[0] == "":
return "", "", fmt.Errorf(fmt.Sprintf("invalid agent endpoint `%s`. expected format: `hostname:port`", ae))
}
return p[0], p[1], nil
}

0 comments on commit 39afba6

Please sign in to comment.