@@ -18,10 +18,25 @@ import (
1818)
1919
2020const (
21- licensePath = "/_license"
22- licensePathV7 = "/_license?accept_enterprise"
21+ clusterSettingsPath = "/_cluster/settings?filter_path=**.cluster.metadata.display_name"
22+ licensePath = "/_license"
23+ licensePathV7 = "/_license?accept_enterprise"
2324)
2425
26+ type clusterSettingsResponse struct {
27+ Persistent clusterSettingsDisplayName `json:"persistent"`
28+ Transient clusterSettingsDisplayName `json:"transient"`
29+ }
30+
31+ // Partial structure to extract the cluster display name from the cluster settings API response.
32+ type clusterSettingsDisplayName struct {
33+ Cluster struct {
34+ Metadata struct {
35+ DisplayName string `json:"display_name"`
36+ } `json:"metadata"`
37+ } `json:"cluster"`
38+ }
39+
2540// License information returned from `GET /_license`, contained within the "license" object.
2641type license struct {
2742 UID string `json:"uid"`
@@ -75,20 +90,38 @@ func maybeRegisterCloudConnectedCluster(m *elasticsearch.MetricSet, getClusterIn
7590 cloudApiKey , err := getCloudConnectedModeApiKey (m )
7691
7792 if err != nil {
78- return fmt .Errorf ("failed to get Cloud Connected Mode API key : %w" , err )
93+ return fmt .Errorf ("failed to get Cloud Connected API Key : %w" , err )
7994 } else if cloudApiKey == "" {
8095 // if there is no Cloud API Key configured, then there is no request to make
8196 return nil
8297 }
8398
84- m .Logger ().Debugf ("Attempting to get cluster info for Cloud Connected Mode ..." )
99+ m .Logger ().Debugf ("Attempting to get cluster info for Cloud Connected..." )
85100 clusterInfo , err := getClusterInfo (m )
86101
87102 if err != nil {
88103 return fmt .Errorf ("failed to load cluster info: %w" , err )
89104 }
90105
91- m .Logger ().Debugf ("Attempting to fetch license for Cloud Connected Mode..." )
106+ m .Logger ().Debugf ("Attempting to fetch cluster settings for Cloud Connected..." )
107+ clusterSettings , err := utils .FetchAPIData [clusterSettingsResponse ](m , clusterSettingsPath )
108+
109+ if err != nil {
110+ // Log the error, but do not fail the registration - we can continue with the existing cluster name
111+ // Note: You can restart the agent to re-fetch the name later if needed
112+ m .Logger ().Warnf ("Ignoring failure to fetch cluster settings for Cloud Connected: %v" , err )
113+ // prefer transient setting over persistent setting
114+ } else if clusterSettings .Transient .Cluster .Metadata .DisplayName != "" {
115+ m .Logger ().Debugf ("Using transient cluster display name %s for Cloud Connected in place of cluster name %s" , clusterSettings .Transient .Cluster .Metadata .DisplayName , clusterInfo .ClusterName )
116+
117+ clusterInfo .ClusterName = clusterSettings .Transient .Cluster .Metadata .DisplayName
118+ } else if clusterSettings .Persistent .Cluster .Metadata .DisplayName != "" {
119+ m .Logger ().Debugf ("Using persistent cluster display name %s for Cloud Connected in place of cluster name %s" , clusterSettings .Persistent .Cluster .Metadata .DisplayName , clusterInfo .ClusterName )
120+
121+ clusterInfo .ClusterName = clusterSettings .Persistent .Cluster .Metadata .DisplayName
122+ }
123+
124+ m .Logger ().Debugf ("Attempting to fetch license for Cloud Connected..." )
92125 versionedLicensePath := licensePath
93126
94127 // TODO: Drop support for this when we drop support for 7.17
@@ -107,7 +140,7 @@ func maybeRegisterCloudConnectedCluster(m *elasticsearch.MetricSet, getClusterIn
107140 return fmt .Errorf ("cluster license type is not supported: %s" , licenseWrapper .License .Type )
108141 }
109142
110- m .Logger ().Debugf ("Successfully fetched license for Cloud Connected Mode : UUID=%s License=%s" , clusterInfo .ClusterID , licenseWrapper .License .UID )
143+ m .Logger ().Debugf ("Successfully fetched license for Cloud Connected: UUID=%s License=%s" , clusterInfo .ClusterID , licenseWrapper .License .UID )
111144
112145 return registerCloudConnectedCluster (cloudApiKey , clusterInfo , & licenseWrapper .License , m .Logger ())
113146}
@@ -128,14 +161,14 @@ func registerCloudConnectedCluster(cloudApiKey string, clusterInfo *utils.Cluste
128161 })
129162
130163 if err != nil {
131- return fmt .Errorf ("failed to serialize payload for Cloud Connected Mode : %w" , err )
164+ return fmt .Errorf ("failed to serialize payload for Cloud Connected: %w" , err )
132165 }
133166
134167 requestURL := getCloudConnectedModeAPIURL () + "/api/v1/cloud-connected/clusters"
135168 req , err := http .NewRequestWithContext (context .Background (), "POST" , requestURL , bytes .NewBuffer (jsonData ))
136169
137170 if err != nil {
138- return fmt .Errorf ("failed to create HTTP request for Cloud Connected Mode : %w" , err )
171+ return fmt .Errorf ("failed to create HTTP request for Cloud Connected: %w" , err )
139172 }
140173
141174 req .Header .Set ("Accept" , "application/json" )
@@ -146,9 +179,9 @@ func registerCloudConnectedCluster(cloudApiKey string, clusterInfo *utils.Cluste
146179
147180 if err == nil {
148181 utils .SetResourceID (data .ID )
149- logger .Infof ("Registered cluster %s for Cloud Connected Mode : %s" , clusterInfo .ClusterID , data .ID )
182+ logger .Infof ("Registered cluster %s for Cloud Connected: %s" , clusterInfo .ClusterID , data .ID )
150183 return nil
151184 }
152185
153- return fmt .Errorf ("failed to register for Cloud Connected Mode : %w" , err )
186+ return fmt .Errorf ("failed to register for Cloud Connected: %w" , err )
154187}
0 commit comments