github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/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 pkg 16 17 import ( 18 "context" 19 20 "github.com/alibaba/ilogtail/pkg/config" 21 "github.com/alibaba/ilogtail/pkg/util" 22 ) 23 24 const LogTailMeta LogtailMetaKey = "LogtailContextMeta" 25 26 // LogtailContextMeta is used to store metadata in Logtail context and would be 27 // propagated within context.Context. 28 type LogtailContextMeta struct { 29 project string 30 logstore string 31 configName string 32 loggerHeader string 33 alarm *util.Alarm 34 } 35 36 type LogtailMetaKey string 37 38 // NewLogtailContextMeta create a LogtailContextMeta instance. 39 func NewLogtailContextMeta(project, logstore, configName string) (context.Context, *LogtailContextMeta) { 40 meta := &LogtailContextMeta{ 41 project: project, 42 logstore: logstore, 43 configName: config.GetRealConfigName(configName), 44 alarm: new(util.Alarm), 45 } 46 if len(logstore) == 0 { 47 meta.loggerHeader = "[" + configName + "]\t" 48 } else { 49 meta.loggerHeader = "[" + configName + "," + logstore + "]\t" 50 } 51 meta.alarm.Init(project, logstore) 52 ctx := context.WithValue(context.Background(), LogTailMeta, meta) 53 return ctx, meta 54 } 55 56 // NewLogtailContextMetaWithoutAlarm create a LogtailContextMeta without alarm instance. 57 func NewLogtailContextMetaWithoutAlarm(project, logstore, configName string) (context.Context, *LogtailContextMeta) { 58 meta := &LogtailContextMeta{ 59 project: project, 60 logstore: logstore, 61 configName: configName, 62 } 63 if len(logstore) == 0 { 64 meta.loggerHeader = "[" + configName + "]\t" 65 } else { 66 meta.loggerHeader = "[" + configName + "," + logstore + "]\t" 67 } 68 ctx := context.WithValue(context.Background(), LogTailMeta, meta) 69 return ctx, meta 70 } 71 72 func (c *LogtailContextMeta) LoggerHeader() string { 73 return c.loggerHeader 74 } 75 76 func (c *LogtailContextMeta) GetProject() string { 77 return c.project 78 } 79 80 func (c *LogtailContextMeta) GetLogStore() string { 81 return c.logstore 82 } 83 func (c *LogtailContextMeta) GetConfigName() string { 84 return c.configName 85 } 86 87 func (c *LogtailContextMeta) GetAlarm() *util.Alarm { 88 return c.alarm 89 } 90 91 func (c *LogtailContextMeta) RecordAlarm(alarmType, msg string) { 92 if c.alarm == nil { 93 return 94 } 95 c.alarm.Record(alarmType, msg) 96 }