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"
|
|
|
|
"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"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
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
|
|
|
conntrack "github.com/mwitkow/go-conntrack"
|
2016-10-21 03:14:53 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-01-11 07:10:25 -08:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2016-04-25 06:30:11 -07:00
|
|
|
"github.com/prometheus/common/model"
|
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"
|
2016-09-29 05:57:28 -07:00
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
2017-03-18 13:10:44 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
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
|
|
|
yaml_util "github.com/prometheus/prometheus/util/yaml"
|
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
|
|
|
// Constants for instrumentation.
|
|
|
|
namespace = "prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-10-26 02:03:35 -07:00
|
|
|
refreshFailuresCount = prometheus.NewCounter(
|
2016-10-21 03:14:53 -07:00
|
|
|
prometheus.CounterOpts{
|
|
|
|
Namespace: namespace,
|
2016-10-26 02:03:35 -07:00
|
|
|
Name: "sd_marathon_refresh_failures_total",
|
|
|
|
Help: "The number of Marathon-SD refresh failures.",
|
2016-10-21 03:14:53 -07:00
|
|
|
})
|
2016-10-26 02:03:35 -07:00
|
|
|
refreshDuration = prometheus.NewSummary(
|
2016-10-21 03:14:53 -07:00
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Namespace: namespace,
|
2016-10-26 02:03:35 -07:00
|
|
|
Name: "sd_marathon_refresh_duration_seconds",
|
|
|
|
Help: "The duration of a Marathon-SD refresh in seconds.",
|
2016-10-21 03:14:53 -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
|
|
|
// DefaultSDConfig is the default Marathon SD configuration.
|
|
|
|
DefaultSDConfig = SDConfig{
|
|
|
|
Timeout: model.Duration(30 * time.Second),
|
|
|
|
RefreshInterval: model.Duration(30 * time.Second),
|
|
|
|
}
|
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
|
|
|
// SDConfig is the configuration for services running on Marathon.
|
|
|
|
type SDConfig struct {
|
|
|
|
Servers []string `yaml:"servers,omitempty"`
|
|
|
|
Timeout model.Duration `yaml:"timeout,omitempty"`
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
|
|
|
|
TLSConfig config_util.TLSConfig `yaml:"tls_config,omitempty"`
|
|
|
|
BearerToken config_util.Secret `yaml:"bearer_token,omitempty"`
|
|
|
|
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
|
|
|
|
|
|
|
|
// Catches all undefined fields and must be empty after parsing.
|
|
|
|
XXX map[string]interface{} `yaml:",inline"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 err := yaml_util.CheckOverflow(c.XXX, "marathon_sd_config"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(c.Servers) == 0 {
|
|
|
|
return fmt.Errorf("Marathon SD config must contain at least one Marathon server")
|
|
|
|
}
|
|
|
|
if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
|
|
|
|
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-21 03:14:53 -07:00
|
|
|
func init() {
|
2016-10-26 02:03:35 -07:00
|
|
|
prometheus.MustRegister(refreshFailuresCount)
|
|
|
|
prometheus.MustRegister(refreshDuration)
|
2016-10-21 03:14:53 -07: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 {
|
2016-09-29 05:57:28 -07:00
|
|
|
client *http.Client
|
|
|
|
servers []string
|
|
|
|
refreshInterval time.Duration
|
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
|
|
|
lastRefresh map[string]*targetgroup.Group
|
2016-09-29 05:57:28 -07:00
|
|
|
appsClient AppListClient
|
2017-03-02 00:44:20 -08:00
|
|
|
token string
|
2017-04-28 08:12:38 -07:00
|
|
|
logger log.Logger
|
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) {
|
2017-08-11 11:45:52 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:57:28 -07:00
|
|
|
tls, err := httputil.NewTLSConfig(conf.TLSConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-29 04:46:23 -07:00
|
|
|
token := string(conf.BearerToken)
|
2017-03-02 00:44:20 -08:00
|
|
|
if conf.BearerTokenFile != "" {
|
|
|
|
bf, err := ioutil.ReadFile(conf.BearerTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token = strings.TrimSpace(string(bf))
|
|
|
|
}
|
|
|
|
|
2016-09-29 05:57:28 -07:00
|
|
|
client := &http.Client{
|
2016-10-24 03:27:08 -07:00
|
|
|
Timeout: time.Duration(conf.Timeout),
|
2016-09-29 05:57:28 -07:00
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: tls,
|
2017-10-06 03:22:19 -07:00
|
|
|
DialContext: conntrack.NewDialContextFunc(
|
|
|
|
conntrack.DialWithTracing(),
|
|
|
|
conntrack.DialWithName("marathon_sd"),
|
|
|
|
),
|
2016-09-29 05:57:28 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Discovery{
|
|
|
|
client: client,
|
|
|
|
servers: conf.Servers,
|
|
|
|
refreshInterval: time.Duration(conf.RefreshInterval),
|
|
|
|
appsClient: fetchApps,
|
2017-03-02 00:44:20 -08:00
|
|
|
token: token,
|
2017-04-28 08:12:38 -07:00
|
|
|
logger: logger,
|
2016-09-29 05:57:28 -07:00
|
|
|
}, nil
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2018-01-08 15:59:18 -08:00
|
|
|
// Run implements the Discoverer interface.
|
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 (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
2016-04-25 06:30:11 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2017-03-16 16:29:47 -07:00
|
|
|
case <-time.After(d.refreshInterval):
|
|
|
|
err := d.updateServices(ctx, ch)
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(d.logger).Log("msg", "Error while updating services", "err", 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
|
|
|
func (d *Discovery) updateServices(ctx context.Context, ch chan<- []*targetgroup.Group) (err error) {
|
2016-10-21 03:14:53 -07:00
|
|
|
t0 := time.Now()
|
|
|
|
defer func() {
|
2016-10-26 02:03:35 -07:00
|
|
|
refreshDuration.Observe(time.Since(t0).Seconds())
|
2016-10-21 03:14:53 -07:00
|
|
|
if err != nil {
|
2016-10-26 02:03:35 -07:00
|
|
|
refreshFailuresCount.Inc()
|
2016-10-21 03:14:53 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
targetMap, err := d.fetchTargetGroups()
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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():
|
|
|
|
return ctx.Err()
|
|
|
|
case ch <- all:
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-04-25 07:32:04 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
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
|
|
|
case ch <- []*targetgroup.Group{{Source: source}}:
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Debug(d.logger).Log("msg", "Removing group", "source", source)
|
2016-04-25 07:32:04 -07:00
|
|
|
}
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
d.lastRefresh = targetMap
|
2016-04-25 06:30:11 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 (d *Discovery) fetchTargetGroups() (map[string]*targetgroup.Group, error) {
|
2017-03-16 16:29:47 -07:00
|
|
|
url := RandomAppsURL(d.servers)
|
|
|
|
apps, err := d.appsClient(d.client, url, d.token)
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := AppsToTargetGroups(apps)
|
|
|
|
return groups, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Task describes one instance of a service running on Marathon.
|
|
|
|
type Task struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Ports []uint32 `json:"ports"`
|
|
|
|
}
|
|
|
|
|
2017-03-18 13:10:44 -07:00
|
|
|
// PortMappings describes in which port the process are binding inside the docker container.
|
|
|
|
type PortMappings struct {
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
// DockerContainer describes a container which uses the docker runtime.
|
|
|
|
type DockerContainer struct {
|
2017-03-18 13:10:44 -07:00
|
|
|
Image string `json:"image"`
|
|
|
|
PortMappings []PortMappings `json:"portMappings"`
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Container describes the runtime an app in running in.
|
|
|
|
type Container struct {
|
2017-12-05 11:10:40 -08:00
|
|
|
Docker DockerContainer `json:"docker"`
|
|
|
|
PortMappings []PortMappings `json:"portMappings"`
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
2017-03-18 13:10:44 -07:00
|
|
|
// PortDefinitions describes which load balancer port you should access to access the service.
|
|
|
|
type PortDefinitions struct {
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
}
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
// App describes a service running on Marathon.
|
|
|
|
type App struct {
|
2017-03-18 13:10:44 -07:00
|
|
|
ID string `json:"id"`
|
|
|
|
Tasks []Task `json:"tasks"`
|
|
|
|
RunningTasks int `json:"tasksRunning"`
|
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
Container Container `json:"container"`
|
|
|
|
PortDefinitions []PortDefinitions `json:"portDefinitions"`
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppList is a list of Marathon apps.
|
|
|
|
type AppList struct {
|
|
|
|
Apps []App `json:"apps"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppListClient defines a function that can be used to get an application list from marathon.
|
2017-03-02 00:44:20 -08:00
|
|
|
type AppListClient func(client *http.Client, url, token 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.
|
2017-03-02 00:44:20 -08:00
|
|
|
func fetchApps(client *http.Client, url, token string) (*AppList, error) {
|
|
|
|
request, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-03-06 02:36:36 -08:00
|
|
|
// According to https://dcos.io/docs/1.8/administration/id-and-access-mgt/managing-authentication
|
|
|
|
// DC/OS wants with "token=" a different Authorization header than implemented in httputil/client.go
|
|
|
|
// so we set this implicitly here
|
2017-03-02 00:44:20 -08:00
|
|
|
if token != "" {
|
|
|
|
request.Header.Set("Authorization", "token="+token)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(request)
|
2016-04-25 06:30:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseAppJSON(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAppJSON(body []byte) (*AppList, error) {
|
|
|
|
apps := &AppList{}
|
|
|
|
err := json.Unmarshal(body, apps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return apps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RandomAppsURL randomly selects a server from an array and creates
|
|
|
|
// an URL pointing to the app list.
|
|
|
|
func RandomAppsURL(servers []string) string {
|
|
|
|
// TODO: If possible update server list from Marathon at some point.
|
|
|
|
server := servers[rand.Intn(len(servers))]
|
|
|
|
return fmt.Sprintf("%s%s", server, appListPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppsToTargetGroups takes an array of Marathon apps and converts them into target groups.
|
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 AppsToTargetGroups(apps *AppList) map[string]*targetgroup.Group {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
|
|
|
|
func targetsForApp(app *App) []model.LabelSet {
|
|
|
|
targets := make([]model.LabelSet, 0, len(app.Tasks))
|
|
|
|
for _, t := range app.Tasks {
|
2016-06-13 02:24:03 -07:00
|
|
|
if len(t.Ports) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2017-03-18 13:10:44 -07:00
|
|
|
for i := 0; i < len(t.Ports); i++ {
|
|
|
|
targetAddress := targetForTask(&t, i)
|
|
|
|
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
|
|
|
}
|
|
|
|
if i < len(app.PortDefinitions) {
|
|
|
|
for ln, lv := range app.PortDefinitions[i].Labels {
|
|
|
|
ln = portDefinitionLabelPrefix + strutil.SanitizeLabelName(ln)
|
|
|
|
target[model.LabelName(ln)] = model.LabelValue(lv)
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 11:10:40 -08:00
|
|
|
// Prior to Marathon 1.5 the port mappings could be found at the path
|
|
|
|
// "container.docker.portMappings". When support for Marathon 1.4
|
|
|
|
// is dropped then this section of code can be removed.
|
2017-03-18 13:10:44 -07:00
|
|
|
if i < len(app.Container.Docker.PortMappings) {
|
|
|
|
for ln, lv := range app.Container.Docker.PortMappings[i].Labels {
|
|
|
|
ln = portMappingLabelPrefix + strutil.SanitizeLabelName(ln)
|
|
|
|
target[model.LabelName(ln)] = model.LabelValue(lv)
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 11:10:40 -08:00
|
|
|
// In Marathon 1.5.x the container.docker.portMappings object was moved
|
|
|
|
// to container.portMappings.
|
|
|
|
if i < len(app.Container.PortMappings) {
|
|
|
|
for ln, lv := range app.Container.PortMappings[i].Labels {
|
|
|
|
ln = portMappingLabelPrefix + strutil.SanitizeLabelName(ln)
|
|
|
|
target[model.LabelName(ln)] = model.LabelValue(lv)
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 13:10:44 -07:00
|
|
|
targets = append(targets, target)
|
|
|
|
}
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
|
|
|
return targets
|
|
|
|
}
|
|
|
|
|
2017-03-18 13:10:44 -07:00
|
|
|
func targetForTask(task *Task, index int) string {
|
|
|
|
return net.JoinHostPort(task.Host, fmt.Sprintf("%d", task.Ports[index]))
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|