func()

in cloudwatch/cloudwatch.go [620:675]


func (output *OutputPlugin) processRecord(e *Event) ([]byte, error) {
	var err error
	e.Record, err = plugins.DecodeMap(e.Record)
	if err != nil {
		logrus.Debugf("[cloudwatch %d] Failed to decode record: %v\n", output.PluginInstanceID, e.Record)
		return nil, err
	}

	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	var data []byte

	if output.logKey != "" {
		log, err := plugins.LogKey(e.Record, output.logKey)
		if err != nil {
			return nil, err
		}

		data, err = plugins.EncodeLogKey(log)
	} else {
		data, err = json.Marshal(e.Record)
	}

	if err != nil {
		logrus.Debugf("[cloudwatch %d] Failed to marshal record: %v\nLog Key: %s\n", output.PluginInstanceID, e.Record, output.logKey)
		return nil, err
	}

	// append newline
	data = append(data, []byte("\n")...)

	if (len(data) + perEventBytes) > maximumBytesPerEvent {
		logrus.Warnf("[cloudwatch %d] Found record with %d bytes, truncating to 256KB, logGroup=%s, stream=%s\n",
			output.PluginInstanceID, len(data)+perEventBytes, e.group, e.stream)

		/*
		 * Find last byte of trailing unicode character via efficient byte scanning
		 * Avoids corrupting rune
		 *
		 * A unicode character may be composed of 1 - 4 bytes
		 *   bytes [11, 01, 00]xx xxxx: represent the first byte in a unicode character
		 *   byte 10xx xxxx: represent all bytes following the first byte.
		 *
		 * nextByte is the first byte that is truncated,
		 * so nextByte should be the start of a new unicode character in first byte format.
		 */
		nextByte := (maximumBytesPerEvent - len(truncatedSuffix) - perEventBytes)
		for (data[nextByte]&0xc0 == 0x80) && nextByte > 0 {
			nextByte--
		}

		data = data[:nextByte]
		data = append(data, []byte(truncatedSuffix)...)
	}

	return data, nil
}