//dd:span

Create a span surrounding all calls to the annotated function by adding a //dd:span directive on the function declaration. The default operation (span) name is the name of the function, but this can be overridden by specifying a span.name argument:

//dd:span span.name:custom-operation-name other:tag
func myFunction() {
  // The default operation name would have been "myFunction"
}

Function literal expressions don’t have a function name, and their default operation name is the value of the very first directive argument (if there is one). If there are no directive arguments, the operation name will remain blank.

//dd:span other:tag span.name:custom-operation-name
myOp := func() {
  // The default operation name would have been "tag"
}

Annotated with dd:span

Join Point
Function body
  • Has directive//dd:span
Advice
Prepend statements produced by the following template:
// Using the following synthetic imports:
import (
	context "context"
	tracer  "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
{{- $ctx := .Function.ArgumentOfType "context.Context" -}}
{{- $req := .Function.ArgumentOfType "*net/http.Request" -}}
{{- if (eq $ctx "") -}}
	{{- $ctx = "ctx" -}}
	ctx := {{- with $req -}}
		{{ $req }}.Context()
	{{- else -}}
		context.TODO()
	{{- end }}
{{ end -}}

{{ $functionName := .Function.Name -}}
{{- $opName := $functionName -}}
{{- range .DirectiveArgs "dd:span" -}}
	{{- if eq $opName "" -}}
		{{ $opName = .Value }}
	{{- end -}}
	{{- if eq .Key "span.name" -}}
		{{- $opName = .Value -}}
		{{- break -}}
	{{- end -}}
{{- end -}}

var span tracer.Span
span, {{ $ctx }} = tracer.StartSpanFromContext({{ $ctx }}, {{ printf "%q" $opName }},
	{{- with $functionName }}
		tracer.Tag("function-name", {{ printf "%q" $functionName }}),
	{{ end -}}
	{{- range .DirectiveArgs "dd:span" }}
		{{ if eq .Key "span.name" -}}{{- continue -}}{{- end -}}
		tracer.Tag({{ printf "%q" .Key }}, {{ printf "%q" .Value }}),
	{{- end }}
)
{{- with $req }}
	{{ $req }} = {{ $req }}.WithContext({{ $ctx }})
{{- end }}

{{ with .Function.ResultOfType "error" -}}
	defer func(){
		span.Finish(tracer.WithError({{ . }}))
	}()
{{ else -}}
	defer span.Finish()
{{- end -}}