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

Span Kind cleanup and improvements #62

Merged
merged 2 commits into from
Feb 15, 2019
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
6 changes: 3 additions & 3 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func (r *agentS) fullRequestResponse(url string, method string, data interface{}
req, err = http.NewRequest(method, url, nil)
}

// Uncomment this to dump json payloads
// log.debug(bytes.NewBuffer(j))

if err == nil {
req.Header.Set("Content-Type", "application/json")
resp, err = r.client.Do(req)
Expand All @@ -130,9 +133,6 @@ func (r *agentS) fullRequestResponse(url string, method string, data interface{}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
err = errors.New(resp.Status)
} else {

log.debug("agent response:", url, resp.Status)

if body != nil {
var b []byte
b, err = ioutil.ReadAll(resp.Body)
Expand Down
24 changes: 18 additions & 6 deletions example/ot-simple/simple.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"math/rand"
"time"

"github.com/instana/golang-sensor"
Expand All @@ -16,27 +17,38 @@ const (
)

func simple(ctx context.Context) {
parentSpan, ctx := ot.StartSpanFromContext(ctx, "asteroid")
// Handling an incoming request
parentSpan := ot.StartSpan("asteroid")
parentSpan.SetTag(string(ext.Component), "Go simple example app")
parentSpan.SetTag(string(ext.SpanKind), string(ext.SpanKindRPCServerEnum))
parentSpan.SetTag(string(ext.HTTPUrl), "/golang/simple/one")
parentSpan.SetTag(string(ext.HTTPUrl), "https://asteroid.svc.io/golang/api/v2")
parentSpan.SetTag(string(ext.HTTPMethod), "GET")
parentSpan.SetTag(string(ext.HTTPStatusCode), uint16(200))
parentSpan.LogFields(log.String("foo", "bar"))

// Making an HTTP request
childSpan := ot.StartSpan("spacedust", ot.ChildOf(parentSpan.Context()))
childSpan.SetTag(string(ext.SpanKind), string(ext.SpanKindRPCClientEnum))
childSpan.SetTag(string(ext.HTTPUrl), "/golang/simple/two")
childSpan.SetTag(string(ext.HTTPUrl), "https://meteor.svc.io/golang/api/v2")
childSpan.SetTag(string(ext.HTTPMethod), "POST")
childSpan.SetTag(string(ext.HTTPStatusCode), 204)
childSpan.SetBaggageItem("someBaggage", "someValue")

time.Sleep(10 * time.Millisecond)

// Make the HTTP request (we'll just sleep for a random duration as an example)
time.Sleep(time.Duration(rand.Intn(200)) * time.Millisecond)
childSpan.Finish()

time.Sleep(5 * time.Millisecond)
// Rendering a template with custom tags
renderSpan := ot.StartSpan("render", ot.ChildOf(childSpan.Context()))
renderSpan.SetTag("type", "layout")
renderSpan.SetTag("name", "application_layout.erb")

// Render the template (we'll just sleep for a random duration as an example)
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
renderSpan.Finish()

// Additional work (we'll just sleep for a random duration as an example)
time.Sleep(time.Duration(rand.Intn(30)) * time.Millisecond)
parentSpan.Finish()
}

Expand Down
1 change: 1 addition & 0 deletions json_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type jsonSpan struct {
Duration uint64 `json:"d"`
Name string `json:"n"`
From *fromS `json:"f"`
Kind int `json:"k"`
Error bool `json:"error"`
Ec int `json:"ec,omitempty"`
Lang string `json:"ta,omitempty"`
Expand Down
5 changes: 3 additions & 2 deletions recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func (r *Recorder) RecordSpan(span *spanS) {
}

var data = &jsonData{}
kind := span.getSpanKind()
kindTag := span.getSpanKindTag()

data.SDK = &jsonSDKData{
Name: span.Operation,
Type: kind,
Type: kindTag,
Custom: &jsonCustomData{Tags: span.Tags, Logs: span.collectLogs()}}

baggage := make(map[string]string)
Expand Down Expand Up @@ -108,6 +108,7 @@ func (r *Recorder) RecordSpan(span *spanS) {
Ec: span.Ec,
Lang: "go",
From: sensor.agent.from,
Kind: span.getSpanKindInt(),
Data: data})

if r.testMode || !sensor.agent.canSend() {
Expand Down
8 changes: 4 additions & 4 deletions recorder_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ func TestSpanKind(t *testing.T) {
span := tracer.StartSpan("http-client")
span.SetTag(string(ext.SpanKind), "exit")
span.Finish()
kind := span.(*spanS).getSpanKind()
kind := span.(*spanS).getSpanKindTag()
assert.EqualValues(t, "exit", kind, "Wrong span kind")

// Entry
span = tracer.StartSpan("http-client")
span.SetTag(string(ext.SpanKind), "entry")
span.Finish()
kind = span.(*spanS).getSpanKind()
kind = span.(*spanS).getSpanKindTag()
assert.EqualValues(t, "entry", kind, "Wrong span kind")

// Consumer
span = tracer.StartSpan("http-client")
span.SetTag(string(ext.SpanKind), "consumer")
span.Finish()
kind = span.(*spanS).getSpanKind()
kind = span.(*spanS).getSpanKindTag()
assert.EqualValues(t, "entry", kind, "Wrong span kind")

// Producer
span = tracer.StartSpan("http-client")
span.SetTag(string(ext.SpanKind), "producer")
span.Finish()
kind = span.(*spanS).getSpanKind()
kind = span.(*spanS).getSpanKindTag()
assert.EqualValues(t, "exit", kind, "Wrong span kind")
}

Expand Down
16 changes: 14 additions & 2 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (r *spanS) getHostName() string {
return h
}

func (r *spanS) getSpanKind() string {
func (r *spanS) getSpanKindTag() string {
kind := r.getStringTag(string(ext.SpanKind))

switch kind {
Expand All @@ -234,7 +234,19 @@ func (r *spanS) getSpanKind() string {
case string(ext.SpanKindRPCClientEnum), "producer", "exit":
return "exit"
}
return ""
return "intermediate"
}

func (r *spanS) getSpanKindInt() int {
kind := r.getStringTag(string(ext.SpanKind))

switch kind {
case string(ext.SpanKindRPCServerEnum), "consumer", "entry":
return 1
case string(ext.SpanKindRPCClientEnum), "producer", "exit":
return 2
}
return 3
}

func (r *spanS) collectLogs() map[uint64]map[string]interface{} {
Expand Down