Skip to content

Commit

Permalink
Merge pull request #154 from K-Phoen/jaeger-trace-to-logs
Browse files Browse the repository at this point in the history
Support 'trace to logs' options on Jaeger datasources
  • Loading branch information
K-Phoen authored Dec 25, 2021
2 parents 277967b + e3e2bbd commit 66b0fc6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
3 changes: 1 addition & 2 deletions datasource/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"github.com/K-Phoen/sdk"
)

// TODO: support "Trace to logs" settings

var _ datasource.Datasource = Jaeger{}

type Jaeger struct {
builder *sdk.Datasource
}

type Option func(datasource *Jaeger)
type TraceToLogsOption func(settings map[string]interface{})

func New(name string, url string, options ...Option) Jaeger {
jaeger := &Jaeger{
Expand Down
53 changes: 53 additions & 0 deletions datasource/jaeger/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,56 @@ func WithNodeGraph() Option {
}
}
}

// TraceToLogs defines how to navigate from a trace span to the selected datasource logs.
func TraceToLogs(logsDatasourceUID string, options ...TraceToLogsOption) Option {
settings := map[string]interface{}{
"datasourceUid": logsDatasourceUID,
}

for _, opt := range options {
opt(settings)
}

return func(datasource *Jaeger) {
datasource.builder.JSONData.(map[string]interface{})["tracesToLogs"] = settings
}
}

// Tags defines tags that will be used in the Loki query.
// Default tags: 'cluster', 'hostname', 'namespace', 'pod'.
func Tags(tags ...string) TraceToLogsOption {
return func(settings map[string]interface{}) {
settings["tags"] = tags
}
}

// SpanStartShift shifts the start time of the span.
// Default 0 (Time units can be used here, for example: 5s, 1m, 3h)
func SpanStartShift(shift time.Duration) TraceToLogsOption {
return func(settings map[string]interface{}) {
settings["spanStartTimeShift"] = shift.String()
}
}

// SpanEndShift shifts the start time of the span.
// Default 0 (Time units can be used here, for example: 5s, 1m, 3h)
func SpanEndShift(shift time.Duration) TraceToLogsOption {
return func(settings map[string]interface{}) {
settings["spanEndTimeShift"] = shift.String()
}
}

// FilterByTrace filters logs by Trace ID. Appends '|=<trace id>' to the query.
func FilterByTrace() TraceToLogsOption {
return func(settings map[string]interface{}) {
settings["filterByTraceID"] = true
}
}

// FilterBySpan filters logs by Trace ID. Appends '|=<trace id>' to the query.
func FilterBySpan() TraceToLogsOption {
return func(settings map[string]interface{}) {
settings["filterBySpanID"] = true
}
}
25 changes: 25 additions & 0 deletions datasource/jaeger/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,28 @@ func TestWithNodeGraph(t *testing.T) {

req.Equal(true, jsonData["nodeGraph"].(map[string]interface{})["enabled"])
}

func TestTraceToLogs(t *testing.T) {
req := require.New(t)

lokiDatasourceUID := "lala"
datasource := New("", "", TraceToLogs(
lokiDatasourceUID,
Tags("pod", "namespace"),
SpanStartShift(2*time.Second),
SpanEndShift(1*time.Second),
FilterByTrace(),
FilterBySpan(),
))

jsonData := datasource.builder.JSONData.(map[string]interface{})
traceToLogsSettings := jsonData["tracesToLogs"].(map[string]interface{})

req.NotEmpty(traceToLogsSettings)
req.Equal(lokiDatasourceUID, traceToLogsSettings["datasourceUid"])
req.ElementsMatch([]string{"pod", "namespace"}, traceToLogsSettings["tags"])
req.Equal("2s", traceToLogsSettings["spanStartTimeShift"])
req.Equal("1s", traceToLogsSettings["spanEndTimeShift"])
req.Equal(true, traceToLogsSettings["filterByTraceID"])
req.Equal(true, traceToLogsSettings["filterBySpanID"])
}

0 comments on commit 66b0fc6

Please sign in to comment.