gogoogle-cloud-platformgoogle-cloud-stackdriveropencensus

Are there any config NOT to use trace on specific environment


Now I have a Golang Application deployed on GAE, with stackdriver trace. About stackdriver Trace, to get custom span data, I did set up on my code, like

        exporter, err := stackdriver.NewExporter(stackdriver.Options{
                ProjectID: os.Getenv("GOOGLE_CLOUD_PROJECT"),
        })
        if err != nil {
                log.Fatal(err)
        }
        trace.RegisterExporter(exporter)

        client := &http.Client{
                Transport: &ochttp.Transport{
                        // Use Google Cloud propagation format.
                        Propagation: &propagation.HTTPFormat{},
                },
        }

ref. https://cloud.google.com/trace/docs/setup/go

On GAE, I succeed in viewing trace on my GCP console.

but, I DON'T want to trace these log on my local developing environment (I'm using docker).currently, I try to run my application on docker, nil pointer panic shows up on Span.Export() which may be called from Span.End().

So, I wonder if someone knows the way to DISABLE stackdriver trace on specific environment (with my case, on docker).

Otherwise, should I check condition of trace configuration, like as below ?

    if trace.projectId != "" {

     ctx := reque.Context()
     _, span := trace.StartSpan(ctx,"Span blahblah")
     defer span.End()
    }

Solution

  • There is no point for Google in adding an extra logic like you need into the Trace code, the GAE apps are instrumented with, in order to disable that Trace code when the GAE App is executed somewhere in a third party environment like Docker on-prem. Most likely an answer to the question is "No, there is no magic config for that". Hence it is up-to-you how to sort this out.

    As a general idea: following up an approach with [NoopExporter] offered by Emile Pels and having admitted the fact we can't get rid of the Trace code with "magic config", if I developed my app in Python I'd considered using decorator as a wrapper to bring piece of intelligence into the Trace calls, or redefining them as mock functions. It seems Golang does not have direct analog of Python decorators but this functionality could be implemented somehow. This is being discussed in the Internet, for example here:

    Go Decorator Function Pattern

    Redefine function so that it references its own self