From a13b5154e13bde36edb17ed877d76fa98298b21d Mon Sep 17 00:00:00 2001 From: Brian Devins Date: Fri, 21 Apr 2017 10:43:26 -0400 Subject: [PATCH] Adds post sample for go --- README.md | 7 ++++++ go/README.md | 12 ++++++++++ go/post_sample_trace.go | 52 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 README.md create mode 100644 go/README.md create mode 100644 go/post_sample_trace.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..049916e --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# zipkin-api-example +This repository shows you how to use the OpenApi/Swagger definition of +the zipkin-server http api. + +Here's a list of examples: + +* [go](go/README.md) - Shows how to generate a client and upload a sample trace. diff --git a/go/README.md b/go/README.md new file mode 100644 index 0000000..9273487 --- /dev/null +++ b/go/README.md @@ -0,0 +1,12 @@ +## Swagger + +The examples in this directory assume that the zipkin API client has been generated using +[go-swagger](https://github.com/go-swagger/go-swagger) into the `client` directory. + +```bash +# once you've installed swagger, build the client +$ wget https://raw.githubusercontent.com/openzipkin/zipkin-api/master/zipkin2-api.yaml +$ swagger generate client -f zipkin2-api.yaml -A zipkin +# now, you can run one of the examples, such as POSTing a test span +$ DEBUG=1 go run post_sample_trace.go +``` diff --git a/go/post_sample_trace.go b/go/post_sample_trace.go new file mode 100644 index 0000000..f9f7c3a --- /dev/null +++ b/go/post_sample_trace.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "log" + "math/rand" + "strconv" + "time" + + httptransport "github.com/go-openapi/runtime/client" + "github.com/go-openapi/strfmt" + "github.com/openzipkin/zipkin-api-example/go/client" + "github.com/openzipkin/zipkin-api-example/go/client/operations" + "github.com/openzipkin/zipkin-api-example/go/models" +) + +func main() { + transport := httptransport.New("localhost:9411", "/api/v2", []string{"http"}) + zipkin := client.New(transport, strfmt.Default) + + id := uint64(rand.New(rand.NewSource(time.Now().UnixNano())).Int63()) + spanID := strconv.FormatUint(id, 16) + timestamp := int64(time.Now().UnixNano() / int64(time.Microsecond)) + span := &models.Span{ + TraceID: spanID, + ID: spanID, + Kind: models.SpanKindSERVER, + Name: "test", + Timestamp: timestamp, + Duration: int64(200000), + LocalEndpoint: &models.Endpoint{ + ServiceName: "testService", + IPV4: "192.228.233.62", + Port: 80, + }, + RemoteEndpoint: &models.Endpoint{ + IPV4: "192.228.233.62", + }, + Tags: map[string]string{ + "http.path": "/api", + }, + } + + resp, err := zipkin.Operations.PostSpans( + operations.NewPostSpansParams().WithSpans(models.ListOfSpans{span}), + ) + + if err != nil { + log.Fatal(err) + } + fmt.Println(string(resp.Error())) +}