// Function used to add a specific dynamic metric namespace element // into the dimensions field of final Heka message structure func addToDimensions(f *message.Field, fName string) (*message.Field, error) { // If the dimension field does not exists yet, create ti if f == nil { field, err := message.NewField("dimensions", fName, "") if err != nil { logger.WithField("_block", "addToDimensions").Error(err) return nil, err } return field, nil } // Add field name to dimension field f.AddValue(fName) return f, nil }
func lookup_field(msg *message.Message, fn string, fi int, ai int) (int, unsafe.Pointer, int) { var field *message.Field if fi != 0 { fields := msg.FindAllFields(fn) if fi >= len(fields) { return 0, unsafe.Pointer(nil), 0 } field = fields[fi] } else { if field = msg.FindFirstField(fn); field == nil { return 0, unsafe.Pointer(nil), 0 } } fieldType := int(field.GetValueType()) switch field.GetValueType() { case message.Field_STRING: if ai >= len(field.ValueString) { return fieldType, unsafe.Pointer(nil), 0 } value := field.ValueString[ai] cs := C.CString(value) // freed by the caller return fieldType, unsafe.Pointer(cs), len(value) case message.Field_BYTES: if ai >= len(field.ValueBytes) { return fieldType, unsafe.Pointer(nil), 0 } value := field.ValueBytes[ai] return fieldType, unsafe.Pointer(&field.ValueBytes[ai][0]), len(value) case message.Field_INTEGER: if ai >= len(field.ValueInteger) { return fieldType, unsafe.Pointer(nil), 0 } return fieldType, unsafe.Pointer(&field.ValueInteger[ai]), 0 case message.Field_DOUBLE: if ai >= len(field.ValueDouble) { return fieldType, unsafe.Pointer(nil), 0 } return fieldType, unsafe.Pointer(&field.ValueDouble[ai]), 0 case message.Field_BOOL: if ai >= len(field.ValueBool) { return fieldType, unsafe.Pointer(nil), 0 } return fieldType, unsafe.Pointer(&field.ValueBool[ai]), 0 } return 0, unsafe.Pointer(nil), 0 }
func getFieldAsString(msg *message.Message, mvar *messageVariable) string { var field *message.Field if mvar.fi != 0 { fields := msg.FindAllFields(mvar.name) if mvar.fi >= len(fields) { return "" } field = fields[mvar.fi] } else { if field = msg.FindFirstField(mvar.name); field == nil { return "" } } switch field.GetValueType() { case message.Field_STRING: if mvar.ai >= len(field.ValueString) { return "" } return field.ValueString[mvar.ai] case message.Field_BYTES: if mvar.ai >= len(field.ValueBytes) { return "" } return string(field.ValueBytes[mvar.ai]) case message.Field_INTEGER: if mvar.ai >= len(field.ValueInteger) { return "" } return fmt.Sprintf("%d", field.ValueInteger[mvar.ai]) case message.Field_DOUBLE: if mvar.ai >= len(field.ValueDouble) { return "" } return fmt.Sprintf("%g", field.ValueDouble[mvar.ai]) case message.Field_BOOL: if mvar.ai >= len(field.ValueBool) { return "" } return fmt.Sprintf("%t", field.ValueBool[mvar.ai]) } return "" }
func writeField(first bool, b *bytes.Buffer, f *message.Field, raw bool) { if !first { b.WriteString(`,`) } writeQuotedString(b, f.GetName()) b.WriteString(`:`) switch f.GetValueType() { case message.Field_STRING: values := f.GetValueString() if len(values) > 1 { b.WriteString(`[`) for i, value := range values { if raw { b.WriteString(value) } else { writeQuotedString(b, value) } if i < len(values)-1 { b.WriteString(`,`) } } b.WriteString(`]`) } else { if raw { b.WriteString(values[0]) } else { writeQuotedString(b, values[0]) } } case message.Field_BYTES: values := f.GetValueBytes() if len(values) > 1 { b.WriteString(`[`) for i, value := range values { if raw { b.WriteString(string(value)) } else { writeQuotedString(b, base64.StdEncoding.EncodeToString(value)) } if i < len(values)-1 { b.WriteString(`,`) } } b.WriteString(`]`) } else { if raw { b.WriteString(string(values[0])) } else { writeQuotedString(b, string(values[0])) } } case message.Field_INTEGER: values := f.GetValueInteger() if len(values) > 1 { b.WriteString(`[`) for i, value := range values { b.WriteString(strconv.FormatInt(value, 10)) if i < len(values)-1 { b.WriteString(`,`) } } b.WriteString(`]`) } else { b.WriteString(strconv.FormatInt(values[0], 10)) } case message.Field_DOUBLE: values := f.GetValueDouble() if len(values) > 1 { b.WriteString(`[`) for i, value := range values { b.WriteString(strconv.FormatFloat(value, 'g', -1, 64)) if i < len(values)-1 { b.WriteString(`,`) } } b.WriteString(`]`) } else { b.WriteString(strconv.FormatFloat(values[0], 'g', -1, 64)) } case message.Field_BOOL: values := f.GetValueBool() if len(values) > 1 { b.WriteString(`[`) for i, value := range values { b.WriteString(strconv.FormatBool(value)) if i < len(values)-1 { b.WriteString(`,`) } } b.WriteString(`]`) } else { b.WriteString(strconv.FormatBool(values[0])) } } }