Skip to content

Commit

Permalink
fix fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
pavolloffay committed Aug 15, 2017
1 parent 31eec24 commit 7b4e610
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 114 deletions.
2 changes: 1 addition & 1 deletion cmd/collector/app/zipkin/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (aH *APIHandler) saveSpans(w http.ResponseWriter, r *http.Request) {
return
}
} else if contentType == "application/json" {
tSpans, err = deserializeJson(bodyBytes)
tSpans, err = deserializeJSON(bodyBytes)
if err != nil {
http.Error(w, fmt.Sprintf(app.UnableToReadBodyErrFormat, err), http.StatusBadRequest)
return
Expand Down
26 changes: 13 additions & 13 deletions cmd/collector/app/zipkin/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,36 +126,36 @@ func TestJsonFormat(t *testing.T) {
server, handler := initializeTestServer(nil)
defer server.Close()

endpJson := createEndpoint("foo", "127.0.0.1", "2001:db8::c001", 66)
annoJson := createAnno("cs", 1515, endpJson)
binAnnoJson := createBinAnno("http.status_code", "200", endpJson)
spanJson := createSpan("bar", "1234567891234565", "1234567891234567", "1234567891234568", 156, 15145, false,
annoJson, binAnnoJson)
endpJSON := createEndpoint("foo", "127.0.0.1", "2001:db8::c001", 66)
annoJSON := createAnno("cs", 1515, endpJSON)
binAnnoJSON := createBinAnno("http.status_code", "200", endpJSON)
spanJSON := createSpan("bar", "1234567891234565", "1234567891234567", "1234567891234568", 156, 15145, false,
annoJSON, binAnnoJSON)

statusCode, resBodyStr, err := postBytes(server.URL+`/api/v1/spans`, []byte(spanJson), createHeader("application/json"))
statusCode, resBodyStr, err := postBytes(server.URL+`/api/v1/spans`, []byte(spanJSON), createHeader("application/json"))
assert.NoError(t, err)
assert.EqualValues(t, http.StatusAccepted, statusCode)
assert.EqualValues(t, "", resBodyStr)

// error zipkinSpanHandler
handler.zipkinSpansHandler.(*mockZipkinHandler).err = fmt.Errorf("Bad times ahead")
statusCode, resBodyStr, err = postBytes(server.URL+`/api/v1/spans`, []byte(spanJson), createHeader("application/json"))
statusCode, resBodyStr, err = postBytes(server.URL+`/api/v1/spans`, []byte(spanJSON), createHeader("application/json"))
assert.NoError(t, err)
assert.EqualValues(t, http.StatusInternalServerError, statusCode)
assert.EqualValues(t, "Cannot submit Zipkin batch: Bad times ahead\n", resBodyStr)

// error json no id
spanJson = createSpan("bar", "", "1234567891234567", "1234567891234568", 156, 15145, false,
annoJson, binAnnoJson)
statusCode, resBodyStr, err = postBytes(server.URL+`/api/v1/spans`, []byte(spanJson), createHeader("application/json"))
spanJSON = createSpan("bar", "", "1234567891234567", "1234567891234568", 156, 15145, false,
annoJSON, binAnnoJSON)
statusCode, resBodyStr, err = postBytes(server.URL+`/api/v1/spans`, []byte(spanJSON), createHeader("application/json"))
require.NoError(t, err)
assert.EqualValues(t, http.StatusBadRequest, statusCode)
assert.EqualValues(t, "Unable to process request body: id has an incorrect length\n", resBodyStr)

// error toThrift no id no integer
spanJson = createSpan("bar", "Z23456789123456A", "1234567891234567", "1234567891234568", 156, 15145, false,
annoJson, binAnnoJson)
statusCode, resBodyStr, err = postBytes(server.URL+`/api/v1/spans`, []byte(spanJson), createHeader("application/json"))
spanJSON = createSpan("bar", "Z23456789123456A", "1234567891234567", "1234567891234568", 156, 15145, false,
annoJSON, binAnnoJSON)
statusCode, resBodyStr, err = postBytes(server.URL+`/api/v1/spans`, []byte(spanJSON), createHeader("application/json"))
require.NoError(t, err)
assert.EqualValues(t, http.StatusBadRequest, statusCode)
assert.EqualValues(t, "Unable to process request body: strconv.ParseUint: parsing \"Z23456789123456A\": invalid syntax\n", resBodyStr)
Expand Down
92 changes: 57 additions & 35 deletions cmd/collector/app/zipkin/zipkin_json.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zipkin

import (
Expand All @@ -13,35 +33,37 @@ type endpoint struct {
ServiceName string `json:"serviceName"`
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
Port int16 `json:"port"`
Port int16 `json:"port"`
}
type annotation struct {
Endpoint endpoint `json:"endpoint"`
Value string `json:"value"`
Timestamp int64 `json:"timestamp"`
Value string `json:"value"`
Timestamp int64 `json:"timestamp"`
}
type binaryAnnotation struct {
Endpoint endpoint `json:"endpoint"`
Key string `json:"key"`
Value string `json:"value"`
Key string `json:"key"`
Value string `json:"value"`
}
type zipkinSpan struct {
ID string `json:"id"`
ParentID string `json:"parentId,omitempty"`
TraceID string `json:"traceId"`
Name string `json:"name"`
Timestamp *int64 `json:"timestamp"`
Duration *int64 `json:"duration"`
Debug bool `json:"debug"`
ID string `json:"id"`
ParentID string `json:"parentId,omitempty"`
TraceID string `json:"traceId"`
Name string `json:"name"`
Timestamp *int64 `json:"timestamp"`
Duration *int64 `json:"duration"`
Debug bool `json:"debug"`
Annotations []annotation `json:"annotations"`
BinaryAnnotations []binaryAnnotation `json:"binaryAnnotations"`
}

var spanIdError = errors.New("id has an incorrect length")
var parentIdError = errors.New("parentId has an incorrect length")
var traceIdError = errors.New("traceId has an incorrect length")
var (
errSpanID = errors.New("id has an incorrect length")
errParentID = errors.New("parentId has an incorrect length")
errTraceID = errors.New("traceId has an incorrect length")
)

func deserializeJson(body []byte) ([]*zipkincore.Span, error) {
func deserializeJSON(body []byte) ([]*zipkincore.Span, error) {
spans, err := decode(body)
if err != nil {
return nil, err
Expand All @@ -60,13 +82,13 @@ func decode(body []byte) ([]zipkinSpan, error) {

for _, span := range spans {
if len(span.TraceID) != 16 {
return nil, traceIdError
return nil, errTraceID
}
if len(span.ID) != 16 && len(span.ID) != 32 {
return nil, spanIdError
return nil, errSpanID
}
if len(span.ParentID) > 0 && len(span.ParentID) != 16 {
return nil, parentIdError
return nil, errParentID
}
}

Expand All @@ -91,26 +113,26 @@ func spanToThrift(span zipkinSpan) (*zipkincore.Span, error) {
if err != nil {
return nil, err
}
traceId, err := strconv.ParseUint(span.TraceID, 16, 64)
traceID, err := strconv.ParseUint(span.TraceID, 16, 64)
if err != nil {
return nil, err
}

tSpan := &zipkincore.Span{
ID: int64(id),
TraceID: int64(traceId),
Name: span.Name,
Debug: span.Debug,
ID: int64(id),
TraceID: int64(traceID),
Name: span.Name,
Debug: span.Debug,
Timestamp: span.Timestamp,
Duration: span.Duration,
Duration: span.Duration,
}

if len(span.ParentID) > 0 {
parentId, err := strconv.ParseUint(span.ParentID, 16, 64)
parentID, err := strconv.ParseUint(span.ParentID, 16, 64)
if err != nil {
return nil, err
}
signed := int64(parentId)
signed := int64(parentID)
tSpan.ParentID = &signed
}

Expand Down Expand Up @@ -140,8 +162,8 @@ func endpointToThrift(endp endpoint) (*zipkincore.Endpoint, error) {

return &zipkincore.Endpoint{
ServiceName: endp.ServiceName,
Port: endp.Port,
Ipv4: ipv4,
Port: endp.Port,
Ipv4: ipv4,
}, nil
}

Expand All @@ -153,8 +175,8 @@ func annoToThrift(anno annotation) (*zipkincore.Annotation, error) {

return &zipkincore.Annotation{
Timestamp: anno.Timestamp,
Value: anno.Value,
Host: endpoint,
Value: anno.Value,
Host: endpoint,
}, nil
}

Expand All @@ -165,9 +187,9 @@ func binAnnoToThrift(binAnno binaryAnnotation) (*zipkincore.BinaryAnnotation, er
}

return &zipkincore.BinaryAnnotation{
Key: binAnno.Key,
Value: []byte(binAnno.Value),
Host: endpoint,
Key: binAnno.Key,
Value: []byte(binAnno.Value),
Host: endpoint,
AnnotationType: zipkincore.AnnotationType_STRING,
}, nil
}
Expand All @@ -186,7 +208,7 @@ func parseIpv4(str string) (int32, error) {
}

parsed32 := int32(parsed)
ipv4 = ipv4 << 8 | (parsed32 & 0xff)
ipv4 = ipv4<<8 | (parsed32 & 0xff)
}

return ipv4, nil
Expand Down
Loading

0 comments on commit 7b4e610

Please sign in to comment.