GithubHelp home page GithubHelp logo

Comments (5)

daniel-chapa-nike avatar daniel-chapa-nike commented on September 27, 2024

+1 on getting template_name instead of template_id

from anka-prometheus-exporter.

Esysc avatar Esysc commented on September 27, 2024

I'm quite new to golang, so I apologize for the quality of code. Actually I've developed something working in my environment.
The idea is to map the templates uuid with their names and then add it to the metric.

diff --git a/src/client/communicator.go b/src/client/communicator.go
index 3fe83a3..b66d6eb 100644
--- a/src/client/communicator.go
+++ b/src/client/communicator.go
@@ -64,7 +64,16 @@ func (this *Communicator) GetVmsData() (interface{}, error) {
        if err != nil {
                return nil, fmt.Errorf("getting vms data error: %s", err)
        }
-       return d, nil
+       s, err := this.GetRegistryVms()
+       if err != nil {
+               return nil, fmt.Errorf("getting vms registry data error: %s", err)
+       }
+       m := d.([]types.Instance)
+       for k, v := range m {
+               i := v.Vm.TemplateUUID
+               m[k].Vm.TemplateNAME = s[i]
+       }
+       return m, nil
 }
 
 func (this *Communicator) GetRegistryData() (interface{}, error) {
@@ -77,6 +86,21 @@ func (this *Communicator) GetRegistryData() (interface{}, error) {
        return d, nil
 }
 
+func (this *Communicator) GetRegistryVms() (map[string]string, error) {
+       endpoint := "/api/v1/registry/vm"
+       resp := &types.RegistryVmResponse{}
+       d, err := this.getData(endpoint, resp)
+       if err != nil {
+               return nil, fmt.Errorf("getting registry vms error: %s", err)
+       }
+       m := d.([]types.Vms)
+       VmsData := make(map[string]string)
+       for _, v := range m {
+               VmsData[v.VmID] = v.TemplateNAME
+       }
+       return VmsData, nil
+}
+
 func (this *Communicator) getData(endpoint string, repsObject types.Response) (interface{}, error) {
        r, err := this.getResponse(endpoint)
        if err != nil {
diff --git a/src/metrics/instance_state_per.go b/src/metrics/instance_state_per.go
index d43c8d3..be61d7a 100644
--- a/src/metrics/instance_state_per.go
+++ b/src/metrics/instance_state_per.go
@ -32,14 +32,16 @@ func (this InstanceStatePerMetric) GetEventHandler() func(interface{}) error {
 var ankaInstanceStatePerMetrics = []InstanceStatePerMetric{
        InstanceStatePerMetric{
                BaseAnkaMetric: BaseAnkaMetric{
-                       metric: CreateGaugeMetricVec("anka_instance_state_per_template_count", "Count of Instances in a particular state, per Template (label: state, template_name)", []string{"state", "template_uuid"}),
+                       metric: CreateGaugeMetricVec("anka_instance_state_per_template_count", "Count of Instances in a particular state, per Template (label: state, template_uuid, template_name)", []string{"state", "template_uuid", "template_name"}),
                        event:  events.EVENT_VM_DATA_UPDATED,
                },
                HandleData: func(instances []types.Instance, metric *prometheus.GaugeVec) {
                        var InstanceStatePerTemplateCountMap = map[string]map[string]int{}
                        var instanceTemplates []string
+                       var instanceTemplatesMap = map[string]string{}
                        for _, instance := range instances {
                                instanceTemplates = append(instanceTemplates, instance.Vm.TemplateUUID)
+                               instanceTemplatesMap[instance.Vm.TemplateUUID] = instance.Vm.TemplateNAME
                        }
                        instanceTemplates = uniqueThisStringArray(instanceTemplates)
                        for _, wantedState := range types.InstanceStates {
@@ -63,7 +65,7 @@ var ankaInstanceStatePerMetrics = []InstanceStatePerMetric{
                        checkAndHandleResetOfGuageVecMetric((len(instances) + len(instanceTemplates)), "anka_instance_state_per_template_count", metric)
                        for wantedState, wantedStateMap := range InstanceStatePerTemplateCountMap {
                                for wantedTemplateUUID, count := range wantedStateMap {
-                                       metric.With(prometheus.Labels{"state": wantedState, "template_uuid": wantedTemplateUUID}).Set(float64(count))
+                                       metric.With(prometheus.Labels{"state": wantedState, "template_uuid": wantedTemplateUUID, "template_name": instanceTemplatesMap[wantedTemplateUUID]}).Set(float64(count))
                                }
                        }
                },
@@ -154,4 +156,4 @@ func init() { // runs on exporter init only (updates are made with the above Eve
        for _, instanceStatePerMetric := range ankaInstanceStatePerMetrics {
                AddMetric(instanceStatePerMetric)
        }
-}
+}
\ No newline at end of file
diff --git a/src/types/types.go b/src/types/types.go
index 8c254ed..b90a27c 100644
--- a/src/types/types.go
+++ b/src/types/types.go
@@ -57,10 +57,19 @@ type Instance struct {
 type VmData struct {
        State        string `json:"instance_state"`
        TemplateUUID string `json:"vmid"`
+       TemplateNAME string `json:"name"` 
        GroupUUID    string `json:"group_id"`
        NodeUUID     string `json:"node_id"`
 }
 
+type Vms struct {
+       TemplateNAME   string `json:"name"`
+       VmID               string `json:"id"`
+       Size           int64  `json:"size"`
+}
+
+type TemplateMap interface{}
+
 type Response interface {
        GetStatus() string
        GetMessage() string
@@ -98,6 +107,16 @@ func (this *RegistryResponse) GetBody() interface{} {
        return this.Body
 }
 
+type RegistryVmResponse struct {
+       DefaultResponse
+       Body []Vms `json:"body,omtiempty"`
+}
+
+func (this *RegistryVmResponse) GetBody() interface{} {
+       return this.Body
+}
+
+
 type InstancesResponse struct {
        DefaultResponse
        Body []Instance `json:"body,omtiempty"`

@NorseGaud , I can review the code and propose a PR.

from anka-prometheus-exporter.

NorseGaud avatar NorseGaud commented on September 27, 2024

@Esysc Regarding a PR: please do!

from anka-prometheus-exporter.

Esysc avatar Esysc commented on September 27, 2024

I had not so much time to do earlier. Here it is: #12 !

from anka-prometheus-exporter.

NorseGaud avatar NorseGaud commented on September 27, 2024

Thanks! This is great! It works wonderfully. https://github.com/veertuinc/anka-prometheus-exporter/releases/tag/v2.1.4

from anka-prometheus-exporter.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.