Repository: incubator-htrace
Updated Branches:
  refs/heads/master 3e6288491 -> bbe0c6c87


HTRACE-52. Set content-type in htraced JSON API (cmccabe)


Project: http://git-wip-us.apache.org/repos/asf/incubator-htrace/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-htrace/commit/bbe0c6c8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-htrace/tree/bbe0c6c8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-htrace/diff/bbe0c6c8

Branch: refs/heads/master
Commit: bbe0c6c87c7facf062e33caaad85a3ec93a7ab91
Parents: 3e62884
Author: Colin P. Mccabe <[email protected]>
Authored: Sun Jan 4 23:34:53 2015 -0800
Committer: Colin P. Mccabe <[email protected]>
Committed: Mon Jan 5 08:56:14 2015 -0800

----------------------------------------------------------------------
 .../src/go/src/org/apache/htrace/common/span.go |  2 +-
 .../src/go/src/org/apache/htrace/htrace/cmd.go  | 15 +++----
 .../go/src/org/apache/htrace/htraced/rest.go    | 42 +++++++++++++-------
 3 files changed, 36 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/bbe0c6c8/htrace-core/src/go/src/org/apache/htrace/common/span.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/common/span.go 
b/htrace-core/src/go/src/org/apache/htrace/common/span.go
index 4efc101..20d3e45 100644
--- a/htrace-core/src/go/src/org/apache/htrace/common/span.go
+++ b/htrace-core/src/go/src/org/apache/htrace/common/span.go
@@ -46,7 +46,7 @@ type TimelineAnnotation struct {
 type SpanId int64
 
 func (id SpanId) String() string {
-       return fmt.Sprintf("%016x", id)
+       return fmt.Sprintf("%016x", uint64(id))
 }
 
 func (id SpanId) Val() int64 {

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/bbe0c6c8/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go 
b/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
index d3129e4..c1ef249 100644
--- a/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
+++ b/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
@@ -45,10 +45,10 @@ func main() {
        serverInfo := app.Command("serverInfo", "Print information retrieved 
from an htraced server.")
        findSpan := app.Command("findSpan", "Print information about a trace 
span with a given ID.")
        findSpanId := findSpan.Flag("id", "Span ID to find, as a signed decimal 
64-bit "+
-               "number").Required().Int64()
+               "number").Required().Uint64()
        findChildren := app.Command("findChildren", "Print out the span IDs 
that are children of a given span ID.")
        parentSpanId := findChildren.Flag("id", "Span ID to print children for, 
as a signed decimal 64-bit "+
-               "number").Required().Int64()
+               "number").Required().Uint64()
        childLim := findChildren.Flag("lim", "Maximum number of child IDs to 
print.").Default("20").Int()
        writeSpans := app.Command("writeSpans", "Write spans to the server in 
JSON form.")
        spanJson := writeSpans.Flag("json", "The JSON span data to write to the 
server.").Required().String()
@@ -95,8 +95,8 @@ func printServerInfo(restAddr string) int {
 }
 
 // Print information about a trace span.
-func doFindSpan(restAddr string, sid int64) int {
-       buf, err := makeGetRequest(restAddr, fmt.Sprintf("%016x", sid))
+func doFindSpan(restAddr string, sid uint64) int {
+       buf, err := makeGetRequest(restAddr, fmt.Sprintf("span/%016x", sid))
        if err != nil {
                fmt.Printf("%s\n", err.Error())
                return 1
@@ -128,13 +128,13 @@ func doWriteSpans(restAddr string, spanJson string) int {
 }
 
 // Find information about the children of a span.
-func doFindChildren(restAddr string, sid int64, lim int) int {
-       buf, err := makeGetRequest(restAddr, 
fmt.Sprintf("%016x/children&lim=%d", sid, lim))
+func doFindChildren(restAddr string, sid uint64, lim int) int {
+       buf, err := makeGetRequest(restAddr, 
fmt.Sprintf("span/%016x/children?lim=%d", sid, lim))
        if err != nil {
                fmt.Printf("%s\n", err.Error())
                return 1
        }
-       var spanIds []int64
+       var spanIds []common.SpanId
        err = json.Unmarshal(buf, &spanIds)
        if err != nil {
                fmt.Printf("Error: error unmarshalling response body %s: %s\n",
@@ -159,6 +159,7 @@ func makeRestRequest(reqType string, restAddr string, 
reqName string,
        reqBody io.Reader) ([]byte, error) {
        url := fmt.Sprintf("http://%s/%s";, restAddr, reqName)
        req, err := http.NewRequest(reqType, url, reqBody)
+       req.Header.Set("Content-Type", "application/json")
        client := &http.Client{}
        resp, err := client.Do(req)
        if err != nil {

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/bbe0c6c8/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go 
b/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go
index 9f22de9..744a5b6 100644
--- a/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go
+++ b/htrace-core/src/go/src/org/apache/htrace/htraced/rest.go
@@ -21,6 +21,7 @@ package main
 
 import (
        "encoding/json"
+       "fmt"
        "github.com/gorilla/mux"
        "io"
        "log"
@@ -34,16 +35,31 @@ import (
        "strings"
 )
 
+// Set the response headers.
+func setResponseHeaders(hdr http.Header) {
+       hdr.Set("Content-Type", "application/json")
+}
+
+// Write a JSON error response.
+func writeError(w http.ResponseWriter, errCode int, fstr string, args 
...interface{}) {
+       str := fmt.Sprintf(fstr, args)
+       str = strings.Replace(str, `"`, `'`, -1)
+       log.Println(str)
+       w.WriteHeader(errCode)
+       w.Write([]byte(`{ "error" : "` + str + `"}`))
+}
+
 type serverInfoHandler struct {
 }
 
 func (handler *serverInfoHandler) ServeHTTP(w http.ResponseWriter, req 
*http.Request) {
+       setResponseHeaders(w.Header())
        version := common.ServerInfo{ReleaseVersion: common.RELEASE_VERSION,
                GitVersion: common.GIT_VERSION}
        buf, err := json.Marshal(&version)
        if err != nil {
-               log.Printf("error marshalling ServerInfo: %s\n", err.Error())
-               w.WriteHeader(http.StatusInternalServerError)
+               writeError(w, http.StatusInternalServerError,
+                       "error marshalling ServerInfo: %s\n", err.Error())
                return
        }
        w.Write(buf)
@@ -56,7 +72,8 @@ type dataStoreHandler struct {
 func (hand *dataStoreHandler) parse64(w http.ResponseWriter, str string) 
(int64, bool) {
        val, err := strconv.ParseUint(str, 16, 64)
        if err != nil {
-               w.WriteHeader(http.StatusBadRequest)
+               writeError(w, http.StatusBadRequest, "Failed to parse span ID 
%s: %s",
+                       str, err.Error())
                w.Write([]byte("Error parsing : " + err.Error()))
                return -1, false
        }
@@ -67,14 +84,12 @@ func (hand *dataStoreHandler) getReqField32(fieldName 
string, w http.ResponseWri
        req *http.Request) (int32, bool) {
        str := req.FormValue(fieldName)
        if str == "" {
-               w.WriteHeader(http.StatusBadRequest)
-               w.Write([]byte("No " + fieldName + " specified."))
+               writeError(w, http.StatusBadRequest, "No %s specified.", 
fieldName)
                return -1, false
        }
        val, err := strconv.ParseUint(str, 16, 32)
        if err != nil {
-               w.WriteHeader(http.StatusBadRequest)
-               w.Write([]byte("Error parsing " + fieldName + ": " + 
err.Error()))
+               writeError(w, http.StatusBadRequest, "Error parsing %s: %s.", 
fieldName, err.Error())
                return -1, false
        }
        return int32(val), true
@@ -85,6 +100,7 @@ type findSidHandler struct {
 }
 
 func (hand *findSidHandler) ServeHTTP(w http.ResponseWriter, req 
*http.Request) {
+       setResponseHeaders(w.Header())
        req.ParseForm()
        vars := mux.Vars(req)
        stringSid := vars["id"]
@@ -94,7 +110,7 @@ func (hand *findSidHandler) ServeHTTP(w http.ResponseWriter, 
req *http.Request)
        }
        span := hand.store.FindSpan(sid)
        if span == nil {
-               w.WriteHeader(http.StatusNoContent)
+               writeError(w, http.StatusNoContent, "No spans were specified.")
                return
        }
        w.Write(span.ToJson())
@@ -105,6 +121,7 @@ type findChildrenHandler struct {
 }
 
 func (hand *findChildrenHandler) ServeHTTP(w http.ResponseWriter, req 
*http.Request) {
+       setResponseHeaders(w.Header())
        req.ParseForm()
        vars := mux.Vars(req)
        stringSid := vars["id"]
@@ -118,10 +135,6 @@ func (hand *findChildrenHandler) ServeHTTP(w 
http.ResponseWriter, req *http.Requ
                return
        }
        children := hand.store.FindChildren(sid, lim)
-       if len(children) == 0 {
-               w.WriteHeader(http.StatusNoContent)
-               return
-       }
        jbytes, err := json.Marshal(children)
        if err != nil {
                panic(err)
@@ -134,6 +147,7 @@ type writeSpansHandler struct {
 }
 
 func (hand *writeSpansHandler) ServeHTTP(w http.ResponseWriter, req 
*http.Request) {
+       setResponseHeaders(w.Header())
        dec := json.NewDecoder(req.Body)
        spans := make([]*common.Span, 0, 32)
        for {
@@ -141,9 +155,7 @@ func (hand *writeSpansHandler) ServeHTTP(w 
http.ResponseWriter, req *http.Reques
                err := dec.Decode(&span)
                if err != nil {
                        if err != io.EOF {
-                               w.WriteHeader(http.StatusBadRequest)
-                               log.Printf("Error parsing spans: %s\n", 
err.Error())
-                               w.Write([]byte("Error parsing spans : " + 
err.Error()))
+                               writeError(w, http.StatusBadRequest, "Error 
parsing spans: %s", err.Error())
                                return
                        }
                        break

Reply via email to