inject-declarations

The inject-declarations advice merges declarations produced by a code template into the matched node’s compilation unit.

This is often used to introduce new type definitions or declare foreign functions linked via //go:linkname to avoid creating dependency cycles.

Examples

inject-declarations:
    imports:
        context: context
        ddtrace: gopkg.in/DataDog/dd-trace-go.v1/ddtrace
    links:
        - gopkg.in/DataDog/dd-trace-go.v1/internal/appsec
        - gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/emitter/httpsec
        - gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer
    template: |
        //go:linkname __dd_appsec_RASPEnabled gopkg.in/DataDog/dd-trace-go.v1/internal/appsec.RASPEnabled
        func __dd_appsec_RASPEnabled() bool

        //go:linkname __dd_httpsec_ProtectRoundTrip gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/emitter/httpsec.ProtectRoundTrip
        func __dd_httpsec_ProtectRoundTrip(context.Context, string) error

        //go:linkname __dd_tracer_SpanType gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer.SpanType
        func __dd_tracer_SpanType(string) ddtrace.StartSpanOption

        //go:linkname __dd_tracer_ResourceName gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer.ResourceName
        func __dd_tracer_ResourceName(string) ddtrace.StartSpanOption

        //go:linkname __dd_tracer_Tag gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer.Tag
        func __dd_tracer_Tag(string, any) ddtrace.StartSpanOption

        //go:linkname __dd_tracer_StartSpanFromContext gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer.StartSpanFromContext
        func __dd_tracer_StartSpanFromContext(context.Context, string, ...ddtrace.StartSpanOption) (ddtrace.Span, context.Context)

        //go:linkname __dd_tracer_WithError gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer.WithError
        func __dd_tracer_WithError(error) ddtrace.FinishOption

        //go:linkname __dd_tracer_Inject gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer.Inject
        func __dd_tracer_Inject(ddtrace.SpanContext, any) error

        type __dd_tracer_HTTPHeadersCarrier Header
        func (c __dd_tracer_HTTPHeadersCarrier) Set(key, val string) {
          Header(c).Set(key, val)
        }        
inject-declarations:
    imports:
        ddtrace: gopkg.in/DataDog/dd-trace-go.v1/ddtrace
        http: net/http
        internal: gopkg.in/DataDog/dd-trace-go.v1/internal
        telemetry: gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry
        tracer: gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer
    template: |-
        type ddRouterConfig struct {
          ignoreRequest func(*http.Request) bool
          headerTags    *internal.LockMap
          resourceNamer func(*Router, *http.Request) string
          serviceName   string
          spanOpts      []ddtrace.StartSpanOption
        }

        func ddDefaultResourceNamer(router *Router, req *http.Request) string {
          var (
            match RouteMatch
            route = "unknown"
          )
          if router.Match(req, &match) && match.Route != nil {
            if r, err := match.Route.GetPathTemplate(); err == nil {
              route = r
            }
          }
          return fmt.Sprintf("%s %s", req.Method, route)
        }

        func init() {
          telemetry.LoadIntegration("gorilla/mux")
          tracer.MarkIntegrationImported("github.com/gorilla/mux")
        }