2016-04-25 06:30:11 -07:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package marathon
|
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2016-04-25 06:30:11 -07:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-04-18 01:50:37 -07:00
|
|
|
"io"
|
2016-04-25 06:30:11 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
2016-09-05 05:40:28 -07:00
|
|
|
"net"
|
2016-04-25 06:30:11 -07:00
|
|
|
"net/http"
|
Marathon SD: Set port index label
The changes [1][] to Marathon service discovery to support multiple
ports mean that Prometheus now attempts to scrape all ports belonging to
a Marathon service.
You can use port definition or port mapping labels to filter out which
ports to scrape but that requires service owners to update their
Marathon configuration.
To allow for a smoother migration path, add a
`__meta_marathon_port_index` label, whose value is set to the port's
sequential index integer. For example, PORT0 has the value `0`, PORT1
has the value `1`, and so on.
This allows you to support scraping both the first available port (the
previous behaviour) in addition to ports with a `metrics` label.
For example, here's the relabel configuration we might use with
this patch:
- action: keep
source_labels: ['__meta_marathon_port_definition_label_metrics', '__meta_marathon_port_mapping_label_metrics', '__meta_marathon_port_index']
# Keep if port mapping or definition has a 'metrics' label with any
# non-empty value, or if no 'metrics' port label exists but this is the
# service's first available port
regex: ([^;]+;;[^;]+|;[^;]+;[^;]+|;;0)
This assumes that the Marathon API returns the ports in sorted order
(matching PORT0, PORT1, etc), which it appears that it does.
[1]: https://github.com/prometheus/prometheus/pull/2506
2017-06-20 07:43:36 -07:00
|
|
|
"strconv"
|
2017-03-02 00:44:20 -08:00
|
|
|
"strings"
|
2016-04-25 06:30:11 -07:00
|
|
|
"time"
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
2019-03-25 16:01:12 -07:00
|
|
|
"github.com/pkg/errors"
|
2020-08-20 05:48:26 -07:00
|
|
|
"github.com/prometheus/common/config"
|
2016-04-25 06:30:11 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2019-03-25 03:54:22 -07:00
|
|
|
|
2020-08-20 05:48:26 -07:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2019-03-25 03:54:22 -07:00
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2017-03-18 13:10:44 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2016-04-25 06:30:11 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// metaLabelPrefix is the meta prefix used for all meta labels in this discovery.
|
|
|
|
metaLabelPrefix = model.MetaLabelPrefix + "marathon_"
|
|
|
|
// appLabelPrefix is the prefix for the application labels.
|
|
|
|
appLabelPrefix = metaLabelPrefix + "app_label_"
|
|
|
|
|
|
|
|
// appLabel is used for the name of the app in Marathon.
|
|
|
|
appLabel model.LabelName = metaLabelPrefix + "app"
|
|
|
|
// imageLabel is the label that is used for the docker image running the service.
|
|
|
|
imageLabel model.LabelName = metaLabelPrefix + "image"
|
Marathon SD: Set port index label
The changes [1][] to Marathon service discovery to support multiple
ports mean that Prometheus now attempts to scrape all ports belonging to
a Marathon service.
You can use port definition or port mapping labels to filter out which
ports to scrape but that requires service owners to update their
Marathon configuration.
To allow for a smoother migration path, add a
`__meta_marathon_port_index` label, whose value is set to the port's
sequential index integer. For example, PORT0 has the value `0`, PORT1
has the value `1`, and so on.
This allows you to support scraping both the first available port (the
previous behaviour) in addition to ports with a `metrics` label.
For example, here's the relabel configuration we might use with
this patch:
- action: keep
source_labels: ['__meta_marathon_port_definition_label_metrics', '__meta_marathon_port_mapping_label_metrics', '__meta_marathon_port_index']
# Keep if port mapping or definition has a 'metrics' label with any
# non-empty value, or if no 'metrics' port label exists but this is the
# service's first available port
regex: ([^;]+;;[^;]+|;[^;]+;[^;]+|;;0)
This assumes that the Marathon API returns the ports in sorted order
(matching PORT0, PORT1, etc), which it appears that it does.
[1]: https://github.com/prometheus/prometheus/pull/2506
2017-06-20 07:43:36 -07:00
|
|
|
// portIndexLabel is the integer port index when multiple ports are defined;
|
|
|
|
// e.g. PORT1 would have a value of '1'
|
|
|
|
portIndexLabel model.LabelName = metaLabelPrefix + "port_index"
|
2016-04-25 06:30:11 -07:00
|
|
|
// taskLabel contains the mesos task name of the app instance.
|
|
|
|
taskLabel model.LabelName = metaLabelPrefix + "task"
|
2016-10-21 03:14:53 -07:00
|
|
|
|
2017-03-18 13:10:44 -07:00
|
|
|
// portMappingLabelPrefix is the prefix for the application portMappings labels.
|
|
|
|
portMappingLabelPrefix = metaLabelPrefix + "port_mapping_label_"
|
|
|
|
// portDefinitionLabelPrefix is the prefix for the application portDefinitions labels.
|
|
|
|
portDefinitionLabelPrefix = metaLabelPrefix + "port_definition_label_"
|
2016-10-21 03:14:53 -07:00
|
|
|
)
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// DefaultSDConfig is the default Marathon SD configuration.
|
|
|
|
var DefaultSDConfig = SDConfig{
|
|
|
|
RefreshInterval: model.Duration(30 * time.Second),
|
|
|
|
}
|
2016-04-25 06:30:11 -07:00
|
|
|
|
2020-08-20 05:48:26 -07:00
|
|
|
func init() {
|
|
|
|
discovery.RegisterConfig(&SDConfig{})
|
|
|
|
}
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
// SDConfig is the configuration for services running on Marathon.
|
|
|
|
type SDConfig struct {
|
2020-08-20 05:48:26 -07:00
|
|
|
Servers []string `yaml:"servers,omitempty"`
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
|
|
|
|
AuthToken config.Secret `yaml:"auth_token,omitempty"`
|
|
|
|
AuthTokenFile string `yaml:"auth_token_file,omitempty"`
|
|
|
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the Config.
|
|
|
|
func (*SDConfig) Name() string { return "marathon" }
|
|
|
|
|
|
|
|
// NewDiscoverer returns a Discoverer for the Config.
|
|
|
|
func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
|
|
|
return NewDiscovery(*c, opts.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirectory joins any relative file paths with dir.
|
|
|
|
func (c *SDConfig) SetDirectory(dir string) {
|
|
|
|
c.HTTPClientConfig.SetDirectory(dir)
|
|
|
|
c.AuthTokenFile = config.JoinDir(dir, c.AuthTokenFile)
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = DefaultSDConfig
|
|
|
|
type plain SDConfig
|
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(c.Servers) == 0 {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("marathon_sd: must contain at least one Marathon server")
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
}
|
2018-04-05 01:08:18 -07:00
|
|
|
if len(c.AuthToken) > 0 && len(c.AuthTokenFile) > 0 {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("marathon_sd: at most one of auth_token & auth_token_file must be configured")
|
2018-04-05 01:08:18 -07:00
|
|
|
}
|
|
|
|
if c.HTTPClientConfig.BasicAuth != nil && (len(c.AuthToken) > 0 || len(c.AuthTokenFile) > 0) {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("marathon_sd: at most one of basic_auth, auth_token & auth_token_file must be configured")
|
2018-04-05 01:08:18 -07:00
|
|
|
}
|
|
|
|
if (len(c.HTTPClientConfig.BearerToken) > 0 || len(c.HTTPClientConfig.BearerTokenFile) > 0) && (len(c.AuthToken) > 0 || len(c.AuthTokenFile) > 0) {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("marathon_sd: at most one of bearer_token, bearer_token_file, auth_token & auth_token_file must be configured")
|
2018-04-05 01:08:18 -07:00
|
|
|
}
|
2018-09-21 03:53:04 -07:00
|
|
|
return c.HTTPClientConfig.Validate()
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
const appListPath string = "/v2/apps/?embed=apps.tasks"
|
|
|
|
|
|
|
|
// Discovery provides service discovery based on a Marathon instance.
|
|
|
|
type Discovery struct {
|
2019-03-25 03:54:22 -07:00
|
|
|
*refresh.Discovery
|
|
|
|
client *http.Client
|
|
|
|
servers []string
|
|
|
|
lastRefresh map[string]*targetgroup.Group
|
|
|
|
appsClient appListClient
|
2016-09-29 05:57:28 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// NewDiscovery returns a new Marathon Discovery.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func NewDiscovery(conf SDConfig, logger log.Logger) (*Discovery, error) {
|
2020-08-20 05:48:26 -07:00
|
|
|
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "marathon_sd", false, false)
|
2016-09-29 05:57:28 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-05 01:08:18 -07:00
|
|
|
if len(conf.AuthToken) > 0 {
|
|
|
|
rt, err = newAuthTokenRoundTripper(conf.AuthToken, rt)
|
|
|
|
} else if len(conf.AuthTokenFile) > 0 {
|
|
|
|
rt, err = newAuthTokenFileRoundTripper(conf.AuthTokenFile, rt)
|
2017-03-02 00:44:20 -08:00
|
|
|
}
|
2018-04-05 01:08:18 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-29 05:57:28 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
d := &Discovery{
|
|
|
|
client: &http.Client{Transport: rt},
|
|
|
|
servers: conf.Servers,
|
|
|
|
appsClient: fetchApps,
|
|
|
|
}
|
|
|
|
d.Discovery = refresh.NewDiscovery(
|
|
|
|
logger,
|
|
|
|
"marathon",
|
|
|
|
time.Duration(conf.RefreshInterval),
|
|
|
|
d.refresh,
|
|
|
|
)
|
|
|
|
return d, nil
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2018-04-05 01:08:18 -07:00
|
|
|
type authTokenRoundTripper struct {
|
2020-08-20 05:48:26 -07:00
|
|
|
authToken config.Secret
|
2018-04-05 01:08:18 -07:00
|
|
|
rt http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAuthTokenRoundTripper adds the provided auth token to a request.
|
2020-08-20 05:48:26 -07:00
|
|
|
func newAuthTokenRoundTripper(token config.Secret, rt http.RoundTripper) (http.RoundTripper, error) {
|
2018-04-05 01:08:18 -07:00
|
|
|
return &authTokenRoundTripper{token, rt}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *authTokenRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
|
|
|
|
// According to https://docs.mesosphere.com/1.11/security/oss/managing-authentication/
|
|
|
|
// DC/OS wants with "token=" a different Authorization header than implemented in httputil/client.go
|
|
|
|
// so we set this explicitly here.
|
|
|
|
request.Header.Set("Authorization", "token="+string(rt.authToken))
|
|
|
|
|
|
|
|
return rt.rt.RoundTrip(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
type authTokenFileRoundTripper struct {
|
|
|
|
authTokenFile string
|
|
|
|
rt http.RoundTripper
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAuthTokenFileRoundTripper adds the auth token read from the file to a request.
|
|
|
|
func newAuthTokenFileRoundTripper(tokenFile string, rt http.RoundTripper) (http.RoundTripper, error) {
|
|
|
|
// fail-fast if we can't read the file.
|
|
|
|
_, err := ioutil.ReadFile(tokenFile)
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return nil, errors.Wrapf(err, "unable to read auth token file %s", tokenFile)
|
2018-04-05 01:08:18 -07:00
|
|
|
}
|
|
|
|
return &authTokenFileRoundTripper{tokenFile, rt}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *authTokenFileRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
|
|
|
|
b, err := ioutil.ReadFile(rt.authTokenFile)
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return nil, errors.Wrapf(err, "unable to read auth token file %s", rt.authTokenFile)
|
2018-04-05 01:08:18 -07:00
|
|
|
}
|
|
|
|
authToken := strings.TrimSpace(string(b))
|
|
|
|
|
|
|
|
// According to https://docs.mesosphere.com/1.11/security/oss/managing-authentication/
|
|
|
|
// DC/OS wants with "token=" a different Authorization header than implemented in httputil/client.go
|
|
|
|
// so we set this explicitly here.
|
|
|
|
request.Header.Set("Authorization", "token="+authToken)
|
|
|
|
return rt.rt.RoundTrip(request)
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
2019-02-26 05:49:16 -08:00
|
|
|
targetMap, err := d.fetchTargetGroups(ctx)
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
2019-03-25 03:54:22 -07:00
|
|
|
return nil, err
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
all := make([]*targetgroup.Group, 0, len(targetMap))
|
2016-04-25 06:30:11 -07:00
|
|
|
for _, tg := range targetMap {
|
|
|
|
all = append(all, tg)
|
|
|
|
}
|
|
|
|
|
2016-04-25 07:32:04 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-03-25 03:54:22 -07:00
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
2016-04-25 07:32:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove services which did disappear.
|
2017-03-16 16:29:47 -07:00
|
|
|
for source := range d.lastRefresh {
|
2016-04-25 06:30:11 -07:00
|
|
|
_, ok := targetMap[source]
|
|
|
|
if !ok {
|
2019-03-25 03:54:22 -07:00
|
|
|
all = append(all, &targetgroup.Group{Source: source})
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
d.lastRefresh = targetMap
|
2019-03-25 03:54:22 -07:00
|
|
|
return all, nil
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2019-02-26 05:49:16 -08:00
|
|
|
func (d *Discovery) fetchTargetGroups(ctx context.Context) (map[string]*targetgroup.Group, error) {
|
2019-03-25 03:54:22 -07:00
|
|
|
url := randomAppsURL(d.servers)
|
2019-02-26 05:49:16 -08:00
|
|
|
apps, err := d.appsClient(ctx, d.client, url)
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
groups := appsToTargetGroups(apps)
|
2016-04-25 06:30:11 -07:00
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// task describes one instance of a service running on Marathon.
|
|
|
|
type task struct {
|
2018-09-21 03:53:04 -07:00
|
|
|
ID string `json:"id"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Ports []uint32 `json:"ports"`
|
2019-03-25 03:54:22 -07:00
|
|
|
IPAddresses []ipAddress `json:"ipAddresses"`
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// ipAddress describes the address and protocol the container's network interface is bound to.
|
|
|
|
type ipAddress struct {
|
2018-09-21 03:53:04 -07:00
|
|
|
Address string `json:"ipAddress"`
|
|
|
|
Proto string `json:"protocol"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PortMapping describes in which port the process are binding inside the docker container.
|
2019-03-25 03:54:22 -07:00
|
|
|
type portMapping struct {
|
2018-09-21 03:53:04 -07:00
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
ContainerPort uint32 `json:"containerPort"`
|
2018-11-26 04:39:35 -08:00
|
|
|
HostPort uint32 `json:"hostPort"`
|
2018-09-21 03:53:04 -07:00
|
|
|
ServicePort uint32 `json:"servicePort"`
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
// DockerContainer describes a container which uses the docker runtime.
|
2019-03-25 03:54:22 -07:00
|
|
|
type dockerContainer struct {
|
2018-09-21 03:53:04 -07:00
|
|
|
Image string `json:"image"`
|
2019-03-25 03:54:22 -07:00
|
|
|
PortMappings []portMapping `json:"portMappings"`
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Container describes the runtime an app in running in.
|
2019-03-25 03:54:22 -07:00
|
|
|
type container struct {
|
|
|
|
Docker dockerContainer `json:"docker"`
|
|
|
|
PortMappings []portMapping `json:"portMappings"`
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2018-09-21 03:53:04 -07:00
|
|
|
// PortDefinition describes which load balancer port you should access to access the service.
|
2019-03-25 03:54:22 -07:00
|
|
|
type portDefinition struct {
|
2017-03-18 13:10:44 -07:00
|
|
|
Labels map[string]string `json:"labels"`
|
2018-09-21 03:53:04 -07:00
|
|
|
Port uint32 `json:"port"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Network describes the name and type of network the container is attached to.
|
2019-03-25 03:54:22 -07:00
|
|
|
type network struct {
|
2018-09-21 03:53:04 -07:00
|
|
|
Name string `json:"name"`
|
|
|
|
Mode string `json:"mode"`
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
// App describes a service running on Marathon.
|
2019-03-25 03:54:22 -07:00
|
|
|
type app struct {
|
2017-03-18 13:10:44 -07:00
|
|
|
ID string `json:"id"`
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks []task `json:"tasks"`
|
2017-03-18 13:10:44 -07:00
|
|
|
RunningTasks int `json:"tasksRunning"`
|
|
|
|
Labels map[string]string `json:"labels"`
|
2019-03-25 03:54:22 -07:00
|
|
|
Container container `json:"container"`
|
|
|
|
PortDefinitions []portDefinition `json:"portDefinitions"`
|
|
|
|
Networks []network `json:"networks"`
|
2019-01-14 09:20:22 -08:00
|
|
|
RequirePorts bool `json:"requirePorts"`
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// isContainerNet checks if the app's first network is set to mode 'container'.
|
2019-03-25 03:54:22 -07:00
|
|
|
func (app app) isContainerNet() bool {
|
2018-09-21 03:53:04 -07:00
|
|
|
return len(app.Networks) > 0 && app.Networks[0].Mode == "container"
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// appList is a list of Marathon apps.
|
|
|
|
type appList struct {
|
|
|
|
Apps []app `json:"apps"`
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// appListClient defines a function that can be used to get an application list from marathon.
|
|
|
|
type appListClient func(ctx context.Context, client *http.Client, url string) (*appList, error)
|
2016-04-25 06:30:11 -07:00
|
|
|
|
2016-09-29 05:57:28 -07:00
|
|
|
// fetchApps requests a list of applications from a marathon server.
|
2019-03-25 03:54:22 -07:00
|
|
|
func fetchApps(ctx context.Context, client *http.Client, url string) (*appList, error) {
|
2017-03-02 00:44:20 -08:00
|
|
|
request, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-26 05:49:16 -08:00
|
|
|
request = request.WithContext(ctx)
|
2017-03-02 00:44:20 -08:00
|
|
|
|
|
|
|
resp, err := client.Do(request)
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-18 01:50:37 -07:00
|
|
|
defer func() {
|
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
}()
|
2016-04-25 06:30:11 -07:00
|
|
|
|
2018-04-17 01:28:06 -07:00
|
|
|
if (resp.StatusCode < 200) || (resp.StatusCode >= 300) {
|
2019-03-25 16:01:12 -07:00
|
|
|
return nil, errors.Errorf("non 2xx status '%v' response during marathon service discovery", resp.StatusCode)
|
2018-04-17 01:28:06 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
var apps appList
|
2018-11-27 05:58:27 -08:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&apps)
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return nil, errors.Wrapf(err, "%q", url)
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
2018-11-27 05:58:27 -08:00
|
|
|
return &apps, nil
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// randomAppsURL randomly selects a server from an array and creates
|
2016-04-25 06:30:11 -07:00
|
|
|
// an URL pointing to the app list.
|
2019-03-25 03:54:22 -07:00
|
|
|
func randomAppsURL(servers []string) string {
|
2016-04-25 06:30:11 -07:00
|
|
|
// TODO: If possible update server list from Marathon at some point.
|
|
|
|
server := servers[rand.Intn(len(servers))]
|
|
|
|
return fmt.Sprintf("%s%s", server, appListPath)
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// appsToTargetGroups takes an array of Marathon apps and converts them into target groups.
|
|
|
|
func appsToTargetGroups(apps *appList) map[string]*targetgroup.Group {
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
tgroups := map[string]*targetgroup.Group{}
|
2016-04-25 06:30:11 -07:00
|
|
|
for _, a := range apps.Apps {
|
|
|
|
group := createTargetGroup(&a)
|
|
|
|
tgroups[group.Source] = group
|
|
|
|
}
|
|
|
|
return tgroups
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func createTargetGroup(app *app) *targetgroup.Group {
|
2016-04-25 06:30:11 -07:00
|
|
|
var (
|
|
|
|
targets = targetsForApp(app)
|
|
|
|
appName = model.LabelValue(app.ID)
|
|
|
|
image = model.LabelValue(app.Container.Docker.Image)
|
|
|
|
)
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
tg := &targetgroup.Group{
|
2016-04-25 06:30:11 -07:00
|
|
|
Targets: targets,
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
appLabel: appName,
|
|
|
|
imageLabel: image,
|
|
|
|
},
|
|
|
|
Source: app.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
for ln, lv := range app.Labels {
|
2017-03-18 13:10:44 -07:00
|
|
|
ln = appLabelPrefix + strutil.SanitizeLabelName(ln)
|
2016-04-25 06:30:11 -07:00
|
|
|
tg.Labels[model.LabelName(ln)] = model.LabelValue(lv)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tg
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func targetsForApp(app *app) []model.LabelSet {
|
2016-04-25 06:30:11 -07:00
|
|
|
targets := make([]model.LabelSet, 0, len(app.Tasks))
|
2018-09-21 03:53:04 -07:00
|
|
|
|
|
|
|
var ports []uint32
|
|
|
|
var labels []map[string]string
|
|
|
|
var prefix string
|
|
|
|
|
|
|
|
if len(app.Container.PortMappings) != 0 {
|
|
|
|
// In Marathon 1.5.x the "container.docker.portMappings" object was moved
|
|
|
|
// to "container.portMappings".
|
|
|
|
ports, labels = extractPortMapping(app.Container.PortMappings, app.isContainerNet())
|
|
|
|
prefix = portMappingLabelPrefix
|
|
|
|
|
|
|
|
} else if len(app.Container.Docker.PortMappings) != 0 {
|
|
|
|
// Prior to Marathon 1.5 the port mappings could be found at the path
|
|
|
|
// "container.docker.portMappings".
|
|
|
|
ports, labels = extractPortMapping(app.Container.Docker.PortMappings, app.isContainerNet())
|
|
|
|
prefix = portMappingLabelPrefix
|
|
|
|
|
|
|
|
} else if len(app.PortDefinitions) != 0 {
|
|
|
|
// PortDefinitions deprecates the "ports" array and can be used to specify
|
|
|
|
// a list of ports with metadata in case a mapping is not required.
|
|
|
|
ports = make([]uint32, len(app.PortDefinitions))
|
|
|
|
labels = make([]map[string]string, len(app.PortDefinitions))
|
|
|
|
|
|
|
|
for i := 0; i < len(app.PortDefinitions); i++ {
|
|
|
|
labels[i] = app.PortDefinitions[i].Labels
|
2019-01-14 09:20:22 -08:00
|
|
|
// When requirePorts is false, this port becomes the 'servicePort', not the listen port.
|
|
|
|
// In this case, the port needs to be taken from the task instead of the app.
|
|
|
|
if app.RequirePorts {
|
|
|
|
ports[i] = app.PortDefinitions[i].Port
|
|
|
|
}
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
prefix = portDefinitionLabelPrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather info about the app's 'tasks'. Each instance (container) is considered a task
|
|
|
|
// and can be reachable at one or more host:port endpoints.
|
2016-04-25 06:30:11 -07:00
|
|
|
for _, t := range app.Tasks {
|
2018-09-21 03:53:04 -07:00
|
|
|
|
|
|
|
// There are no labels to gather if only Ports is defined. (eg. with host networking)
|
|
|
|
// Ports can only be gathered from the Task (not from the app) and are guaranteed
|
|
|
|
// to be the same across all tasks. If we haven't gathered any ports by now,
|
|
|
|
// use the task's ports as the port list.
|
|
|
|
if len(ports) == 0 && len(t.Ports) != 0 {
|
|
|
|
ports = t.Ports
|
2016-06-13 02:24:03 -07:00
|
|
|
}
|
2018-09-21 03:53:04 -07:00
|
|
|
|
|
|
|
// Iterate over the ports we gathered using one of the methods above.
|
|
|
|
for i, port := range ports {
|
|
|
|
|
2019-01-14 09:20:22 -08:00
|
|
|
// A zero port here means that either the portMapping has a zero port defined,
|
|
|
|
// or there is a portDefinition with requirePorts set to false. This means the port
|
|
|
|
// is auto-generated by Mesos and needs to be looked up in the task.
|
2018-11-26 04:39:35 -08:00
|
|
|
if port == 0 && len(t.Ports) == len(ports) {
|
|
|
|
port = t.Ports[i]
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:53:04 -07:00
|
|
|
// Each port represents a possible Prometheus target.
|
|
|
|
targetAddress := targetEndpoint(&t, port, app.isContainerNet())
|
2017-03-18 13:10:44 -07:00
|
|
|
target := model.LabelSet{
|
|
|
|
model.AddressLabel: model.LabelValue(targetAddress),
|
|
|
|
taskLabel: model.LabelValue(t.ID),
|
Marathon SD: Set port index label
The changes [1][] to Marathon service discovery to support multiple
ports mean that Prometheus now attempts to scrape all ports belonging to
a Marathon service.
You can use port definition or port mapping labels to filter out which
ports to scrape but that requires service owners to update their
Marathon configuration.
To allow for a smoother migration path, add a
`__meta_marathon_port_index` label, whose value is set to the port's
sequential index integer. For example, PORT0 has the value `0`, PORT1
has the value `1`, and so on.
This allows you to support scraping both the first available port (the
previous behaviour) in addition to ports with a `metrics` label.
For example, here's the relabel configuration we might use with
this patch:
- action: keep
source_labels: ['__meta_marathon_port_definition_label_metrics', '__meta_marathon_port_mapping_label_metrics', '__meta_marathon_port_index']
# Keep if port mapping or definition has a 'metrics' label with any
# non-empty value, or if no 'metrics' port label exists but this is the
# service's first available port
regex: ([^;]+;;[^;]+|;[^;]+;[^;]+|;;0)
This assumes that the Marathon API returns the ports in sorted order
(matching PORT0, PORT1, etc), which it appears that it does.
[1]: https://github.com/prometheus/prometheus/pull/2506
2017-06-20 07:43:36 -07:00
|
|
|
portIndexLabel: model.LabelValue(strconv.Itoa(i)),
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
2018-09-21 03:53:04 -07:00
|
|
|
|
|
|
|
// Gather all port labels and set them on the current target, skip if the port has no Marathon labels.
|
|
|
|
// This will happen in the host networking case with only `ports` defined, where
|
|
|
|
// it is inefficient to allocate a list of possibly hundreds of empty label maps per host port.
|
|
|
|
if len(labels) > 0 {
|
|
|
|
for ln, lv := range labels[i] {
|
|
|
|
ln = prefix + strutil.SanitizeLabelName(ln)
|
2017-12-05 11:10:40 -08:00
|
|
|
target[model.LabelName(ln)] = model.LabelValue(lv)
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 03:53:04 -07:00
|
|
|
|
2017-03-18 13:10:44 -07:00
|
|
|
targets = append(targets, target)
|
|
|
|
}
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:53:04 -07:00
|
|
|
// Generate a target endpoint string in host:port format.
|
2019-03-25 03:54:22 -07:00
|
|
|
func targetEndpoint(task *task, port uint32, containerNet bool) string {
|
2018-09-21 03:53:04 -07:00
|
|
|
|
|
|
|
var host string
|
|
|
|
|
|
|
|
// Use the task's ipAddress field when it's in a container network
|
|
|
|
if containerNet && len(task.IPAddresses) > 0 {
|
|
|
|
host = task.IPAddresses[0].Address
|
|
|
|
} else {
|
|
|
|
host = task.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.JoinHostPort(host, fmt.Sprintf("%d", port))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of ports and a list of labels from a PortMapping.
|
2019-03-25 03:54:22 -07:00
|
|
|
func extractPortMapping(portMappings []portMapping, containerNet bool) ([]uint32, []map[string]string) {
|
2018-09-21 03:53:04 -07:00
|
|
|
|
|
|
|
ports := make([]uint32, len(portMappings))
|
|
|
|
labels := make([]map[string]string, len(portMappings))
|
|
|
|
|
|
|
|
for i := 0; i < len(portMappings); i++ {
|
|
|
|
|
|
|
|
labels[i] = portMappings[i].Labels
|
|
|
|
|
|
|
|
if containerNet {
|
|
|
|
// If the app is in a container network, connect directly to the container port.
|
|
|
|
ports[i] = portMappings[i].ContainerPort
|
|
|
|
} else {
|
2018-11-26 04:39:35 -08:00
|
|
|
// Otherwise, connect to the allocated host port for the container.
|
|
|
|
// Note that this host port is likely set to 0 in the app definition, which means it is
|
|
|
|
// automatically generated and needs to be extracted from the task's 'ports' array at a later stage.
|
|
|
|
ports[i] = portMappings[i].HostPort
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ports, labels
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|