Skip to content

Commit

Permalink
Adds post sample for go
Browse files Browse the repository at this point in the history
  • Loading branch information
devinsba authored and Adrian Cole committed Aug 15, 2017
1 parent 3155601 commit a13b515
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -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
```
52 changes: 52 additions & 0 deletions go/post_sample_trace.go
Original file line number Diff line number Diff line change
@@ -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()))
}

0 comments on commit a13b515

Please sign in to comment.