census-instrumentation/opencensus-go

Spans getting multiplied across subsequent http requests

petedzappulla opened this issue · 0 comments

Apologies for the terrible title, but this issue is strange and difficult to explain. I'm using a default exporter to write to Microsoft's LocalForwarder for Application Insights telemetry. When I run my go app, my initial http request correctly creates a span and correctly associates a child span to the parent. However, the next time I hit that endpoint, a new trace is successfully created, but now it has two parent spans (at the root level) and two children spans that are both associated to last parent span in the root. A third request will triple the spans, and so on.

Please see attached screen shot describing the problem.
Span Issue

Here's the code that's causing the problem:

func (assetController *AssetController) GetByIdReqTop(writer *interfaces.FileResponseWriter, request *http.Request) {

	//setup opencensus ~~~~~~~~~~~~~~~~~~~
	serviceName := os.Getenv("SERVICE_NAME")
	if len(serviceName) == 0 {
		serviceName = "go-app"
	}

	agentEndpoint := os.Getenv("OCAGENT_TRACE_EXPORTER_ENDPOINT")

	if len(agentEndpoint) == 0 {
		agentEndpoint = fmt.Sprintf("%s:%d", ocagent.DefaultAgentHost, ocagent.DefaultAgentPort)
	}

	exporter, _ := ocagent.NewExporter(ocagent.WithInsecure(), ocagent.WithServiceName(serviceName), ocagent.WithAddress(agentEndpoint))

	trace.RegisterExporter(exporter)

	trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	ctx, span := trace.StartSpan(request.Context(), "GetByIdReqTop", trace.WithSpanKind(trace.SpanKindServer))
	defer span.End()


//..more code in here


	ctx2, span2 := trace.StartSpan(ctx, "Child of GetByIdReqTop", trace.WithSpanKind(trace.SpanKindServer))
	defer span2.End()


//..more code in here


}