Repository: incubator-htrace Updated Branches: refs/heads/master a560a555f -> 42b2f6a2c
http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/42b2f6a2/htrace-htraced/src/go/src/org/apache/htrace/htraced/hrpc.go ---------------------------------------------------------------------- diff --git a/htrace-htraced/src/go/src/org/apache/htrace/htraced/hrpc.go b/htrace-htraced/src/go/src/org/apache/htrace/htraced/hrpc.go deleted file mode 100644 index a53380e..0000000 --- a/htrace-htraced/src/go/src/org/apache/htrace/htraced/hrpc.go +++ /dev/null @@ -1,251 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package main - -import ( - "bufio" - "bytes" - "encoding/binary" - "encoding/json" - "errors" - "fmt" - "github.com/ugorji/go/codec" - "io" - "net" - "net/rpc" - "org/apache/htrace/common" - "org/apache/htrace/conf" -) - -// Handles HRPC calls -type HrpcHandler struct { - lg *common.Logger - store *dataStore -} - -// The HRPC server -type HrpcServer struct { - *rpc.Server - hand *HrpcHandler - listener net.Listener -} - -// Codec which encodes HRPC data via JSON -type HrpcServerCodec struct { - lg *common.Logger - conn net.Conn - length uint32 -} - -func asJson(val interface{}) string { - js, err := json.Marshal(val) - if err != nil { - return "encoding error: " + err.Error() - } - return string(js) -} - -func createErrAndWarn(lg *common.Logger, val string) error { - return createErrAndLog(lg, val, common.WARN) -} - -func createErrAndLog(lg *common.Logger, val string, level common.Level) error { - lg.Write(level, val+"\n") - return errors.New(val) -} - -func (cdc *HrpcServerCodec) ReadRequestHeader(req *rpc.Request) error { - hdr := common.HrpcRequestHeader{} - if cdc.lg.TraceEnabled() { - cdc.lg.Tracef("Reading HRPC request header from %s\n", cdc.conn.RemoteAddr()) - } - err := binary.Read(cdc.conn, binary.LittleEndian, &hdr) - if err != nil { - level := common.WARN - if err == io.EOF { - level = common.DEBUG - } - return createErrAndLog(cdc.lg, fmt.Sprintf("Error reading header bytes: %s", - err.Error()), level) - } - if cdc.lg.TraceEnabled() { - cdc.lg.Tracef("Read HRPC request header %s from %s\n", - asJson(&hdr), cdc.conn.RemoteAddr()) - } - if hdr.Magic != common.HRPC_MAGIC { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Invalid request header: expected "+ - "magic number of 0x%04x, but got 0x%04x", common.HRPC_MAGIC, hdr.Magic)) - } - if hdr.Length > common.MAX_HRPC_BODY_LENGTH { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Length prefix was too long. "+ - "Maximum length is %d, but we got %d.", common.MAX_HRPC_BODY_LENGTH, - hdr.Length)) - } - req.ServiceMethod = common.HrpcMethodIdToMethodName(hdr.MethodId) - if req.ServiceMethod == "" { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Unknown MethodID code 0x%04x", - hdr.MethodId)) - } - req.Seq = hdr.Seq - cdc.length = hdr.Length - return nil -} - -func (cdc *HrpcServerCodec) ReadRequestBody(body interface{}) error { - if cdc.lg.TraceEnabled() { - cdc.lg.Tracef("Reading HRPC %d-byte request body from %s\n", - cdc.length, cdc.conn.RemoteAddr()) - } - mh := new(codec.MsgpackHandle) - mh.WriteExt = true - dec := codec.NewDecoder(io.LimitReader(cdc.conn, int64(cdc.length)), mh) - err := dec.Decode(body) - if err != nil { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Failed to read request "+ - "body from %s: %s", cdc.conn.RemoteAddr(), err.Error())) - } - if cdc.lg.TraceEnabled() { - cdc.lg.Tracef("Read body from %s: %s\n", - cdc.conn.RemoteAddr(), asJson(&body)) - } - return nil -} - -var EMPTY []byte = make([]byte, 0) - -func (cdc *HrpcServerCodec) WriteResponse(resp *rpc.Response, msg interface{}) error { - var err error - buf := EMPTY - if msg != nil { - mh := new(codec.MsgpackHandle) - mh.WriteExt = true - w := bytes.NewBuffer(make([]byte, 0, 128)) - enc := codec.NewEncoder(w, mh) - err := enc.Encode(msg) - if err != nil { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Failed to marshal "+ - "response message: %s", err.Error())) - } - buf = w.Bytes() - } - hdr := common.HrpcResponseHeader{} - hdr.MethodId = common.HrpcMethodNameToId(resp.ServiceMethod) - hdr.Seq = resp.Seq - hdr.ErrLength = uint32(len(resp.Error)) - hdr.Length = uint32(len(buf)) - writer := bufio.NewWriterSize(cdc.conn, 256) - err = binary.Write(writer, binary.LittleEndian, &hdr) - if err != nil { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Failed to write response "+ - "header: %s", err.Error())) - } - if hdr.ErrLength > 0 { - _, err = io.WriteString(writer, resp.Error) - if err != nil { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Failed to write error "+ - "string: %s", err.Error())) - } - } - if hdr.Length > 0 { - var length int - length, err = writer.Write(buf) - if err != nil { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Failed to write response "+ - "message: %s", err.Error())) - } - if uint32(length) != hdr.Length { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Failed to write all of "+ - "response message: %s", err.Error())) - } - } - err = writer.Flush() - if err != nil { - return createErrAndWarn(cdc.lg, fmt.Sprintf("Failed to write the response "+ - "bytes: %s", err.Error())) - } - return nil -} - -func (cdc *HrpcServerCodec) Close() error { - return cdc.conn.Close() -} - -func (hand *HrpcHandler) WriteSpans(req *common.WriteSpansReq, - resp *common.WriteSpansResp) (err error) { - hand.lg.Debugf("hrpc writeSpansHandler: received %d span(s). "+ - "defaultPid = %s\n", len(req.Spans), req.DefaultPid) - for i := range req.Spans { - span := req.Spans[i] - if span.ProcessId == "" { - span.ProcessId = req.DefaultPid - } - if hand.lg.TraceEnabled() { - hand.lg.Tracef("writing span %d: %s\n", i, span.ToJson()) - } - hand.store.WriteSpan(span) - } - return nil -} - -func CreateHrpcServer(cnf *conf.Config, store *dataStore) (*HrpcServer, error) { - lg := common.NewLogger("hrpc", cnf) - hsv := &HrpcServer{ - Server: rpc.NewServer(), - hand: &HrpcHandler{ - lg: lg, - store: store, - }, - } - var err error - hsv.listener, err = net.Listen("tcp", cnf.Get(conf.HTRACE_HRPC_ADDRESS)) - if err != nil { - return nil, err - } - hsv.Server.Register(hsv.hand) - go hsv.run() - lg.Infof("Started HRPC server on %s...\n", hsv.listener.Addr().String()) - return hsv, nil -} - -func (hsv *HrpcServer) run() { - lg := hsv.hand.lg - for { - conn, err := hsv.listener.Accept() - if err != nil { - lg.Errorf("HRPC Accept error: %s\n", err.Error()) - continue - } - if lg.TraceEnabled() { - lg.Tracef("Accepted HRPC connection from %s\n", conn.RemoteAddr()) - } - go hsv.ServeCodec(&HrpcServerCodec{ - lg: lg, - conn: conn, - }) - } -} - -func (hsv *HrpcServer) Addr() net.Addr { - return hsv.listener.Addr() -} - -func (hsv *HrpcServer) Close() { - hsv.listener.Close() -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/42b2f6a2/htrace-htraced/src/go/src/org/apache/htrace/htraced/htraced.go ---------------------------------------------------------------------- diff --git a/htrace-htraced/src/go/src/org/apache/htrace/htraced/htraced.go b/htrace-htraced/src/go/src/org/apache/htrace/htraced/htraced.go deleted file mode 100644 index 64da457..0000000 --- a/htrace-htraced/src/go/src/org/apache/htrace/htraced/htraced.go +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package main - -import ( - "encoding/json" - "fmt" - "net" - "org/apache/htrace/common" - "org/apache/htrace/conf" - "os" - "strings" - "time" -) - -var RELEASE_VERSION string -var GIT_VERSION string - -const USAGE = `htraced: the HTrace server daemon. - -htraced receives trace spans sent from HTrace clients. It exposes a REST -interface which others can query. It also runs a web server with a graphical -user interface. htraced stores its span data in levelDB files on the local -disks. - -Usage: ---help: this help message - --Dk=v: set configuration key 'k' to value 'v' -For example -Dweb.address=127.0.0.1:8080 sets the web address to localhost, -port 8080. - --Dk: set configuration key 'k' to 'true' - -Normally, configuration options should be set in the ` + conf.CONFIG_FILE_NAME + ` -configuration file. We find this file by searching the paths in the -` + conf.HTRACED_CONF_DIR + `. The command-line options are just an alternate way -of setting configuration when launching the daemon. -` - -func main() { - for idx := range os.Args { - arg := os.Args[idx] - if strings.HasPrefix(arg, "--h") || strings.HasPrefix(arg, "-h") { - fmt.Fprintf(os.Stderr, USAGE) - os.Exit(0) - } - } - cnf := common.LoadApplicationConfig() - common.InstallSignalHandlers(cnf) - lg := common.NewLogger("main", cnf) - defer lg.Close() - store, err := CreateDataStore(cnf, nil) - if err != nil { - lg.Errorf("Error creating datastore: %s\n", err.Error()) - os.Exit(1) - } - var rsv *RestServer - rsv, err = CreateRestServer(cnf, store) - if err != nil { - lg.Errorf("Error creating REST server: %s\n", err.Error()) - os.Exit(1) - } - var hsv *HrpcServer - if cnf.Get(conf.HTRACE_HRPC_ADDRESS) != "" { - hsv, err = CreateHrpcServer(cnf, store) - if err != nil { - lg.Errorf("Error creating HRPC server: %s\n", err.Error()) - os.Exit(1) - } - } else { - lg.Infof("Not starting HRPC server because no value was given for %s.\n", - conf.HTRACE_HRPC_ADDRESS) - } - naddr := cnf.Get(conf.HTRACE_STARTUP_NOTIFICATION_ADDRESS) - if naddr != "" { - notif := StartupNotification{ - HttpAddr: rsv.Addr().String(), - ProcessId: os.Getpid(), - } - if hsv != nil { - notif.HrpcAddr = hsv.Addr().String() - } - err = sendStartupNotification(naddr, ¬if) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to send startup notification: "+ - "%s\n", err.Error()) - os.Exit(1) - } - } - for { - time.Sleep(time.Duration(10) * time.Hour) - } -} - -// A startup notification message that we optionally send on startup. -// Used by unit tests. -type StartupNotification struct { - HttpAddr string - HrpcAddr string - ProcessId int -} - -func sendStartupNotification(naddr string, notif *StartupNotification) error { - conn, err := net.Dial("tcp", naddr) - if err != nil { - return err - } - defer func() { - if conn != nil { - conn.Close() - } - }() - var buf []byte - buf, err = json.Marshal(notif) - if err != nil { - return err - } - _, err = conn.Write(buf) - conn.Close() - conn = nil - return nil -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/42b2f6a2/htrace-htraced/src/go/src/org/apache/htrace/htraced/mini_htraced.go ---------------------------------------------------------------------- diff --git a/htrace-htraced/src/go/src/org/apache/htrace/htraced/mini_htraced.go b/htrace-htraced/src/go/src/org/apache/htrace/htraced/mini_htraced.go deleted file mode 100644 index a54f2cb..0000000 --- a/htrace-htraced/src/go/src/org/apache/htrace/htraced/mini_htraced.go +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package main - -import ( - "fmt" - "io/ioutil" - "org/apache/htrace/common" - "org/apache/htrace/conf" - "os" - "strings" -) - -// -// MiniHTraceD is used in unit tests to set up a daemon with certain settings. -// It takes care of things like creating and cleaning up temporary directories. -// - -// The default number of managed data directories to use. -const DEFAULT_NUM_DATA_DIRS = 2 - -// Builds a MiniHTraced object. -type MiniHTracedBuilder struct { - // The name of the MiniHTraced to build. This shows up in the test directory name and some - // other places. - Name string - - // The configuration values to use for the MiniHTraced. - // If ths is nil, we use the default configuration for everything. - Cnf map[string]string - - // The DataDirs to use. Empty entries will turn into random names. - DataDirs []string - - // If true, we will keep the data dirs around after MiniHTraced#Close - KeepDataDirsOnClose bool - - // If non-null, the WrittenSpans channel to use when creating the DataStore. - WrittenSpans chan *common.Span -} - -type MiniHTraced struct { - Name string - Cnf *conf.Config - DataDirs []string - Store *dataStore - Rsv *RestServer - Hsv *HrpcServer - Lg *common.Logger - KeepDataDirsOnClose bool -} - -func (bld *MiniHTracedBuilder) Build() (*MiniHTraced, error) { - var err error - var store *dataStore - var rsv *RestServer - var hsv *HrpcServer - if bld.Name == "" { - bld.Name = "HTraceTest" - } - if bld.Cnf == nil { - bld.Cnf = make(map[string]string) - } - if bld.DataDirs == nil { - bld.DataDirs = make([]string, 2) - } - for idx := range bld.DataDirs { - if bld.DataDirs[idx] == "" { - bld.DataDirs[idx], err = ioutil.TempDir(os.TempDir(), - fmt.Sprintf("%s%d", bld.Name, idx+1)) - if err != nil { - return nil, err - } - } - } - bld.Cnf[conf.HTRACE_DATA_STORE_DIRECTORIES] = - strings.Join(bld.DataDirs, conf.PATH_LIST_SEP) - bld.Cnf[conf.HTRACE_WEB_ADDRESS] = ":0" // use a random port for the REST server - bld.Cnf[conf.HTRACE_HRPC_ADDRESS] = ":0" // use a random port for the HRPC server - bld.Cnf[conf.HTRACE_LOG_LEVEL] = "TRACE" - cnfBld := conf.Builder{Values: bld.Cnf, Defaults: conf.DEFAULTS} - cnf, err := cnfBld.Build() - if err != nil { - return nil, err - } - lg := common.NewLogger("mini.htraced", cnf) - defer func() { - if err != nil { - if store != nil { - store.Close() - } - for idx := range bld.DataDirs { - if bld.DataDirs[idx] != "" { - os.RemoveAll(bld.DataDirs[idx]) - } - } - if rsv != nil { - rsv.Close() - } - lg.Infof("Failed to create MiniHTraced %s: %s\n", bld.Name, err.Error()) - lg.Close() - } - }() - store, err = CreateDataStore(cnf, bld.WrittenSpans) - if err != nil { - return nil, err - } - rsv, err = CreateRestServer(cnf, store) - if err != nil { - return nil, err - } - hsv, err = CreateHrpcServer(cnf, store) - if err != nil { - return nil, err - } - - lg.Infof("Created MiniHTraced %s\n", bld.Name) - return &MiniHTraced{ - Name: bld.Name, - Cnf: cnf, - DataDirs: bld.DataDirs, - Store: store, - Rsv: rsv, - Hsv: hsv, - Lg: lg, - KeepDataDirsOnClose: bld.KeepDataDirsOnClose, - }, nil -} - -// Return a Config object that clients can use to connect to this MiniHTraceD. -func (ht *MiniHTraced) ClientConf() *conf.Config { - return ht.Cnf.Clone(conf.HTRACE_WEB_ADDRESS, ht.Rsv.Addr().String(), - conf.HTRACE_HRPC_ADDRESS, ht.Hsv.Addr().String()) -} - -func (ht *MiniHTraced) Close() { - ht.Lg.Infof("Closing MiniHTraced %s\n", ht.Name) - ht.Rsv.Close() - ht.Store.Close() - if !ht.KeepDataDirsOnClose { - for idx := range ht.DataDirs { - ht.Lg.Infof("Removing %s...\n", ht.DataDirs[idx]) - os.RemoveAll(ht.DataDirs[idx]) - } - } - ht.Lg.Infof("Finished closing MiniHTraced %s\n", ht.Name) - ht.Lg.Close() -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/42b2f6a2/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go ---------------------------------------------------------------------- diff --git a/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go b/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go deleted file mode 100644 index 69b316c..0000000 --- a/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go +++ /dev/null @@ -1,308 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package main - -import ( - "bytes" - "encoding/json" - "fmt" - "github.com/gorilla/mux" - "io" - "net" - "net/http" - "org/apache/htrace/common" - "org/apache/htrace/conf" - "os" - "path/filepath" - "strconv" - "strings" -) - -// Set the response headers. -func setResponseHeaders(hdr http.Header) { - hdr.Set("Content-Type", "application/json") -} - -// Write a JSON error response. -func writeError(lg *common.Logger, w http.ResponseWriter, errCode int, - errStr string) { - str := strings.Replace(errStr, `"`, `'`, -1) - lg.Info(str + "\n") - w.WriteHeader(errCode) - w.Write([]byte(`{ "error" : "` + str + `"}`)) -} - -type serverInfoHandler struct { - lg *common.Logger -} - -func (hand *serverInfoHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - setResponseHeaders(w.Header()) - version := common.ServerInfo{ReleaseVersion: RELEASE_VERSION, - GitVersion: GIT_VERSION} - buf, err := json.Marshal(&version) - if err != nil { - writeError(hand.lg, w, http.StatusInternalServerError, - fmt.Sprintf("error marshalling ServerInfo: %s\n", err.Error())) - return - } - if hand.lg.DebugEnabled() { - hand.lg.Debugf("Returned serverInfo %s\n", string(buf)) - } - w.Write(buf) -} - -type dataStoreHandler struct { - lg *common.Logger - store *dataStore -} - -func (hand *dataStoreHandler) parseSid(w http.ResponseWriter, - str string) (common.SpanId, bool) { - val, err := strconv.ParseUint(str, 16, 64) - if err != nil { - writeError(hand.lg, w, http.StatusBadRequest, - fmt.Sprintf("Failed to parse span ID %s: %s", str, err.Error())) - w.Write([]byte("Error parsing : " + err.Error())) - return 0, false - } - return common.SpanId(val), true -} - -func (hand *dataStoreHandler) getReqField32(fieldName string, w http.ResponseWriter, - req *http.Request) (int32, bool) { - str := req.FormValue(fieldName) - if str == "" { - writeError(hand.lg, w, http.StatusBadRequest, fmt.Sprintf("No %s specified.", fieldName)) - return -1, false - } - val, err := strconv.ParseUint(str, 16, 32) - if err != nil { - writeError(hand.lg, w, http.StatusBadRequest, - fmt.Sprintf("Error parsing %s: %s.", fieldName, err.Error())) - return -1, false - } - return int32(val), true -} - -type findSidHandler struct { - dataStoreHandler -} - -func (hand *findSidHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - setResponseHeaders(w.Header()) - req.ParseForm() - vars := mux.Vars(req) - stringSid := vars["id"] - sid, ok := hand.parseSid(w, stringSid) - if !ok { - return - } - hand.lg.Debugf("findSidHandler(sid=%s)\n", sid.String()) - span := hand.store.FindSpan(sid) - if span == nil { - writeError(hand.lg, w, http.StatusNoContent, - fmt.Sprintf("No such span as %s\n", sid.String())) - return - } - w.Write(span.ToJson()) -} - -type findChildrenHandler struct { - dataStoreHandler -} - -func (hand *findChildrenHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - setResponseHeaders(w.Header()) - req.ParseForm() - vars := mux.Vars(req) - stringSid := vars["id"] - sid, ok := hand.parseSid(w, stringSid) - if !ok { - return - } - var lim int32 - lim, ok = hand.getReqField32("lim", w, req) - if !ok { - return - } - hand.lg.Debugf("findChildrenHandler(sid=%s, lim=%d)\n", sid.String(), lim) - children := hand.store.FindChildren(sid, lim) - jbytes, err := json.Marshal(children) - if err != nil { - writeError(hand.lg, w, http.StatusInternalServerError, - fmt.Sprintf("Error marshalling children: %s", err.Error())) - return - } - w.Write(jbytes) -} - -type writeSpansHandler struct { - dataStoreHandler -} - -func (hand *writeSpansHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - setResponseHeaders(w.Header()) - dec := json.NewDecoder(req.Body) - spans := make([]*common.Span, 0, 32) - defaultPid := req.Header.Get("htrace-pid") - for { - var span common.Span - err := dec.Decode(&span) - if err != nil { - if err != io.EOF { - writeError(hand.lg, w, http.StatusBadRequest, - fmt.Sprintf("Error parsing spans: %s", err.Error())) - return - } - break - } - if span.ProcessId == "" { - span.ProcessId = defaultPid - } - spans = append(spans, &span) - } - hand.lg.Debugf("writeSpansHandler: received %d span(s). defaultPid = %s\n", - len(spans), defaultPid) - for spanIdx := range spans { - if hand.lg.DebugEnabled() { - hand.lg.Debugf("writing span %s\n", spans[spanIdx].ToJson()) - } - hand.store.WriteSpan(spans[spanIdx]) - } -} - -type queryHandler struct { - lg *common.Logger - dataStoreHandler -} - -func (hand *queryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - setResponseHeaders(w.Header()) - queryString := req.FormValue("query") - if queryString == "" { - writeError(hand.lg, w, http.StatusBadRequest, "No query provided.\n") - return - } - var query common.Query - reader := bytes.NewBufferString(queryString) - dec := json.NewDecoder(reader) - err := dec.Decode(&query) - if err != nil { - writeError(hand.lg, w, http.StatusBadRequest, - fmt.Sprintf("Error parsing query: %s", err.Error())) - return - } - var results []*common.Span - results, err = hand.store.HandleQuery(&query) - if err != nil { - writeError(hand.lg, w, http.StatusInternalServerError, - fmt.Sprintf("Internal error processing query %s: %s", - query.String(), err.Error())) - return - } - var jbytes []byte - jbytes, err = json.Marshal(results) - if err != nil { - writeError(hand.lg, w, http.StatusInternalServerError, - fmt.Sprintf("Error marshalling results: %s", err.Error())) - return - } - w.Write(jbytes) -} - -type logErrorHandler struct { - lg *common.Logger -} - -func (hand *logErrorHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { - hand.lg.Errorf("Got unknown request %s\n", req.RequestURI) - writeError(hand.lg, w, http.StatusBadRequest, "Unknown request.") -} - -type RestServer struct { - listener net.Listener - lg *common.Logger -} - -func CreateRestServer(cnf *conf.Config, store *dataStore) (*RestServer, error) { - var err error - rsv := &RestServer{} - rsv.listener, err = net.Listen("tcp", cnf.Get(conf.HTRACE_WEB_ADDRESS)) - if err != nil { - return nil, err - } - var success bool - defer func() { - if !success { - rsv.Close() - } - }() - rsv.lg = common.NewLogger("rest", cnf) - - r := mux.NewRouter().StrictSlash(false) - - r.Handle("/server/info", &serverInfoHandler{lg: rsv.lg}).Methods("GET") - - writeSpansH := &writeSpansHandler{dataStoreHandler: dataStoreHandler{ - store: store, lg: rsv.lg}} - r.Handle("/writeSpans", writeSpansH).Methods("POST") - - queryH := &queryHandler{lg: rsv.lg, dataStoreHandler: dataStoreHandler{store: store}} - r.Handle("/query", queryH).Methods("GET") - - span := r.PathPrefix("/span").Subrouter() - findSidH := &findSidHandler{dataStoreHandler: dataStoreHandler{store: store, lg: rsv.lg}} - span.Handle("/{id}", findSidH).Methods("GET") - - findChildrenH := &findChildrenHandler{dataStoreHandler: dataStoreHandler{store: store, - lg: rsv.lg}} - span.Handle("/{id}/children", findChildrenH).Methods("GET") - - // Default Handler. This will serve requests for static requests. - webdir := os.Getenv("HTRACED_WEB_DIR") - if webdir == "" { - webdir, err = filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "..", "..", "web")) - - if err != nil { - return nil, err - } - } - - rsv.lg.Infof("Serving static files from %s\n.", webdir) - r.PathPrefix("/").Handler(http.FileServer(http.Dir(webdir))).Methods("GET") - - // Log an error message for unknown non-GET requests. - r.PathPrefix("/").Handler(&logErrorHandler{lg: rsv.lg}) - - go http.Serve(rsv.listener, r) - - rsv.lg.Infof("Started REST server on %s...\n", rsv.listener.Addr().String()) - success = true - return rsv, nil -} - -func (rsv *RestServer) Addr() net.Addr { - return rsv.listener.Addr() -} - -func (rsv *RestServer) Close() { - rsv.listener.Close() -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/42b2f6a2/htrace-htraced/src/go/src/org/apache/htrace/test/random.go ---------------------------------------------------------------------- diff --git a/htrace-htraced/src/go/src/org/apache/htrace/test/random.go b/htrace-htraced/src/go/src/org/apache/htrace/test/random.go deleted file mode 100644 index d10e2f9..0000000 --- a/htrace-htraced/src/go/src/org/apache/htrace/test/random.go +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package test - -import ( - "fmt" - "math/rand" - "org/apache/htrace/common" -) - -func NonZeroRand64(rnd *rand.Rand) int64 { - for { - r := rnd.Int63() - if r == 0 { - continue - } - if rnd.Intn(1) != 0 { - return -r - } - return r - } -} - -func NonZeroRand32(rnd *rand.Rand) int32 { - for { - r := rnd.Int31() - if r == 0 { - continue - } - if rnd.Intn(1) != 0 { - return -r - } - return r - } -} - -// Create a random span. -func NewRandomSpan(rnd *rand.Rand, potentialParents []*common.Span) *common.Span { - parents := []common.SpanId{} - if potentialParents != nil { - parentIdx := rnd.Intn(len(potentialParents) + 1) - if parentIdx < len(potentialParents) { - parents = []common.SpanId{potentialParents[parentIdx].Id} - } - } - return &common.Span{Id: common.SpanId(NonZeroRand64(rnd)), - SpanData: common.SpanData{ - Begin: NonZeroRand64(rnd), - End: NonZeroRand64(rnd), - Description: "getFileDescriptors", - TraceId: common.SpanId(NonZeroRand64(rnd)), - Parents: parents, - ProcessId: fmt.Sprintf("process%d", NonZeroRand32(rnd)), - }} -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/42b2f6a2/htrace-htraced/src/go/src/org/apache/htrace/test/util.go ---------------------------------------------------------------------- diff --git a/htrace-htraced/src/go/src/org/apache/htrace/test/util.go b/htrace-htraced/src/go/src/org/apache/htrace/test/util.go deleted file mode 100644 index cc058e0..0000000 --- a/htrace-htraced/src/go/src/org/apache/htrace/test/util.go +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package test - -import ( - "org/apache/htrace/common" -) - -func SpanId(str string) common.SpanId { - var spanId common.SpanId - err := spanId.FromString(str) - if err != nil { - panic(err.Error()) - } - return spanId -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/42b2f6a2/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 83c38d2..c1a8d25 100644 --- a/pom.xml +++ b/pom.xml @@ -159,8 +159,8 @@ language governing permissions and limitations under the License. --> <exclude>*/generated/*</exclude> <exclude>.git/**</exclude> <exclue>**/README.md</exclue> - <exclude>**/src/go/bin/*</exclude> - <exclude>**/src/go/pkg/*</exclude> + <exclude>**/go/bin/*</exclude> + <exclude>**/go/pkg/*</exclude> <exclude>style.txt</exclude> <!-- external projects --> <exclude>**/bootstrap-3.3.1/**</exclude>
