本文整理匯總了Golang中github.com/elastic/beats/libbeat/common.MapStr類的典型用法代碼示例。如果您正苦於以下問題:Golang MapStr類的具體用法?Golang MapStr怎麽用?Golang MapStr使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了MapStr類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Run
func (f decodeJSONFields) Run(event common.MapStr) (common.MapStr, error) {
var errs []string
for _, field := range f.fields {
data, err := event.GetValue(field)
if err != nil && errors.Cause(err) != common.ErrKeyNotFound {
debug("Error trying to GetValue for field : %s in event : %v", field, event)
errs = append(errs, err.Error())
continue
}
text, ok := data.(string)
if ok {
var output interface{}
err := unmarshal(f.maxDepth, []byte(text), &output, f.processArray)
if err != nil {
debug("Error trying to unmarshal %s", event[field])
errs = append(errs, err.Error())
continue
}
_, err = event.Put(field, output)
if err != nil {
debug("Error trying to Put value %v for field : %s", output, field)
errs = append(errs, err.Error())
continue
}
}
}
if len(errs) > 0 {
return event, fmt.Errorf(strings.Join(errs, ", "))
}
return event, nil
}
示例2: TestValidJSONDepthTwo
func TestValidJSONDepthTwo(t *testing.T) {
input := common.MapStr{
"msg": "{\"log\":\"{\\\"level\\\":\\\"info\\\"}\",\"stream\":\"stderr\",\"count\":3}",
"pipeline": "us1",
}
testConfig, _ = common.NewConfigFrom(map[string]interface{}{
"fields": fields,
"processArray": false,
"maxDepth": 2,
})
actual := getActualValue(t, testConfig, input)
expected := common.MapStr{
"msg": map[string]interface{}{
"log": map[string]interface{}{
"level": "info",
},
"stream": "stderr",
"count": 3,
},
"pipeline": "us1",
}
assert.Equal(t, expected.String(), actual.String())
}
示例3: checkEquals
func (c *Condition) checkEquals(event common.MapStr) bool {
for field, equalValue := range c.equals {
value, err := event.GetValue(field)
if err != nil {
return false
}
intValue, err := extractInt(value)
if err == nil {
if intValue != equalValue.Int {
return false
}
} else {
sValue, err := extractString(value)
if err != nil {
logp.Warn("unexpected type %T in equals condition as it accepts only integers and strings. ", value)
return false
}
if sValue != equalValue.Str {
return false
}
}
}
return true
}
示例4: CheckRegexp
func (c *Condition) CheckRegexp(event common.MapStr) bool {
for field, equalValue := range c.Regexp {
value, err := event.GetValue(field)
if err != nil {
logp.Debug("filter", "unavailable field %s: %v", field, err)
return false
}
switch value.(type) {
case string:
if !equalValue.MatchString(value.(string)) {
return false
}
default:
logp.Warn("unexpected type %T in regexp condition as it accepts only strings. ", value)
return false
}
}
return true
}
示例5: checkContains
func (c *Condition) checkContains(event common.MapStr) bool {
outer:
for field, equalValue := range c.contains {
value, err := event.GetValue(field)
if err != nil {
return false
}
switch value.(type) {
case string:
if !strings.Contains(value.(string), equalValue) {
return false
}
case *string:
if !strings.Contains(*value.(*string), equalValue) {
return false
}
case []string:
for _, s := range value.([]string) {
if strings.Contains(s, equalValue) {
continue outer
}
}
return false
default:
logp.Warn("unexpected type %T in contains condition as it accepts only strings.", value)
return false
}
}
return true
}
示例6: MakePingAllIPPortFactory
func MakePingAllIPPortFactory(
fields common.MapStr,
ports []uint16,
f func(*net.IPAddr, uint16) (common.MapStr, error),
) func(*net.IPAddr) TaskRunner {
if len(ports) == 1 {
port := ports[0]
fields := fields.Clone()
fields["port"] = strconv.Itoa(int(port))
return MakePingIPFactory(fields, func(ip *net.IPAddr) (common.MapStr, error) {
return f(ip, port)
})
}
return MakePingAllIPFactory(fields, func(ip *net.IPAddr) []func() (common.MapStr, error) {
funcs := make([]func() (common.MapStr, error), len(ports))
for i := range ports {
port := ports[i]
funcs[i] = func() (common.MapStr, error) {
event, err := f(ip, port)
if event == nil {
event = common.MapStr{}
}
event["port"] = strconv.Itoa(int(port))
return event, err
}
}
return funcs
})
}
示例7: CheckEquals
func (c *Condition) CheckEquals(event common.MapStr) bool {
for field, equalValue := range c.Equals {
value, err := event.GetValue(field)
if err != nil {
logp.Debug("filter", "unavailable field %s: %v", field, err)
return false
}
switch value.(type) {
case uint8, uint16, uint32, uint64, int8, int16, int32, int64, int, uint:
return value == equalValue.Int
case string:
return value == equalValue.Str
default:
logp.Warn("unexpected type %T in equals condition as it accepts only integers and strings. ", value)
return false
}
}
return true
}
示例8: processGroups
func processGroups(groups []string, topic string, pids map[int32]int64) []common.MapStr {
var events []common.MapStr
for _, group := range groups {
pid_offsets, err := getConsumerOffsets(group, topic, pids)
if err == nil {
for pid, offset := range pid_offsets {
event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": "consumer",
"partition": pid,
"topic": topic,
"group": group,
"offset": offset,
}
size, ok := pids[pid]
if ok {
event.Update(common.MapStr{"lag": size - offset})
}
events = append(events, event)
}
} else {
logp.Debug("kafkabeat", "No offsets for group %s on topic %s", group, topic)
}
}
return events
}
示例9: fieldString
// TODO: move to libbeat/common?
func fieldString(event common.MapStr, field string) (string, error) {
type stringer interface {
String() string
}
v, err := event.GetValue(field)
if err != nil {
return "", err
}
switch s := v.(type) {
case string:
return s, nil
case []byte:
return string(s), nil
case stringer:
return s.String(), nil
case int8, int16, int32, int64, int:
i := reflect.ValueOf(s).Int()
return strconv.FormatInt(i, 10), nil
case uint8, uint16, uint32, uint64, uint:
u := reflect.ValueOf(s).Uint()
return strconv.FormatUint(u, 10), nil
case float32:
return strconv.FormatFloat(float64(s), 'g', -1, 32), nil
case float64:
return strconv.FormatFloat(s, 'g', -1, 64), nil
default:
logp.Warn("Can not convert key '%v' value to string", v)
return "", errConvertString
}
}
示例10: newTCPMonitorHostJob
func newTCPMonitorHostJob(
scheme, host string, port uint16,
tls *transport.TLSConfig,
config *Config,
) (monitors.Job, error) {
typ := config.Name
timeout := config.Timeout
jobName := jobName(typ, jobType(scheme), host, []uint16{port})
validator := makeValidateConn(config)
pingAddr := net.JoinHostPort(host, strconv.Itoa(int(port)))
taskDialer, err := buildDialerChain(scheme, tls, config)
if err != nil {
return nil, err
}
return monitors.MakeSimpleJob(jobName, typ, func() (common.MapStr, error) {
event := common.MapStr{
"scheme": scheme,
"port": port,
"host": host,
}
dialer, err := taskDialer.BuildWithMeasures(event)
if err != nil {
return event, err
}
results, err := pingHost(dialer, pingAddr, timeout, validator)
event.Update(results)
return event, err
}), nil
}
示例11: filterEvent
// filterEvent validates an event for common required fields with types.
// If event is to be filtered out the reason is returned as error.
func filterEvent(event common.MapStr) error {
ts, ok := event["@timestamp"]
if !ok {
return errors.New("Missing '@timestamp' field from event")
}
_, ok = ts.(common.Time)
if !ok {
return errors.New("Invalid '@timestamp' field from event.")
}
err := event.EnsureCountField()
if err != nil {
return err
}
t, ok := event["type"]
if !ok {
return errors.New("Missing 'type' field from event.")
}
_, ok = t.(string)
if !ok {
return errors.New("Invalid 'type' field from event.")
}
return nil
}
示例12: Run
func (f dropFields) Run(event common.MapStr) (common.MapStr, error) {
for _, field := range f.Fields {
err := event.Delete(field)
if err != nil {
return event, fmt.Errorf("Fail to delete key %s: %s", field, err)
}
}
return event, nil
}
示例13: Run
func (p addCloudMetadata) Run(event common.MapStr) (common.MapStr, error) {
if len(p.metadata) == 0 {
return event, nil
}
// This overwrites the meta.cloud if it exists. But the cloud key should be
// reserved for this processor so this should happen.
_, err := event.Put("meta.cloud", p.metadata)
return event, err
}
示例14: DeDotLabels
// DeDotLabels returns a new common.MapStr containing a copy of the labels
// where the dots in each label name have been changed to an underscore.
func DeDotLabels(labels map[string]string) common.MapStr {
outputLabels := common.MapStr{}
for k, v := range labels {
// This is necessary so that ES does not interpret '.' fields as new
// nested JSON objects, and also makes this compatible with ES 2.x.
label := strings.Replace(k, ".", "_", -1)
outputLabels.Put(label, v)
}
return outputLabels
}
示例15: fieldString
// TODO: move to libbeat/common?
func fieldString(event common.MapStr, field string) (string, error) {
v, err := event.GetValue(field)
if err != nil {
return "", err
}
s, err := tryConvString(v)
if err != nil {
logp.Warn("Can not convert key '%v' value to string", v)
}
return s, err
}