// Configure initializes this producer with values from a plugin config. func (prod *Scribe) Configure(conf core.PluginConfig) error { err := prod.ProducerBase.Configure(conf) if err != nil { return err } host := conf.GetString("Address", "localhost:1463") bufferSizeMax := conf.GetInt("BatchSizeMaxKB", 8<<10) << 1 // 8 MB prod.category = make(map[core.MessageStreamID]string, 0) prod.batchSize = conf.GetInt("BatchSizeByte", 8192) prod.batchTimeout = time.Duration(conf.GetInt("BatchTimeoutSec", 5)) * time.Second prod.batch = createScribeMessageBatch(bufferSizeMax) prod.bufferSizeKB = conf.GetInt("ConnectionBufferSizeKB", 1<<10) // 1 MB prod.category = conf.GetStreamMap("Category", "") // Initialize scribe connection prod.socket, err = thrift.NewTSocket(host) if err != nil { Log.Error.Print("Scribe socket error:", err) return err } prod.transport = thrift.NewTFramedTransport(prod.socket) binProtocol := thrift.NewTBinaryProtocol(prod.transport, false, false) prod.scribe = scribe.NewScribeClientProtocol(prod.transport, binProtocol, binProtocol) return nil }
// Configure initializes this producer with values from a plugin config. func (prod *Scribe) Configure(conf core.PluginConfig) error { err := prod.ProducerBase.Configure(conf) if err != nil { return err } prod.SetStopCallback(prod.close) host := conf.GetString("Address", "localhost:1463") prod.batchMaxCount = conf.GetInt("BatchMaxCount", 8192) prod.windowSize = prod.batchMaxCount prod.batchFlushCount = conf.GetInt("BatchFlushCount", prod.batchMaxCount/2) prod.batchFlushCount = shared.MinI(prod.batchFlushCount, prod.batchMaxCount) prod.batchTimeout = time.Duration(conf.GetInt("BatchTimeoutSec", 5)) * time.Second prod.batch = core.NewMessageBatch(prod.batchMaxCount) prod.bufferSizeByte = conf.GetInt("ConnectionBufferSizeKB", 1<<10) << 10 // 1 MB prod.category = conf.GetStreamMap("Category", "") // Initialize scribe connection prod.socket, err = thrift.NewTSocket(host) if err != nil { Log.Error.Print("Scribe socket error:", err) return err } prod.transport = thrift.NewTFramedTransport(prod.socket) binProtocol := thrift.NewTBinaryProtocol(prod.transport, false, false) prod.scribe = scribe.NewScribeClientProtocol(prod.transport, binProtocol, binProtocol) prod.lastMetricUpdate = time.Now() prod.counters = make(map[string]*int64) shared.Metric.New(scribeMetricWindowSize) shared.Metric.SetI(scribeMetricWindowSize, prod.windowSize) for _, category := range prod.category { shared.Metric.New(scribeMetricMessages + category) shared.Metric.New(scribeMetricMessagesSec + category) prod.counters[category] = new(int64) } prod.SetCheckFuseCallback(prod.tryOpenConnection) return nil }
func WriteScribe(category string, message string) { // currently available on linux platform if runtime.GOOS != "linux" { Log(category + " : " + message) return } entry := scribe.NewLogEntry() entry.Category = category entry.Message = message messages := []*scribe.LogEntry{entry} socket, err := thrift.NewTSocket("localhost:1463") CheckError(err) transport := thrift.NewTFramedTransport(socket) protocol := thrift.NewTBinaryProtocol(transport, false, false) client := scribe.NewScribeClientProtocol(transport, protocol, protocol) transport.Open() result, err := client.Log(messages) CheckError(err) transport.Close() Log(result.String()) }
func main() { flag.Usage = Usage var host string var port int var protocol string var urlString string var framed bool var useHttp bool var parsedUrl url.URL var trans thrift.TTransport _ = math.MinInt32 // will become unneeded eventually _ = strconv.Atoi flag.Usage = Usage flag.StringVar(&host, "h", "localhost", "Specify host and port") flag.IntVar(&port, "p", 9090, "Specify port") flag.StringVar(&protocol, "P", "binary", "Specify the protocol (binary, compact, simplejson, json)") flag.StringVar(&urlString, "u", "", "Specify the url") flag.BoolVar(&framed, "framed", false, "Use framed transport") flag.BoolVar(&useHttp, "http", false, "Use http") flag.Parse() if len(urlString) > 0 { parsedUrl, err := url.Parse(urlString) if err != nil { fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) flag.Usage() } host = parsedUrl.Host useHttp = len(parsedUrl.Scheme) <= 0 || parsedUrl.Scheme == "http" } else if useHttp { _, err := url.Parse(fmt.Sprint("http://", host, ":", port)) if err != nil { fmt.Fprintln(os.Stderr, "Error parsing URL: ", err) flag.Usage() } } cmd := flag.Arg(0) var err error if useHttp { trans, err = thrift.NewTHttpClient(parsedUrl.String()) } else { portStr := fmt.Sprint(port) if strings.Contains(host, ":") { host, portStr, err = net.SplitHostPort(host) if err != nil { fmt.Fprintln(os.Stderr, "error with host:", err) os.Exit(1) } } trans, err = thrift.NewTSocket(net.JoinHostPort(host, portStr)) if err != nil { fmt.Fprintln(os.Stderr, "error resolving address:", err) os.Exit(1) } if framed { trans = thrift.NewTFramedTransport(trans) } } if err != nil { fmt.Fprintln(os.Stderr, "Error creating transport", err) os.Exit(1) } defer trans.Close() var protocolFactory thrift.TProtocolFactory switch protocol { case "compact": protocolFactory = thrift.NewTCompactProtocolFactory() break case "simplejson": protocolFactory = thrift.NewTSimpleJSONProtocolFactory() break case "json": protocolFactory = thrift.NewTJSONProtocolFactory() break case "binary", "": protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() break default: fmt.Fprintln(os.Stderr, "Invalid protocol specified: ", protocol) Usage() os.Exit(1) } client := fb303.NewFacebookServiceClientFactory(trans, protocolFactory) if err := trans.Open(); err != nil { fmt.Fprintln(os.Stderr, "Error opening socket to ", host, ":", port, " ", err) os.Exit(1) } switch cmd { case "getName": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "GetName requires 0 args") flag.Usage() } fmt.Print(client.GetName()) fmt.Print("\n") break case "getVersion": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "GetVersion requires 0 args") flag.Usage() } fmt.Print(client.GetVersion()) fmt.Print("\n") break case "getStatus": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "GetStatus requires 0 args") flag.Usage() } fmt.Print(client.GetStatus()) fmt.Print("\n") break case "getStatusDetails": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "GetStatusDetails requires 0 args") flag.Usage() } fmt.Print(client.GetStatusDetails()) fmt.Print("\n") break case "getCounters": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "GetCounters requires 0 args") flag.Usage() } fmt.Print(client.GetCounters()) fmt.Print("\n") break case "getCounter": if flag.NArg()-1 != 1 { fmt.Fprintln(os.Stderr, "GetCounter requires 1 args") flag.Usage() } argvalue0 := flag.Arg(1) value0 := argvalue0 fmt.Print(client.GetCounter(value0)) fmt.Print("\n") break case "setOption": if flag.NArg()-1 != 2 { fmt.Fprintln(os.Stderr, "SetOption requires 2 args") flag.Usage() } argvalue0 := flag.Arg(1) value0 := argvalue0 argvalue1 := flag.Arg(2) value1 := argvalue1 fmt.Print(client.SetOption(value0, value1)) fmt.Print("\n") break case "getOption": if flag.NArg()-1 != 1 { fmt.Fprintln(os.Stderr, "GetOption requires 1 args") flag.Usage() } argvalue0 := flag.Arg(1) value0 := argvalue0 fmt.Print(client.GetOption(value0)) fmt.Print("\n") break case "getOptions": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "GetOptions requires 0 args") flag.Usage() } fmt.Print(client.GetOptions()) fmt.Print("\n") break case "getCpuProfile": if flag.NArg()-1 != 1 { fmt.Fprintln(os.Stderr, "GetCpuProfile requires 1 args") flag.Usage() } tmp0, err62 := (strconv.Atoi(flag.Arg(1))) if err62 != nil { Usage() return } argvalue0 := int32(tmp0) value0 := argvalue0 fmt.Print(client.GetCpuProfile(value0)) fmt.Print("\n") break case "aliveSince": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "AliveSince requires 0 args") flag.Usage() } fmt.Print(client.AliveSince()) fmt.Print("\n") break case "reinitialize": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "Reinitialize requires 0 args") flag.Usage() } fmt.Print(client.Reinitialize()) fmt.Print("\n") break case "shutdown": if flag.NArg()-1 != 0 { fmt.Fprintln(os.Stderr, "Shutdown requires 0 args") flag.Usage() } fmt.Print(client.Shutdown()) fmt.Print("\n") break case "": Usage() break default: fmt.Fprintln(os.Stderr, "Invalid function ", cmd) } }