prometheus

Форк
0
110 строк · 3.3 Кб
1
// Copyright 2020 The Prometheus Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package eureka
15

16
import (
17
	"context"
18
	"encoding/xml"
19
	"fmt"
20
	"io"
21
	"net/http"
22

23
	"github.com/prometheus/common/version"
24
)
25

26
var userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
27

28
type Applications struct {
29
	VersionsDelta int           `xml:"versions__delta"`
30
	AppsHashcode  string        `xml:"apps__hashcode"`
31
	Applications  []Application `xml:"application"`
32
}
33

34
type Application struct {
35
	Name      string     `xml:"name"`
36
	Instances []Instance `xml:"instance"`
37
}
38

39
type Port struct {
40
	Port    int  `xml:",chardata"`
41
	Enabled bool `xml:"enabled,attr"`
42
}
43

44
type Instance struct {
45
	HostName                      string          `xml:"hostName"`
46
	HomePageURL                   string          `xml:"homePageUrl"`
47
	StatusPageURL                 string          `xml:"statusPageUrl"`
48
	HealthCheckURL                string          `xml:"healthCheckUrl"`
49
	App                           string          `xml:"app"`
50
	IPAddr                        string          `xml:"ipAddr"`
51
	VipAddress                    string          `xml:"vipAddress"`
52
	SecureVipAddress              string          `xml:"secureVipAddress"`
53
	Status                        string          `xml:"status"`
54
	Port                          *Port           `xml:"port"`
55
	SecurePort                    *Port           `xml:"securePort"`
56
	DataCenterInfo                *DataCenterInfo `xml:"dataCenterInfo"`
57
	Metadata                      *MetaData       `xml:"metadata"`
58
	IsCoordinatingDiscoveryServer bool            `xml:"isCoordinatingDiscoveryServer"`
59
	ActionType                    string          `xml:"actionType"`
60
	CountryID                     int             `xml:"countryId"`
61
	InstanceID                    string          `xml:"instanceId"`
62
}
63

64
type MetaData struct {
65
	Items []Tag `xml:",any"`
66
}
67

68
type Tag struct {
69
	XMLName xml.Name
70
	Content string `xml:",innerxml"`
71
}
72

73
type DataCenterInfo struct {
74
	Name     string    `xml:"name"`
75
	Class    string    `xml:"class,attr"`
76
	Metadata *MetaData `xml:"metadata"`
77
}
78

79
const appListPath string = "/apps"
80

81
func fetchApps(ctx context.Context, server string, client *http.Client) (*Applications, error) {
82
	url := fmt.Sprintf("%s%s", server, appListPath)
83

84
	request, err := http.NewRequest(http.MethodGet, url, nil)
85
	if err != nil {
86
		return nil, err
87
	}
88
	request = request.WithContext(ctx)
89
	request.Header.Add("User-Agent", userAgent)
90

91
	resp, err := client.Do(request)
92
	if err != nil {
93
		return nil, err
94
	}
95
	defer func() {
96
		io.Copy(io.Discard, resp.Body)
97
		resp.Body.Close()
98
	}()
99

100
	if resp.StatusCode/100 != 2 {
101
		return nil, fmt.Errorf("non 2xx status '%d' response during eureka service discovery", resp.StatusCode)
102
	}
103

104
	var apps Applications
105
	err = xml.NewDecoder(resp.Body).Decode(&apps)
106
	if err != nil {
107
		return nil, fmt.Errorf("%q: %w", url, err)
108
	}
109
	return &apps, nil
110
}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.