github.com/Kong/go-pdk@v0.11.0/node/node.go (about)

     1  /*
     2  Node-level utilities
     3  */
     4  package node
     5  
     6  import (
     7  	"github.com/Kong/go-pdk/bridge"
     8  	"github.com/Kong/go-pdk/server/kong_plugin_protocol"
     9  )
    10  
    11  // Holds this module's functions.  Accessible as `kong.Node`
    12  type Node struct {
    13  	bridge.PdkBridge
    14  }
    15  
    16  type workerLuaVmStats struct {
    17  	HttpAllocatedGc int64 `json:"http_allocated_gc"`
    18  	Pid             int64 `json:"pid"`
    19  }
    20  
    21  type MemoryStats struct {
    22  	LuaSharedDicts struct {
    23  		Kong struct {
    24  			AllocatedSlabs int64 `json:"allocated_slabs"`
    25  			Capacity       int64 `json:"capacity"`
    26  		} `json:"kong"`
    27  		KongDbCache struct {
    28  			AllocatedSlabs int64 `json:"allocated_slabs"`
    29  			Capacity       int64 `json:"capacity"`
    30  		} `json:"kong_db_cache"`
    31  	} `json:"lua_shared_dicts"`
    32  	WorkersLuaVms []workerLuaVmStats `json:"workers_lua_vms"`
    33  }
    34  
    35  // kong.Node.GetId() returns the v4 UUID used by this node to describe itself.
    36  func (n Node) GetId() (string, error) {
    37  	return n.AskString(`kong.node.get_id`, nil)
    38  }
    39  
    40  // kong.Node.GetMemoryStats() returns memory usage statistics about this node.
    41  func (n Node) GetMemoryStats() (MemoryStats, error) {
    42  	out := new(kong_plugin_protocol.MemoryStats)
    43  	err := n.Ask(`kong.node.get_memory_stats`, nil, out)
    44  	if err != nil {
    45  		return MemoryStats{}, err
    46  	}
    47  
    48  	ms := MemoryStats{}
    49  	ms.LuaSharedDicts.Kong.AllocatedSlabs = out.LuaSharedDicts.Kong.AllocatedSlabs
    50  	ms.LuaSharedDicts.Kong.Capacity = out.LuaSharedDicts.Kong.Capacity
    51  	ms.LuaSharedDicts.KongDbCache.AllocatedSlabs = out.LuaSharedDicts.KongDbCache.AllocatedSlabs
    52  	ms.LuaSharedDicts.KongDbCache.Capacity = out.LuaSharedDicts.KongDbCache.Capacity
    53  
    54  	ms.WorkersLuaVms = make([]workerLuaVmStats, len(out.WorkersLuaVms))
    55  	for i, wlv := range out.WorkersLuaVms {
    56  		ms.WorkersLuaVms[i] = workerLuaVmStats{
    57  			HttpAllocatedGc: wlv.HttpAllocatedGc,
    58  			Pid:             wlv.Pid,
    59  		}
    60  	}
    61  
    62  	return ms, nil
    63  }
    64