github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/local_context.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 "context" 19 "encoding/json" 20 "sync" 21 22 "github.com/alibaba/ilogtail/pkg" 23 "github.com/alibaba/ilogtail/pkg/config" 24 "github.com/alibaba/ilogtail/pkg/logger" 25 "github.com/alibaba/ilogtail/pkg/pipeline" 26 "github.com/alibaba/ilogtail/pkg/selfmonitor" 27 "github.com/alibaba/ilogtail/pkg/util" 28 ) 29 30 type LocalContext struct { 31 MetricsRecords []*selfmonitor.MetricsRecord 32 logstoreConfigMetricRecord *selfmonitor.MetricsRecord 33 34 AllCheckPoint map[string][]byte 35 36 ctx context.Context 37 pluginNames string 38 common *pkg.LogtailContextMeta 39 } 40 41 var contextMutex sync.RWMutex 42 43 func (p *LocalContext) GetConfigName() string { 44 return p.common.GetConfigName() 45 } 46 func (p *LocalContext) GetProject() string { 47 return p.common.GetProject() 48 } 49 func (p *LocalContext) GetLogstore() string { 50 return p.common.GetLogStore() 51 } 52 53 func (p *LocalContext) GetPipelineScopeConfig() *config.GlobalConfig { 54 return &config.GlobalConfig{} 55 } 56 57 func (p *LocalContext) AddPlugin(name string) { 58 if len(p.pluginNames) != 0 { 59 p.pluginNames += "," + name 60 } else { 61 p.pluginNames = name 62 } 63 } 64 65 func (p *LocalContext) InitContext(project, logstore, configName string) { 66 // bind metadata information. 67 p.ctx, p.common = pkg.NewLogtailContextMetaWithoutAlarm(project, logstore, configName) 68 p.AllCheckPoint = make(map[string][]byte) 69 } 70 71 func (p *LocalContext) GetRuntimeContext() context.Context { 72 return p.ctx 73 } 74 75 func (p *LocalContext) GetExtension(name string, cfg any) (pipeline.Extension, error) { 76 return nil, nil 77 } 78 79 func (p *LocalContext) RegisterMetricRecord(labels []selfmonitor.LabelPair) *selfmonitor.MetricsRecord { 80 contextMutex.Lock() 81 defer contextMutex.Unlock() 82 83 metricsRecord := &selfmonitor.MetricsRecord{Labels: labels} 84 85 p.MetricsRecords = append(p.MetricsRecords, metricsRecord) 86 return metricsRecord 87 } 88 89 func (p *LocalContext) RegisterLogstoreConfigMetricRecord(labels []selfmonitor.LabelPair) *selfmonitor.MetricsRecord { 90 p.logstoreConfigMetricRecord = &selfmonitor.MetricsRecord{ 91 Labels: labels, 92 } 93 return p.logstoreConfigMetricRecord 94 } 95 96 func (p *LocalContext) GetLogstoreConfigMetricRecord() *selfmonitor.MetricsRecord { 97 return p.logstoreConfigMetricRecord 98 } 99 100 func (p *LocalContext) GetMetricRecord() *selfmonitor.MetricsRecord { 101 contextMutex.RLock() 102 if len(p.MetricsRecords) > 0 { 103 defer contextMutex.RUnlock() 104 return p.MetricsRecords[len(p.MetricsRecords)-1] 105 } 106 contextMutex.RUnlock() 107 return p.RegisterMetricRecord(nil) 108 } 109 110 // ExportMetricRecords is used for exporting metrics records. 111 // Each metric is a map[string]string 112 func (p *LocalContext) ExportMetricRecords() []map[string]string { 113 contextMutex.RLock() 114 defer contextMutex.RUnlock() 115 116 records := make([]map[string]string, 0) 117 for _, metricsRecord := range p.MetricsRecords { 118 records = append(records, metricsRecord.ExportMetricRecords()) 119 } 120 return records 121 } 122 123 func (p *LocalContext) SaveCheckPoint(key string, value []byte) error { 124 logger.Debug(p.ctx, "save checkpoint, key", key, "value", string(value)) 125 p.AllCheckPoint[key] = value 126 return nil 127 } 128 129 func (p *LocalContext) GetCheckPoint(key string) (value []byte, exist bool) { 130 value, exist = p.AllCheckPoint[key] 131 logger.Debug(p.ctx, "get checkpoint, key", key, "value", string(value)) 132 return value, exist 133 } 134 135 func (p *LocalContext) SaveCheckPointObject(key string, obj interface{}) error { 136 val, err := json.Marshal(obj) 137 if err != nil { 138 logger.Debug(p.ctx, "CHECKPOINT_INVALID_ALARM", "save checkpoint error, invalid checkpoint, key", key, "val", util.CutString(string(val), 1024), "error", err) 139 return err 140 } 141 return p.SaveCheckPoint(key, val) 142 } 143 144 func (p *LocalContext) GetCheckPointObject(key string, obj interface{}) (exist bool) { 145 val, ok := p.GetCheckPoint(key) 146 if !ok { 147 return false 148 } 149 err := json.Unmarshal(val, obj) 150 if err != nil { 151 logger.Debug(p.ctx, "CHECKPOINT_INVALID_ALARM", "get checkpoint error, invalid checkpoint, key", key, "val", util.CutString(string(val), 1024), "error", err) 152 return false 153 } 154 return true 155 }