github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_global_attributes.go (about)

     1  // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gmetric
     8  
     9  import (
    10  	"sync"
    11  
    12  	"github.com/gogf/gf/v2/text/gregex"
    13  )
    14  
    15  // SetGlobalAttributesOption binds the global attributes to certain instrument.
    16  type SetGlobalAttributesOption struct {
    17  	// Instrument specifies the instrument name.
    18  	Instrument string
    19  
    20  	// Instrument specifies the instrument version.
    21  	InstrumentVersion string
    22  
    23  	// InstrumentPattern specifies instrument by regular expression on Instrument name.
    24  	// Example:
    25  	// 1. given `.+` will match all instruments.
    26  	// 2. given `github.com/gogf/gf.+` will match all goframe instruments.
    27  	InstrumentPattern string
    28  }
    29  
    30  // GetGlobalAttributesOption binds the global attributes to certain instrument.
    31  type GetGlobalAttributesOption struct {
    32  	Instrument        string // Instrument specifies the instrument name.
    33  	InstrumentVersion string // Instrument specifies the instrument version.
    34  }
    35  
    36  type globalAttributeItem struct {
    37  	Attributes
    38  	SetGlobalAttributesOption
    39  }
    40  
    41  var (
    42  	globalAttributesMu sync.Mutex
    43  	// globalAttributes stores the global attributes to a map.
    44  	globalAttributes = make([]globalAttributeItem, 0)
    45  )
    46  
    47  // SetGlobalAttributes appends global attributes according `SetGlobalAttributesOption`.
    48  // It appends global attributes to all metrics if given `SetGlobalAttributesOption` is empty.
    49  // It appends global attributes to certain instrument by given `SetGlobalAttributesOption`.
    50  func SetGlobalAttributes(attrs Attributes, option SetGlobalAttributesOption) {
    51  	globalAttributesMu.Lock()
    52  	defer globalAttributesMu.Unlock()
    53  	globalAttributes = append(
    54  		globalAttributes, globalAttributeItem{
    55  			Attributes:                attrs,
    56  			SetGlobalAttributesOption: option,
    57  		},
    58  	)
    59  }
    60  
    61  // GetGlobalAttributes retrieves and returns the global attributes by `GetGlobalAttributesOption`.
    62  // It returns the global attributes if given `GetGlobalAttributesOption` is empty.
    63  // It returns global attributes of certain instrument if `GetGlobalAttributesOption` is not empty.
    64  func GetGlobalAttributes(option GetGlobalAttributesOption) Attributes {
    65  	globalAttributesMu.Lock()
    66  	defer globalAttributesMu.Unlock()
    67  	var attributes = make(Attributes, 0)
    68  	for _, attrItem := range globalAttributes {
    69  		// instrument name.
    70  		if attrItem.InstrumentPattern != "" {
    71  			if !gregex.IsMatchString(attrItem.InstrumentPattern, option.Instrument) {
    72  				continue
    73  			}
    74  		} else {
    75  			if (attrItem.Instrument != "" || option.Instrument != "") &&
    76  				attrItem.Instrument != option.Instrument {
    77  				continue
    78  			}
    79  		}
    80  		// instrument version.
    81  		if (attrItem.InstrumentVersion != "" || option.InstrumentVersion != "") &&
    82  			attrItem.InstrumentVersion != option.InstrumentVersion {
    83  			continue
    84  		}
    85  		attributes = append(attributes, attrItem.Attributes...)
    86  	}
    87  	return attributes
    88  }