k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/experiment/logviewer/gcsreader/parsing.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"bufio"
    21  	"io"
    22  	"regexp"
    23  	"strings"
    24  	"time"
    25  
    26  	"k8s.io/klog/v2"
    27  )
    28  
    29  func processLines(reader io.Reader, regex *regexp.Regexp) ([]*logEntry, error) {
    30  	var result []*logEntry
    31  	r := bufio.NewReader(reader)
    32  	for {
    33  		line, err := r.ReadBytes('\n')
    34  		if err != nil {
    35  			if err != io.EOF {
    36  				return nil, err
    37  			}
    38  			if len(line) == 0 {
    39  				break
    40  			}
    41  		}
    42  		matched, entry, err := processLine(line, regex)
    43  		if err != nil {
    44  			// TODO There is a problem that files finish with incomplete line
    45  			klog.Errorf("%s error parsing line %s", err, line)
    46  			continue
    47  		}
    48  		if matched {
    49  			result = append(result, entry)
    50  		}
    51  	}
    52  	return result, nil
    53  }
    54  
    55  func processLine(line []byte, regex *regexp.Regexp) (bool, *logEntry, error) {
    56  	if !regex.Match(line) {
    57  		return false, nil, nil
    58  	}
    59  
    60  	parsed, err := parseLine(string(line))
    61  	return true, parsed, err
    62  }
    63  
    64  func parseLine(line string) (*logEntry, error) {
    65  	const startMarker = "ReceivedTimestamp\":\""
    66  	const endMarker = "\",\"stageTimestamp"
    67  	start := strings.Index(line, startMarker)
    68  	end := strings.Index(line, endMarker)
    69  	if start == -1 {
    70  		return &logEntry{}, &parseLineFailedError{line}
    71  	}
    72  	if end == -1 {
    73  		return &logEntry{}, &parseLineFailedError{line}
    74  	}
    75  	timestamp := line[(start + len(startMarker)):end]
    76  	parsed, e := time.Parse(time.RFC3339Nano, timestamp)
    77  	if e != nil {
    78  		return nil, e
    79  	}
    80  	return &logEntry{log: line, time: parsed}, nil
    81  }
    82  
    83  func (e *parseLineFailedError) Error() string {
    84  	return "Failed to parse line: " + e.line
    85  }
    86  
    87  type parseLineFailedError struct {
    88  	line string
    89  }
    90  
    91  type logEntry struct {
    92  	log  string
    93  	time time.Time
    94  }