prepend-statements

The prepend-statements advice inserts new statements rendered by the provided code template before the matched AST node. This is often used to add logic in the preamble of function implementations, and the defer keyword can be used to also introduce epilogue logic.

Examples

prepend-statements:
    imports:
        ddtrace: gopkg.in/DataDog/dd-trace-go.v1/ddtrace
        ext: gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext
        globalconfig: gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig
        http: net/http
        internal: gopkg.in/DataDog/dd-trace-go.v1/internal
        math: math
        namingschema: gopkg.in/DataDog/dd-trace-go.v1/internal/namingschema
        tracer: gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer
    template: |-
        {{- $res := .Function.Result 0 -}}
        defer func() {
          var analyticsRate float64
          if internal.BoolEnv("DD_TRACE_MUX_ANALYTICS_ENABLED", false) {
            analyticsRate = 1.0
          } else {
            analyticsRate = globalconfig.AnalyticsRate()
          }

          {{ $res }}.__dd_config.headerTags = globalconfig.HeaderTagMap()
          {{ $res }}.__dd_config.ignoreRequest = func(*http.Request) bool { return false }
          {{ $res }}.__dd_config.resourceNamer = ddDefaultResourceNamer
          {{ $res }}.__dd_config.serviceName = namingschema.ServiceName("mux.router")
          {{ $res }}.__dd_config.spanOpts = []ddtrace.StartSpanOption{
            tracer.Tag(ext.Component, "gorilla/mux"),
            tracer.Tag(ext.SpanKind, ext.SpanKindServer),
          }
          if !math.IsNaN(analyticsRate) {
            {{ $res }}.__dd_config.spanOpts = append(
              {{ $res }}.__dd_config.spanOpts,
              tracer.Tag(ext.EventSampleRate, analyticsRate),
            )
          }
        }()