Repository: incubator-htrace
Updated Branches:
  refs/heads/master f0182729e -> df0927283


HTRACE-91. Add usage to htraced, fix htrace usage and support HTRACED_CONF_DIR 
(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/df092728
Tree: http://git-wip-us.apache.org/repos/asf/incubator-htrace/tree/df092728
Diff: http://git-wip-us.apache.org/repos/asf/incubator-htrace/diff/df092728

Branch: refs/heads/master
Commit: df09272838553293865110fb9c90a897374195ac
Parents: f018272
Author: Colin P. Mccabe <[email protected]>
Authored: Tue Jan 27 20:30:09 2015 -0800
Committer: Colin P. Mccabe <[email protected]>
Committed: Tue Jan 27 22:58:30 2015 -0800

----------------------------------------------------------------------
 .../src/go/src/org/apache/htrace/conf/config.go | 17 +++++++---
 .../src/org/apache/htrace/conf/config_keys.go   |  6 +++-
 .../src/org/apache/htrace/conf/config_test.go   | 15 +++++++++
 .../src/go/src/org/apache/htrace/htrace/cmd.go  | 33 +++++++++++++-------
 .../go/src/org/apache/htrace/htraced/htraced.go | 33 +++++++++++++++++++-
 5 files changed, 86 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/df092728/htrace-core/src/go/src/org/apache/htrace/conf/config.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/conf/config.go 
b/htrace-core/src/go/src/org/apache/htrace/conf/config.go
index d905322..4453c44 100644
--- a/htrace-core/src/go/src/org/apache/htrace/conf/config.go
+++ b/htrace-core/src/go/src/org/apache/htrace/conf/config.go
@@ -25,6 +25,7 @@ import (
        "io"
        "log"
        "os"
+       "path/filepath"
        "strconv"
        "strings"
        "syscall"
@@ -65,10 +66,19 @@ type Builder struct {
        Argv []string
 }
 
+func getHTracedConfDirs() []string {
+       confDir := os.Getenv("HTRACED_CONF_DIR")
+       paths := filepath.SplitList(confDir)
+       if len(paths) < 1 {
+               return []string{"."}
+       }
+       return paths
+}
+
 // Load a configuration from the application's argv, configuration file, and 
the standard
 // defaults.
-func LoadApplicationConfig(values map[string]string) *Config {
-       reader, err := openFile(CONFIG_FILE_NAME, []string{"."})
+func LoadApplicationConfig() *Config {
+       reader, err := openFile(CONFIG_FILE_NAME, getHTracedConfDirs())
        if err != nil {
                log.Fatal("Error opening config file: " + err.Error())
        }
@@ -79,9 +89,6 @@ func LoadApplicationConfig(values map[string]string) *Config {
        }
        bld.Argv = os.Args[1:]
        bld.Defaults = DEFAULTS
-       if values != nil {
-               bld.Values = values
-       }
        var cnf *Config
        cnf, err = bld.Build()
        if err != nil {

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/df092728/htrace-core/src/go/src/org/apache/htrace/conf/config_keys.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/conf/config_keys.go 
b/htrace-core/src/go/src/org/apache/htrace/conf/config_keys.go
index 5e359f7..b22e312 100644
--- a/htrace-core/src/go/src/org/apache/htrace/conf/config_keys.go
+++ b/htrace-core/src/go/src/org/apache/htrace/conf/config_keys.go
@@ -35,7 +35,11 @@ var PATH_SEP string = fmt.Sprintf("%c", os.PathSeparator)
 var PATH_LIST_SEP string = fmt.Sprintf("%c", os.PathListSeparator)
 
 // The name of the XML configuration file to look for.
-const CONFIG_FILE_NAME = "htraced.xml"
+const CONFIG_FILE_NAME = "htraced-conf.xml"
+
+// An environment variable containing a list of paths to search for the
+// configuration file in.
+const HTRACED_CONF_DIR = "HTRACED_CONF_DIR"
 
 // The web address to start the REST server on.
 const HTRACE_WEB_ADDRESS = "web.address"

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/df092728/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go 
b/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
index b16abb4..1e94ed7 100644
--- a/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
+++ b/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
@@ -20,6 +20,7 @@
 package conf
 
 import (
+       "os"
        "strings"
        "testing"
 )
@@ -119,3 +120,17 @@ func TestXmlConfigurationFile(t *testing.T) {
                t.Fatal()
        }
 }
+
+// Test our handling of the HTRACE_CONF_DIR environment variable.
+func TestGetHTracedConfDirs(t *testing.T) {
+       os.Setenv("HTRACED_CONF_DIR", "")
+       dirs := getHTracedConfDirs()
+       if len(dirs) != 1 || dirs[0] != "." {
+               t.Fatal()
+       }
+       os.Setenv("HTRACED_CONF_DIR", "/foo/bar:/baz")
+       dirs = getHTracedConfDirs()
+       if len(dirs) != 2 || dirs[0] != "/foo/bar" || dirs[1] != "/baz" {
+               t.Fatal()
+       }
+}

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/df092728/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 8539914..f1e765f 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
@@ -39,20 +39,31 @@ const EXIT_FAILURE = 1
 
 var verbose *bool
 
+const USAGE = `The Apache HTrace command-line tool.  This tool retrieves and 
modifies settings and
+other data on a running htraced daemon.
+
+If we find an ` + conf.CONFIG_FILE_NAME + ` configuration file in the list of 
directories
+specified in ` + conf.HTRACED_CONF_DIR + `, we will use that configuration; 
otherwise, 
+the defaults will be used.
+`
+
 func main() {
+       // Load htraced configuration
+       cnf := conf.LoadApplicationConfig()
+
        // Parse argv
-       app := kingpin.New("htrace", "The HTrace tracing utility.")
-       addr := app.Flag("addr", "Server address.").
-               Default(conf.DEFAULTS[conf.HTRACE_WEB_ADDRESS]).TCP()
+       app := kingpin.New(os.Args[0], USAGE)
+       app.Flag("Dmy.key", "Set configuration key 'my.key' to 'my.value'.  
Replace 'my.key' "+
+               "with any key you want to set.").Default("my.value").String()
+       addr := app.Flag("addr", "Server address.").String()
        verbose = app.Flag("verbose", "Verbose.").Default("false").Bool()
        version := app.Command("version", "Print the version of this program.")
        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().Uint64()
+       findSpanId := findSpan.Arg("id", "Span ID to find. Example: 
0x123456789abcdef").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().Uint64()
+       parentSpanId := findChildren.Arg("id", "Span ID to print children for. 
Example: 0x123456789abcdef").
+               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.").String()
@@ -60,10 +71,10 @@ func main() {
                "A file containing JSON span data to write to the 
server.").String()
        cmd := kingpin.MustParse(app.Parse(os.Args[1:]))
 
-       // Load htraced configuration
-       values := make(map[string]string)
-       values[conf.HTRACE_WEB_ADDRESS] = (*addr).String()
-       cnf := conf.LoadApplicationConfig(values)
+       // Add the command-line settings into the configuration.
+       if *addr != "" {
+               cnf = cnf.Clone(conf.HTRACE_WEB_ADDRESS, *addr)
+       }
 
        // Create HTrace client
        hcl, err := htrace.NewClient(cnf)

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/df092728/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go 
b/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
index 4694789..d2cbafc 100644
--- a/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
+++ b/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
@@ -20,17 +20,48 @@
 package main
 
 import (
+       "fmt"
        "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() {
-       cnf := conf.LoadApplicationConfig(nil)
+       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 := conf.LoadApplicationConfig()
        lg := common.NewLogger("main", cnf)
        defer lg.Close()
        store, err := CreateDataStore(cnf, nil)

Reply via email to