github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/meta_helper.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/pipeline" 19 20 "time" 21 "unsafe" 22 ) 23 24 var metaKeys = make([]string, 0, 5) 25 26 const ( 27 defaultLen = 16 28 empty = "{}" 29 emptyArr = "[]" 30 ) 31 32 // MetaNode describes a superset of the metadata that probes can collect 33 // about a given node in a given topology, along with the edges (aka 34 // adjacency) emanating from the node. 35 type MetaNode struct { 36 ID string 37 Type string 38 Attributes Attributes 39 Labels Labels 40 Parents Parents 41 } 42 43 // Attributes used to store attributes in common conditions. 44 // 45 //easyjson:json 46 type Attributes map[string]interface{} 47 48 //easyjson:json 49 type Labels map[string]string 50 51 //easyjson:json 52 type Parents []string 53 54 func NewMetaNode(id, nodeType string) *MetaNode { 55 return &MetaNode{ 56 ID: id, 57 Type: nodeType, 58 } 59 } 60 61 func (n *MetaNode) WithLabels(labels Labels) *MetaNode { 62 n.Labels = labels 63 return n 64 } 65 66 func (n *MetaNode) WithAttributes(attributes Attributes) *MetaNode { 67 n.Attributes = attributes 68 return n 69 } 70 71 func (n *MetaNode) WithParents(parents Parents) *MetaNode { 72 n.Parents = parents 73 return n 74 } 75 76 func (n *MetaNode) WithParent(key, parentID, parentName string) *MetaNode { 77 n.Parents = append(n.Parents, key+":"+parentID+":"+parentName) 78 return n 79 } 80 81 func (n *MetaNode) WithLabel(k, v string) *MetaNode { 82 if n.Labels == nil { 83 n.Labels = make(Labels, defaultLen) 84 } 85 n.Labels[k] = v 86 return n 87 } 88 89 func (n *MetaNode) WithAttribute(k string, v interface{}) *MetaNode { 90 if n.Attributes == nil { 91 n.Attributes = make(Attributes, defaultLen) 92 } 93 n.Attributes[k] = v 94 return n 95 } 96 97 // AddMetadata to the collector. 98 func AddMetadata(collector pipeline.Collector, time time.Time, node *MetaNode) { 99 keys, vals := makeMetaLog(node) 100 collector.AddDataArray(nil, keys, vals, time) 101 } 102 103 // makeMetaLog convert MetaNode to the log of Logtail 104 // 105 //nolint:gosec 106 func makeMetaLog(node *MetaNode) (keys, values []string) { 107 values = make([]string, 5) 108 values[0] = node.ID 109 values[1] = node.Type 110 if node.Attributes != nil { 111 bytes, _ := node.Attributes.MarshalJSON() 112 values[2] = *(*string)(unsafe.Pointer(&bytes)) 113 } else { 114 values[2] = empty 115 } 116 if len(node.Labels) == 0 { 117 values[3] = empty 118 } else { 119 bytes, _ := node.Labels.MarshalJSON() 120 values[3] = *(*string)(unsafe.Pointer(&bytes)) 121 } 122 if len(node.Parents) == 0 { 123 values[4] = emptyArr 124 } else { 125 bytes, _ := node.Parents.MarshalJSON() 126 values[4] = *(*string)(unsafe.Pointer(&bytes)) 127 } 128 return metaKeys, values 129 } 130 func init() { 131 metaKeys = append(metaKeys, "id", "type", "attributes", "labels", "parents") 132 }