本文整理汇总了Golang中github.com/jeffail/gabs.Container类的典型用法代码示例。如果您正苦于以下问题:Golang Container类的具体用法?Golang Container怎么用?Golang Container使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Container类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: scrapeMetrics
func (e *Exporter) scrapeMetrics(json *gabs.Container, ch chan<- prometheus.Metric) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
switch key {
case "message":
log.Errorf("Problem collecting metrics: %s\n", element.Data().(string))
return
case "version":
data := element.Data()
version, ok := data.(string)
if !ok {
log.Errorf(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for version\n", data))
} else {
gauge, _ := e.Gauges.Fetch("metrics_version", "Marathon metrics version", "version")
gauge.WithLabelValues(version).Set(1)
gauge.Collect(ch)
}
case "counters":
e.scrapeCounters(element)
case "gauges":
e.scrapeGauges(element)
case "histograms":
e.scrapeHistograms(element)
case "meters":
e.scrapeMeters(element)
case "timers":
e.scrapeTimers(element)
}
}
}
示例2: testGetMultStatementOfGroup
func testGetMultStatementOfGroup(t *testing.T, group *gabs.Container) {
db := initDatabase(t)
defer db.Close()
mart := initHandler(db)
ids := []string{
uuid.NewV4().String(),
uuid.NewV4().String(),
uuid.NewV4().String(),
uuid.NewV4().String(),
}
stmt, err := gabs.ParseJSON([]byte(statetmentAgentBoilerplate))
fatalIfError(t, err)
stmtSlice := []*gabs.Container{
appendAccount(t, stmt),
appendMbox(t, stmt),
appendMboxSHA1(t, stmt),
appendOpenID(t, stmt),
}
for i := 0; i < len(ids); i++ {
putStatement(t, mart, stmtSlice[i].String(), ids[i])
var s interface{}
err = json.Unmarshal([]byte(stmtSlice[i].Search("actor").String()), &s)
fatalIfError(t, err)
err = group.ArrayAppendP(s, "actor.member")
fatalIfError(t, err)
}
// construct query
v := &url.Values{}
//t.Log(group.Search("actor").String())
v.Add("agent", group.Search("actor").String())
resp := getStatement(t, mart, v)
//t.Log(string(resp))
respstmt, err := gabs.ParseJSON(resp)
fatalIfError(t, err)
cnt, err := respstmt.ArrayCount("statements")
fatalIfError(t, err)
if cnt != len(ids) {
t.Fatalf("Expected %d statements in response; got %d", len(ids), cnt)
}
children, err := respstmt.S("statements").Children()
fatalIfError(t, err)
for idx, stm := range children {
if id, ok := stm.Search("id").Data().(string); !ok || id != ids[idx] {
t.Fatalf("Got invalid order of statement array")
}
}
}
示例3: saveGuestConfig
// Save guest configuration to path from JSON format.
func saveGuestConfig(config *gabs.Container, path string) (err error) {
s := config.String()
dst, err := os.Create(path)
if err != nil {
return
}
defer dst.Close()
_, err = dst.WriteString(s)
return
}
示例4: appendOpenID
func appendOpenID(t *testing.T, stmt *gabs.Container) *gabs.Container {
stmt, err := gabs.ParseJSON([]byte(stmt.String()))
fatalIfError(t, err)
domainPrefix := uuid.NewV4().String()
openID := "http://" + domainPrefix + ".openid.example.com/"
_, err = stmt.SetP(openID, "actor.openid")
fatalIfError(t, err)
return stmt
}
示例5: PublicUserString
func PublicUserString(token *gabs.Container) string {
s, err := gabs.ParseJSON([]byte(token.String()))
if err != nil {
return "{}"
}
s.SetP("", "privid")
s.SetP("", "alertaddress")
return s.String()
}
示例6: scrapeTimers
func (e *Exporter) scrapeTimers(json *gabs.Container) {
elements, _ := json.ChildrenMap()
for key, element := range elements {
new, err := e.scrapeTimer(key, element)
if err != nil {
log.Debug(err)
} else if new {
log.Infof("Added timer %q\n", key)
}
}
}
示例7: appendMbox
func appendMbox(t *testing.T, stmt *gabs.Container) *gabs.Container {
stmt, err := gabs.ParseJSON([]byte(stmt.String()))
fatalIfError(t, err)
domainPrefix := uuid.NewV4().String()
mbox := "mbox:[email protected]" + domainPrefix + ".example.com"
_, err = stmt.SetP(mbox, "actor.mbox")
fatalIfError(t, err)
return stmt
}
示例8: scrapeCounter
func (e *Exporter) scrapeCounter(key string, json *gabs.Container) (bool, error) {
data := json.Path("count").Data()
count, ok := data.(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for counter %s\n", data, key))
}
name := renameMetric(key)
help := fmt.Sprintf(counterHelp, key)
counter, new := e.Counters.Fetch(name, help)
counter.WithLabelValues().Set(count)
return new, nil
}
示例9: scrapeGauge
func (e *Exporter) scrapeGauge(key string, json *gabs.Container) (bool, error) {
data := json.Path("value").Data()
value, ok := data.(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for gauge %s\n", data, key))
}
name := renameMetric(key)
help := fmt.Sprintf(gaugeHelp, key)
gauge, new := e.Gauges.Fetch(name, help)
gauge.WithLabelValues().Set(value)
return new, nil
}
示例10: appendMboxSHA1
func appendMboxSHA1(t *testing.T, stmt *gabs.Container) *gabs.Container {
stmt, err := gabs.ParseJSON([]byte(stmt.String()))
fatalIfError(t, err)
data := make([]byte, 16)
_, err = rand.Read(data)
fatalIfError(t, err)
mboxSHA1 := fmt.Sprintf("%x", sha1.Sum(data))
_, err = stmt.SetP(mboxSHA1, "actor.mbox_sha1sum")
fatalIfError(t, err)
return stmt
}
示例11: ExtractAndCheckToken
func (svc *Service) ExtractAndCheckToken(so socketio.Socket, g *gabs.Container) (string, bool) {
if g.Path("t").Data() == nil {
so.Emit("auth_error", "Missing Token")
return "", false
}
uid, err := svc.ValidateUserToken(nil, g.Path("t").Data().(string))
if err != nil {
so.Emit("auth_error", "Invalid Token")
return "", false
}
return uid, true
}
示例12: scrapeApps
func (e *Exporter) scrapeApps(json *gabs.Container, ch chan<- prometheus.Metric) {
elements, _ := json.S("apps").Children()
states := map[string]string{
"running": "tasksRunning",
"staged": "tasksStaged",
"healthy": "tasksHealthy",
"unhealthy": "tasksUnhealthy",
"cpus": "cpus",
"mem_in_mb": "mem",
"disk_in_mb": "disk",
"gpus": "gpus",
"avg_uptime": "taskStats.startedAfterLastScaling.stats.lifeTime.averageSeconds",
}
name := "app_instances"
gauge, new := e.Gauges.Fetch(name, "Marathon app instance count", "app")
if new {
log.Infof("Added gauge %q\n", name)
}
gauge.Reset()
for _, app := range elements {
id := app.Path("id").Data().(string)
data := app.Path("instances").Data()
count, ok := data.(float64)
if !ok {
log.Debugf(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for number of app instances\n", data))
continue
}
gauge.WithLabelValues(id).Set(count)
for key, value := range states {
name := fmt.Sprintf("app_task_%s", key)
gauge, new := e.Gauges.Fetch(name, fmt.Sprintf("Marathon app task %s count", key), "app")
if new {
log.Infof("Added gauge %q\n", name)
}
data := app.Path(value).Data()
count, ok := data.(float64)
if !ok {
log.Debugf(fmt.Sprintf("Bad conversion! Unexpected value \"%v\" for number of \"%s\" tasks\n", data, key))
continue
}
gauge.WithLabelValues(id).Set(count)
}
}
}
示例13: documentFromHit
func documentFromHit(hit *gabs.Container) *Document {
return &Document{
Index: hit.Path("_index").Data().(string),
Type: hit.Path("_type").Data().(string),
Id: hit.Path("_id").Data().(string),
source: []byte(hit.Path("_source").String()),
}
}
示例14: scrapeHistogram
func (e *Exporter) scrapeHistogram(key string, json *gabs.Container) (bool, error) {
count, ok := json.Path("count").Data().(float64)
if !ok {
return false, errors.New(fmt.Sprintf("Bad historgram! %s has no count\n", key))
}
name := renameMetric(key)
help := fmt.Sprintf(histogramHelp, key)
counter, new := e.Counters.Fetch(name+"_count", help)
counter.WithLabelValues().Set(count)
percentiles, _ := e.Gauges.Fetch(name, help, "percentile")
max, _ := e.Gauges.Fetch(name+"_max", help)
mean, _ := e.Gauges.Fetch(name+"_mean", help)
min, _ := e.Gauges.Fetch(name+"_min", help)
stddev, _ := e.Gauges.Fetch(name+"_stddev", help)
properties, _ := json.ChildrenMap()
for key, property := range properties {
switch key {
case "p50", "p75", "p95", "p98", "p99", "p999":
if value, ok := property.Data().(float64); ok {
percentiles.WithLabelValues("0." + key[1:]).Set(value)
}
case "min":
if value, ok := property.Data().(float64); ok {
min.WithLabelValues().Set(value)
}
case "max":
if value, ok := property.Data().(float64); ok {
max.WithLabelValues().Set(value)
}
case "mean":
if value, ok := property.Data().(float64); ok {
mean.WithLabelValues().Set(value)
}
case "stddev":
if value, ok := property.Data().(float64); ok {
stddev.WithLabelValues().Set(value)
}
}
}
return new, nil
}
示例15: PushEvent
// TODO: Properly get commit count
func PushEvent(data *gabs.Container) {
repo, _ := data.S("repository", "full_name").Data().(string)
user, _ := data.S("pusher", "name").Data().(string)
gitio := GitioShort(data.S("head_commit", "url").Data().(string))
commits, _ := data.S("commits").Children()
cobj, _ := data.S("commits").ChildrenMap()
fmt.Printf("[!] Commit # %d\n", len(cobj))
commitlen := strconv.Itoa(len(cobj))
message <- "[" + repo + "] " + user + " pushed " + commitlen + " commits " + gitio
for _, commit := range commits {
hash := commit.S("id").Data().(string)[0:6]
msg := commit.S("message").Data().(string)
user := commit.S("author", "name").Data().(string)
message <- "[" + repo + "] " + user + " " + hash + " - " + msg
}
}