Example #1
0
// eventToPbEvent translates a raidman.Event into a lowlevel protocol Event.
func eventToPbEvent(event *Event) (*proto.Event, error) {
	var e proto.Event

	h := event.Host
	if event.Host == "" {
		h, _ = os.Hostname()
	}
	e.Host = &h

	if event.State != "" {
		e.State = &event.State
	}
	if event.Service != "" {
		e.Service = &event.Service
	}
	if event.Description != "" {
		e.Description = &event.Description
	}
	if event.Ttl != 0 {
		e.Ttl = &event.Ttl
	}
	if event.Time != 0 {
		e.Time = &event.Time
	}
	if len(event.Tags) > 0 {
		e.Tags = event.Tags
	}

	err := assignEventMetric(&e, event.Metric)
	if err != nil {
		return nil, err
	}

	attrs := make([]*proto.Attribute, len(event.Attributes))
	i := 0
	for k, v := range event.Attributes {
		switch x := v.(type) {
		case string:
			w := v.(string)
			attrs[i] = &proto.Attribute{&k, &w, nil}
		case bool:
			if x {
				attrs[i].Key = &k
			}
		default:
			return nil, fmt.Errorf("Attribute %v has invalid type (type %T)", k, v)
		}
		i++
	}

	if i > 0 {
		e.Attributes = attrs
	}

	return &e, nil
}
Example #2
0
// assignEventMetric updates the "Metric" fields of the underlying protobuf Event type based on the type of value provided
func assignEventMetric(e *proto.Event, v interface{}) error {
	switch x := v.(type) {
	case nil:
		// do nothing; an event without a metric is legitimate
	case int:
		i := int64(x)
		e.MetricSint64 = &i
	case int32:
		i := int64(x)
		e.MetricSint64 = &i
	case int64:
		e.MetricSint64 = &x
	case float32:
		e.MetricF = &x
	case float64:
		e.MetricD = &x
	default:
		return fmt.Errorf("Metric of invalid type (type %T)", v)
	}
	return nil
}