Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Add possibility to Jaeger to add process tags (#871)
Browse files Browse the repository at this point in the history
Jaeger allows to attach tags to it's process, which this patch
allows to specify.
Currently this is limited to bool, string, int64 and int32, as
attributeToTag is used for converting it to *jaeger.Tag.

For reference see the official Jaeger client go:
https://github.com/jaegertracing/jaeger-client-go/blob/252d853b2a4f3cfc98bb89c3a8bb3d3aa50ed4d6/tracer.go#L64

This change helps to specify tags, such as 'ip', which has a special
handling in Jaeger (for example jaeger-query skips clock skew
adjustments for processes from the same IP).

This change makes Process part of the Jaeger Options.
The Options ServiceName is deprecated and used as a fallback.
All examples are updated accordingly.
Int32Tag is removed (as there is Int64Tag already).
  • Loading branch information
bastianccm authored and Ramon Nogueira committed Aug 23, 2018
1 parent 7e6c39b commit 71e2e3e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 13 deletions.
6 changes: 4 additions & 2 deletions exporter/jaeger/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ func main() {
// Register the Jaeger exporter to be able to retrieve
// the collected spans.
exporter, err := jaeger.NewExporter(jaeger.Options{
Endpoint: "http://localhost:14268",
ServiceName: "trace-demo",
Endpoint: "http://localhost:14268",
Process: jaeger.Process{
ServiceName: "trace-demo",
},
})
if err != nil {
log.Fatal(err)
Expand Down
32 changes: 29 additions & 3 deletions exporter/jaeger/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ func ExampleNewExporter_collector() {
// Register the Jaeger exporter to be able to retrieve
// the collected spans.
exporter, err := jaeger.NewExporter(jaeger.Options{
Endpoint: "http://localhost:14268",
ServiceName: "trace-demo",
Endpoint: "http://localhost:14268",
Process: jaeger.Process{
ServiceName: "trace-demo",
},
})
if err != nil {
log.Fatal(err)
Expand All @@ -39,7 +41,31 @@ func ExampleNewExporter_agent() {
// the collected spans.
exporter, err := jaeger.NewExporter(jaeger.Options{
AgentEndpoint: "localhost:6831",
ServiceName: "trace-demo",
Process: jaeger.Process{
ServiceName: "trace-demo",
},
})
if err != nil {
log.Fatal(err)
}
trace.RegisterExporter(exporter)
}

// ExampleNewExporter_processTags shows how to set ProcessTags
// on a Jaeger exporter. These tags will be added to the exported
// Jaeger process.
func ExampleNewExporter_processTags() {
// Register the Jaeger exporter to be able to retrieve
// the collected spans.
exporter, err := jaeger.NewExporter(jaeger.Options{
AgentEndpoint: "localhost:6831",
Process: jaeger.Process{
ServiceName: "trace-demo",
Tags: []jaeger.Tag{
jaeger.StringTag("ip", "127.0.0.1"),
jaeger.BoolTag("demo", true),
},
},
})
if err != nil {
log.Fatal(err)
Expand Down
60 changes: 52 additions & 8 deletions exporter/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ type Options struct {
Password string

// ServiceName is the Jaeger service name.
// Deprecated: Specify Process instead.
ServiceName string

// Process contains the information about the exporting process.
Process Process
}

// NewExporter returns a trace.Exporter implementation that exports
Expand Down Expand Up @@ -86,17 +90,27 @@ func NewExporter(o Options) (*Exporter, error) {
}
log.Printf("Error when uploading spans to Jaeger: %v", err)
}
service := o.ServiceName
if service == "" {
service := o.Process.ServiceName
if service == "" && o.ServiceName != "" {
// fallback to old service name if specified
service = o.ServiceName
} else if service == "" {
service = defaultServiceName
}
tags := make([]*gen.Tag, len(o.Process.Tags))
for i, tag := range o.Process.Tags {
tags[i] = attributeToTag(tag.key, tag.value)
}
e := &Exporter{
endpoint: endpoint,
agentEndpoint: o.AgentEndpoint,
client: client,
username: o.Username,
password: o.Password,
service: service,
process: &gen.Process{
ServiceName: service,
Tags: tags,
},
}
bundler := bundler.NewBundler((*gen.Span)(nil), func(bundle interface{}) {
if err := e.upload(bundle.([]*gen.Span)); err != nil {
Expand All @@ -107,11 +121,43 @@ func NewExporter(o Options) (*Exporter, error) {
return e, nil
}

// Process contains the information exported to jaeger about the source
// of the trace data.
type Process struct {
// ServiceName is the Jaeger service name.
ServiceName string

// Tags are added to Jaeger Process exports
Tags []Tag
}

// Tag defines a key-value pair
// It is limited to the possible conversions to *jaeger.Tag by attributeToTag
type Tag struct {
key string
value interface{}
}

// BoolTag creates a new tag of type bool, exported as jaeger.TagType_BOOL
func BoolTag(key string, value bool) Tag {
return Tag{key, value}
}

// StringTag creates a new tag of type string, exported as jaeger.TagType_STRING
func StringTag(key string, value string) Tag {
return Tag{key, value}
}

// Int64Tag creates a new tag of type int64, exported as jaeger.TagType_LONG
func Int64Tag(key string, value int64) Tag {
return Tag{key, value}
}

// Exporter is an implementation of trace.Exporter that uploads spans to Jaeger.
type Exporter struct {
endpoint string
agentEndpoint string
service string
process *gen.Process
bundler *bundler.Bundler
client *agentClientUDP

Expand Down Expand Up @@ -230,10 +276,8 @@ func (e *Exporter) Flush() {

func (e *Exporter) upload(spans []*gen.Span) error {
batch := &gen.Batch{
Spans: spans,
Process: &gen.Process{
ServiceName: e.service,
},
Spans: spans,
Process: e.process,
}
if e.endpoint != "" {
return e.uploadCollector(batch)
Expand Down

0 comments on commit 71e2e3e

Please sign in to comment.