Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create exporter based on agent endpoint configuration #2029

Merged
merged 3 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/tracing-agent-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Tracing agent configuration

Earlier we could only use the collector URL directly, but since an agent can be deployed as a sidecar process it makes much more sense to use it instead of the collector directly.

https://github.com/cs3org/reva/pull/2029
2 changes: 1 addition & 1 deletion cmd/revad/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func initServers(mainConf map[string]interface{}, log *zerolog.Logger) map[strin
}

func initTracing(conf *coreConf) {
rtrace.SetTraceProvider(conf.TracingCollector)
rtrace.SetTraceProvider(conf.TracingCollector, conf.TracingEndpoint)
}

func initCPUCount(conf *coreConf, log *zerolog.Logger) {
Expand Down
61 changes: 57 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,35 @@ 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 err != nil {
panic(err)
}
}

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

Provider = sdktrace.NewTracerProvider(
Expand All @@ -50,3 +79,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
}