github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/local_collector.go (about)

     1  // Copyright 2021 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package helper
    16  
    17  import (
    18  	"github.com/alibaba/ilogtail/pkg/protocol"
    19  
    20  	"time"
    21  )
    22  
    23  // LocalCollector for unit test
    24  type LocalCollector struct {
    25  	Logs []*protocol.Log
    26  }
    27  
    28  func (p *LocalCollector) AddData(tags map[string]string, fields map[string]string, t ...time.Time) {
    29  	p.AddDataWithContext(tags, fields, nil, t...)
    30  }
    31  
    32  func (p *LocalCollector) AddDataArray(tags map[string]string,
    33  	columns []string,
    34  	values []string,
    35  	t ...time.Time) {
    36  	p.AddDataArrayWithContext(tags, columns, values, nil, t...)
    37  }
    38  
    39  func (p *LocalCollector) AddRawLog(log *protocol.Log) {
    40  	p.AddRawLogWithContext(log, nil)
    41  }
    42  
    43  func (p *LocalCollector) AddDataWithContext(tags map[string]string, fields map[string]string, ctx map[string]interface{}, t ...time.Time) {
    44  	// log.Printf("Begin add %v %v", tags, fields)
    45  	var logTime time.Time
    46  	if len(t) == 0 {
    47  		logTime = time.Now()
    48  	} else {
    49  		logTime = t[0]
    50  	}
    51  	slsLog, _ := CreateLog(logTime, len(t) != 0, nil, tags, fields)
    52  	p.Logs = append(p.Logs, slsLog)
    53  }
    54  
    55  func (p *LocalCollector) AddDataArrayWithContext(tags map[string]string,
    56  	columns []string,
    57  	values []string,
    58  	ctx map[string]interface{},
    59  	t ...time.Time) {
    60  	var logTime time.Time
    61  	if len(t) == 0 {
    62  		logTime = time.Now()
    63  	} else {
    64  		logTime = t[0]
    65  	}
    66  	slsLog, _ := CreateLogByArray(logTime, len(t) != 0, nil, tags, columns, values)
    67  	p.Logs = append(p.Logs, slsLog)
    68  }
    69  
    70  func (p *LocalCollector) AddRawLogWithContext(log *protocol.Log, ctx map[string]interface{}) {
    71  	p.Logs = append(p.Logs, log)
    72  }