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

cmd/tracegen: Try to extract context from other params #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

cmd/tracegen: Try to extract context from other params #13

wants to merge 1 commit into from

Conversation

xswordsx
Copy link
Contributor

@xswordsx xswordsx commented Nov 12, 2019

Why was this pull request opened?

There are cases in which the traced interface does not accept a context.Context, but rather, uses the context from the passed down *http.Request. These cases can also be traced with a little change in the tracegen tool.

What does this Pull request do?

This PR addresses the above-mentioned case.

Anything worth noting?

The code does not check if the *Request is actually net/http or some other package (see TODO).

Can you provide a minimal example?

Example

Given the source code ifc.go in github.com/Bo0mer/gentools/ifc:

package ifc

import (
        "context"
        "net/http"
)

//go:generate tracegen . Spammer

// Spammer spams messages to given request.
type Spammer interface {
        Spam(*http.Request)
        SpamWithContext(context.Context, *http.Request)
        SpamContext(context.Context)
}

running the tool produces the following file:

// Code generated by tracegen. DO NOT EDIT.
package ifcmws

import (
        alias4 "context"
        alias1 "github.com/Bo0mer/gentools/ifc"
        alias2 "go.opencensus.io/trace"
        alias3 "net/http"
)

type tracingSpammer struct {
        next alias1.Spammer
}

// NewTracingSpammer creates new tracing middleware.
func NewTracingSpammer(next alias1.Spammer) alias1.Spammer {
        return &tracingSpammer{next}
}
func (m *tracingSpammer) Spam(arg1 *alias3.Request) {
        ctx := arg1.Context()
        ctx, _span := alias2.StartSpan(ctx, "github.com/Bo0mer/gentools/ifc.Spammer.Spam")
        defer _span.End()
        arg1 = arg1.WithContext(ctx)
        m.next.Spam(arg1)
}
func (m *tracingSpammer) SpamWithContext(arg1 alias4.Context, arg2 *alias3.Request) {
        arg1, _span := alias2.StartSpan(arg1, "github.com/Bo0mer/gentools/ifc.Spammer.SpamWithContext")
        defer _span.End()
        m.next.SpamWithContext(arg1, arg2)
}
func (m *tracingSpammer) SpamContext(arg1 alias4.Context) {
        arg1, _span := alias2.StartSpan(arg1, "github.com/Bo0mer/gentools/ifc.Spammer.SpamContext")
        defer _span.End()
        m.next.SpamContext(arg1)
}

@xswordsx xswordsx marked this pull request as ready for review November 12, 2019 14:18
Sometimes, `*http.Request` is given as a param, which can be used for
extracting `context.Context` and using it for tracing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant