public long extractTimestampMillis(String topic, final byte[] bytes) throws IOException { if (timestampFieldPath != null) { com.google.protobuf.Message decodedMessage = protobufUtil.decodeMessage(topic, bytes); int i = 0; for (; i < timestampFieldPath.length - 1; ++i) { decodedMessage = (com.google.protobuf.Message) decodedMessage.getField( decodedMessage.getDescriptorForType().findFieldByName(timestampFieldPath[i])); } Object timestampObject = decodedMessage.getField( decodedMessage.getDescriptorForType().findFieldByName(timestampFieldPath[i])); if (timestampObject instanceof com.google.protobuf.Timestamp) { return Timestamps.toMillis((com.google.protobuf.Timestamp) timestampObject); } else { return toMillis((Long) timestampObject); } } else { // Assume that the timestamp field is the first field, is required, // and is a uint64. CodedInputStream input = CodedInputStream.newInstance(bytes); // Don't really care about the tag, but need to read it to get, to // the payload. input.readTag(); return toMillis(input.readUInt64()); } }
public ProtobufMessageParser(SecorConfig config) { super(config); protobufUtil = new ProtobufUtil(config); if (protobufUtil.isConfigured()) { String timestampFieldName = mConfig.getMessageTimestampName(); String timestampFieldSeparator = mConfig.getMessageTimestampNameSeparator(); if (timestampFieldSeparator == null || timestampFieldSeparator.isEmpty()) { timestampFieldSeparator = "."; } LOG.info( "Using protobuf timestamp field path: {} with separator: {}", timestampFieldName, timestampFieldSeparator); timestampFieldPath = timestampFieldName.split(Pattern.quote(timestampFieldSeparator)); } else { LOG.info( "Protobuf message class is not configured, will assume that timestamp is the first uint64 field"); } }