openzipkin / zipkin-go

Zipkin distributed tracing library for go.
Apache License 2.0
612 stars 114 forks source link

adding option to send synchronous tracing #187

Closed arun0009 closed 3 years ago

arun0009 commented 3 years ago

We aren't getting tracing when using zipkin-go in lambda function unless we add some kind of sleep so that tracing data gets a chance to be sent to reporter (default time is 1 second).

Here's an example, if you run this code on a local Zipkin docker, it won't show any trace unless we uncomment the last sleep statement.

package main

import (
    "context"
    "fmt"
    "github.com/openzipkin/zipkin-go"
    zipkinHttpReporter "github.com/openzipkin/zipkin-go/reporter/http"
    "time"
)

const ZipkinHttpEndpoint = "http://localhost:9411/api/v2/spans"

func test(){
    time.Sleep(1 * time.Second)
}

func main(){
    reporter := zipkinHttpReporter.NewReporter(ZipkinHttpEndpoint)
    endpoint, err := zipkin.NewEndpoint("myService", "")
    if err != nil {
        fmt.Println("error creating endpoint", err)
    }
    tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint))
    if err != nil {
        fmt.Println("error creating tracer", err)
    }
    span, _ := tracer.StartSpanFromContext(context.Background(), "test")
    test()
    span.Finish()
    //time.Sleep(5 * time.Second)
}

Allowing to set an asynchronous option (defaulting to true, to maintain current behavior), if set to false will send reporting data in sync. Added tests to show async vs sync behavior.

coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.01%) to 73.69% when pulling 477fd143e86f35ae8634e9ce713c5cdf1e1eaae5 on arun0009:master into 0b3ebdbc2ddf7409f84316407fec22faf1ce8a0f on openzipkin:master.

coveralls commented 3 years ago

Coverage Status

Coverage decreased (-0.03%) to 73.67% when pulling eaaf40b1ba8f3369aae5a3881b5eec12ba2ec11a on arun0009:master into 0b3ebdbc2ddf7409f84316407fec22faf1ce8a0f on openzipkin:master.

basvanbeek commented 3 years ago

Hi this change is not needed. If you close the reporter on exit it will send completed spans that are still in the reporter queue.

Per your example:

package main

import (
    "context"
    "fmt"

    "github.com/openzipkin/zipkin-go"
    zipkinHttpReporter "github.com/openzipkin/zipkin-go/reporter/http"
)

const ZipkinHttpEndpoint = "http://localhost:9411/api/v2/spans"

func main() {
    reporter := zipkinHttpReporter.NewReporter(ZipkinHttpEndpoint)
    defer reporter.Close() // this will make sure the spans are reported to the collector on exit.

    endpoint, err := zipkin.NewEndpoint("myService", "")
    if err != nil {
        fmt.Println("error creating endpoint", err)
    }
    tracer, err := zipkin.NewTracer(reporter, zipkin.WithLocalEndpoint(endpoint))
    if err != nil {
        fmt.Println("error creating tracer", err)
    }
    span, _ := tracer.StartSpanFromContext(context.Background(), "test")
    span.Finish()
}
arun0009 commented 3 years ago

closing this issue as reporter.Close() will send spans over before lambda terminates