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

     1  // Copyright 2022 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  	"sort"
    20  	"strings"
    21  
    22  	"github.com/alibaba/ilogtail/pkg"
    23  	"github.com/alibaba/ilogtail/pkg/util"
    24  )
    25  
    26  // ManagerMeta is designed for a special input plugin to log self telemetry data, such as telegraf.
    27  // The kind of plugin would connect with other agents, so most of them have a global manager to control or connect with other agents.
    28  type ManagerMeta struct {
    29  	Metas map[string]map[string]map[string]struct{}
    30  	meta  *pkg.LogtailContextMeta
    31  	ctx   context.Context
    32  }
    33  
    34  func NewmanagerMeta(configName string) *ManagerMeta {
    35  	ctx, meta := pkg.NewLogtailContextMeta("", "", configName)
    36  	return &ManagerMeta{
    37  		Metas: make(map[string]map[string]map[string]struct{}),
    38  		ctx:   ctx,
    39  		meta:  meta,
    40  	}
    41  }
    42  
    43  func (b *ManagerMeta) Add(prj, logstore, cfg string) {
    44  	change := false
    45  	if _, ok := b.Metas[prj]; !ok {
    46  		b.Metas[prj] = make(map[string]map[string]struct{})
    47  		change = true
    48  	}
    49  	if _, ok := b.Metas[prj][logstore]; !ok {
    50  		b.Metas[prj][logstore] = make(map[string]struct{})
    51  		change = true
    52  	}
    53  	if _, ok := b.Metas[prj][logstore][cfg]; !ok {
    54  		b.Metas[prj][logstore][cfg] = struct{}{}
    55  	}
    56  	if change {
    57  		b.UpdateAlarm()
    58  	}
    59  }
    60  
    61  func (b *ManagerMeta) Delete(prj, logstore, cfg string) {
    62  	change := false
    63  	delete(b.Metas[prj][logstore], cfg)
    64  	if _, ok := b.Metas[prj][logstore]; ok && len(b.Metas[prj][logstore]) == 0 {
    65  		delete(b.Metas[prj], logstore)
    66  		change = true
    67  	}
    68  	if _, ok := b.Metas[prj]; ok && len(b.Metas[prj]) == 0 {
    69  		delete(b.Metas, prj)
    70  		change = true
    71  	}
    72  	if change {
    73  		b.UpdateAlarm()
    74  	}
    75  }
    76  
    77  func (b *ManagerMeta) UpdateAlarm() {
    78  	var prjSlice, logstoresSlice []string
    79  	for prj, logstores := range b.Metas {
    80  		for logstore := range logstores {
    81  			logstoresSlice = append(logstoresSlice, logstore)
    82  		}
    83  		prjSlice = append(prjSlice, prj)
    84  	}
    85  	sort.Strings(prjSlice)
    86  	sort.Strings(logstoresSlice)
    87  	b.meta.GetAlarm().Update(strings.Join(prjSlice, ","), strings.Join(logstoresSlice, ","))
    88  }
    89  
    90  func (b *ManagerMeta) GetAlarm() *util.Alarm {
    91  	return b.meta.GetAlarm()
    92  }
    93  
    94  func (b *ManagerMeta) GetContext() context.Context {
    95  	return b.ctx
    96  }