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

Commit 71e2e3e

Browse files
bastianccmRamon Nogueira
authored andcommitted
Add possibility to Jaeger to add process tags (#871)
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).
1 parent 7e6c39b commit 71e2e3e

File tree

3 files changed

+85
-13
lines changed

3 files changed

+85
-13
lines changed

exporter/jaeger/example/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ func main() {
3030
// Register the Jaeger exporter to be able to retrieve
3131
// the collected spans.
3232
exporter, err := jaeger.NewExporter(jaeger.Options{
33-
Endpoint: "http://localhost:14268",
34-
ServiceName: "trace-demo",
33+
Endpoint: "http://localhost:14268",
34+
Process: jaeger.Process{
35+
ServiceName: "trace-demo",
36+
},
3537
})
3638
if err != nil {
3739
log.Fatal(err)

exporter/jaeger/example_test.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ func ExampleNewExporter_collector() {
2525
// Register the Jaeger exporter to be able to retrieve
2626
// the collected spans.
2727
exporter, err := jaeger.NewExporter(jaeger.Options{
28-
Endpoint: "http://localhost:14268",
29-
ServiceName: "trace-demo",
28+
Endpoint: "http://localhost:14268",
29+
Process: jaeger.Process{
30+
ServiceName: "trace-demo",
31+
},
3032
})
3133
if err != nil {
3234
log.Fatal(err)
@@ -39,7 +41,31 @@ func ExampleNewExporter_agent() {
3941
// the collected spans.
4042
exporter, err := jaeger.NewExporter(jaeger.Options{
4143
AgentEndpoint: "localhost:6831",
42-
ServiceName: "trace-demo",
44+
Process: jaeger.Process{
45+
ServiceName: "trace-demo",
46+
},
47+
})
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
trace.RegisterExporter(exporter)
52+
}
53+
54+
// ExampleNewExporter_processTags shows how to set ProcessTags
55+
// on a Jaeger exporter. These tags will be added to the exported
56+
// Jaeger process.
57+
func ExampleNewExporter_processTags() {
58+
// Register the Jaeger exporter to be able to retrieve
59+
// the collected spans.
60+
exporter, err := jaeger.NewExporter(jaeger.Options{
61+
AgentEndpoint: "localhost:6831",
62+
Process: jaeger.Process{
63+
ServiceName: "trace-demo",
64+
Tags: []jaeger.Tag{
65+
jaeger.StringTag("ip", "127.0.0.1"),
66+
jaeger.BoolTag("demo", true),
67+
},
68+
},
4369
})
4470
if err != nil {
4571
log.Fatal(err)

exporter/jaeger/jaeger.go

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ type Options struct {
5858
Password string
5959

6060
// ServiceName is the Jaeger service name.
61+
// Deprecated: Specify Process instead.
6162
ServiceName string
63+
64+
// Process contains the information about the exporting process.
65+
Process Process
6266
}
6367

6468
// NewExporter returns a trace.Exporter implementation that exports
@@ -86,17 +90,27 @@ func NewExporter(o Options) (*Exporter, error) {
8690
}
8791
log.Printf("Error when uploading spans to Jaeger: %v", err)
8892
}
89-
service := o.ServiceName
90-
if service == "" {
93+
service := o.Process.ServiceName
94+
if service == "" && o.ServiceName != "" {
95+
// fallback to old service name if specified
96+
service = o.ServiceName
97+
} else if service == "" {
9198
service = defaultServiceName
9299
}
100+
tags := make([]*gen.Tag, len(o.Process.Tags))
101+
for i, tag := range o.Process.Tags {
102+
tags[i] = attributeToTag(tag.key, tag.value)
103+
}
93104
e := &Exporter{
94105
endpoint: endpoint,
95106
agentEndpoint: o.AgentEndpoint,
96107
client: client,
97108
username: o.Username,
98109
password: o.Password,
99-
service: service,
110+
process: &gen.Process{
111+
ServiceName: service,
112+
Tags: tags,
113+
},
100114
}
101115
bundler := bundler.NewBundler((*gen.Span)(nil), func(bundle interface{}) {
102116
if err := e.upload(bundle.([]*gen.Span)); err != nil {
@@ -107,11 +121,43 @@ func NewExporter(o Options) (*Exporter, error) {
107121
return e, nil
108122
}
109123

124+
// Process contains the information exported to jaeger about the source
125+
// of the trace data.
126+
type Process struct {
127+
// ServiceName is the Jaeger service name.
128+
ServiceName string
129+
130+
// Tags are added to Jaeger Process exports
131+
Tags []Tag
132+
}
133+
134+
// Tag defines a key-value pair
135+
// It is limited to the possible conversions to *jaeger.Tag by attributeToTag
136+
type Tag struct {
137+
key string
138+
value interface{}
139+
}
140+
141+
// BoolTag creates a new tag of type bool, exported as jaeger.TagType_BOOL
142+
func BoolTag(key string, value bool) Tag {
143+
return Tag{key, value}
144+
}
145+
146+
// StringTag creates a new tag of type string, exported as jaeger.TagType_STRING
147+
func StringTag(key string, value string) Tag {
148+
return Tag{key, value}
149+
}
150+
151+
// Int64Tag creates a new tag of type int64, exported as jaeger.TagType_LONG
152+
func Int64Tag(key string, value int64) Tag {
153+
return Tag{key, value}
154+
}
155+
110156
// Exporter is an implementation of trace.Exporter that uploads spans to Jaeger.
111157
type Exporter struct {
112158
endpoint string
113159
agentEndpoint string
114-
service string
160+
process *gen.Process
115161
bundler *bundler.Bundler
116162
client *agentClientUDP
117163

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

231277
func (e *Exporter) upload(spans []*gen.Span) error {
232278
batch := &gen.Batch{
233-
Spans: spans,
234-
Process: &gen.Process{
235-
ServiceName: e.service,
236-
},
279+
Spans: spans,
280+
Process: e.process,
237281
}
238282
if e.endpoint != "" {
239283
return e.uploadCollector(batch)

0 commit comments

Comments
 (0)