github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/promtail/scrapeconfig/scrapeconfig.go (about) 1 package scrapeconfig 2 3 import ( 4 "fmt" 5 "reflect" 6 "time" 7 8 "github.com/Shopify/sarama" 9 "github.com/grafana/dskit/flagext" 10 11 promconfig "github.com/prometheus/common/config" 12 "github.com/prometheus/common/model" 13 "github.com/prometheus/prometheus/discovery" 14 "github.com/prometheus/prometheus/discovery/aws" 15 "github.com/prometheus/prometheus/discovery/azure" 16 "github.com/prometheus/prometheus/discovery/consul" 17 "github.com/prometheus/prometheus/discovery/digitalocean" 18 "github.com/prometheus/prometheus/discovery/dns" 19 "github.com/prometheus/prometheus/discovery/file" 20 "github.com/prometheus/prometheus/discovery/gce" 21 "github.com/prometheus/prometheus/discovery/kubernetes" 22 "github.com/prometheus/prometheus/discovery/marathon" 23 "github.com/prometheus/prometheus/discovery/moby" 24 "github.com/prometheus/prometheus/discovery/openstack" 25 "github.com/prometheus/prometheus/discovery/triton" 26 "github.com/prometheus/prometheus/discovery/zookeeper" 27 "github.com/prometheus/prometheus/model/relabel" 28 "github.com/weaveworks/common/server" 29 30 "github.com/grafana/loki/clients/pkg/logentry/stages" 31 "github.com/grafana/loki/clients/pkg/promtail/discovery/consulagent" 32 ) 33 34 // Config describes a job to scrape. 35 type Config struct { 36 JobName string `yaml:"job_name,omitempty"` 37 PipelineStages stages.PipelineStages `yaml:"pipeline_stages,omitempty"` 38 JournalConfig *JournalTargetConfig `yaml:"journal,omitempty"` 39 SyslogConfig *SyslogTargetConfig `yaml:"syslog,omitempty"` 40 GcplogConfig *GcplogTargetConfig `yaml:"gcplog,omitempty"` 41 PushConfig *PushTargetConfig `yaml:"loki_push_api,omitempty"` 42 WindowsConfig *WindowsEventsTargetConfig `yaml:"windows_events,omitempty"` 43 KafkaConfig *KafkaTargetConfig `yaml:"kafka,omitempty"` 44 GelfConfig *GelfTargetConfig `yaml:"gelf,omitempty"` 45 CloudflareConfig *CloudflareConfig `yaml:"cloudflare,omitempty"` 46 HerokuDrainConfig *HerokuDrainTargetConfig `yaml:"heroku_drain,omitempty"` 47 RelabelConfigs []*relabel.Config `yaml:"relabel_configs,omitempty"` 48 // List of Docker service discovery configurations. 49 DockerSDConfigs []*moby.DockerSDConfig `yaml:"docker_sd_configs,omitempty"` 50 ServiceDiscoveryConfig ServiceDiscoveryConfig `yaml:",inline"` 51 Encoding string `yaml:"encoding,omitempty"` 52 } 53 54 type ServiceDiscoveryConfig struct { 55 // List of labeled target groups for this job. 56 StaticConfigs discovery.StaticConfig `yaml:"static_configs"` 57 // List of DNS service discovery configurations. 58 DNSSDConfigs []*dns.SDConfig `yaml:"dns_sd_configs,omitempty"` 59 // List of file service discovery configurations. 60 FileSDConfigs []*file.SDConfig `yaml:"file_sd_configs,omitempty"` 61 // List of Consul service discovery configurations. 62 ConsulSDConfigs []*consul.SDConfig `yaml:"consul_sd_configs,omitempty"` 63 // List of Consul agent service discovery configurations. 64 ConsulAgentSDConfigs []*consulagent.SDConfig `yaml:"consulagent_sd_configs,omitempty"` 65 // List of DigitalOcean service discovery configurations. 66 DigitalOceanSDConfigs []*digitalocean.SDConfig `yaml:"digitalocean_sd_configs,omitempty"` 67 // List of Docker Swarm service discovery configurations. 68 DockerSwarmSDConfigs []*moby.DockerSwarmSDConfig `yaml:"dockerswarm_sd_configs,omitempty"` 69 // List of Serverset service discovery configurations. 70 ServersetSDConfigs []*zookeeper.ServersetSDConfig `yaml:"serverset_sd_configs,omitempty"` 71 // NerveSDConfigs is a list of Nerve service discovery configurations. 72 NerveSDConfigs []*zookeeper.NerveSDConfig `yaml:"nerve_sd_configs,omitempty"` 73 // MarathonSDConfigs is a list of Marathon service discovery configurations. 74 MarathonSDConfigs []*marathon.SDConfig `yaml:"marathon_sd_configs,omitempty"` 75 // List of Kubernetes service discovery configurations. 76 KubernetesSDConfigs []*kubernetes.SDConfig `yaml:"kubernetes_sd_configs,omitempty"` 77 // List of GCE service discovery configurations. 78 GCESDConfigs []*gce.SDConfig `yaml:"gce_sd_configs,omitempty"` 79 // List of EC2 service discovery configurations. 80 EC2SDConfigs []*aws.EC2SDConfig `yaml:"ec2_sd_configs,omitempty"` 81 // List of OpenStack service discovery configurations. 82 OpenstackSDConfigs []*openstack.SDConfig `yaml:"openstack_sd_configs,omitempty"` 83 // List of Azure service discovery configurations. 84 AzureSDConfigs []*azure.SDConfig `yaml:"azure_sd_configs,omitempty"` 85 // List of Triton service discovery configurations. 86 TritonSDConfigs []*triton.SDConfig `yaml:"triton_sd_configs,omitempty"` 87 } 88 89 func (cfg ServiceDiscoveryConfig) Configs() (res discovery.Configs) { 90 if x := cfg.StaticConfigs; len(x) > 0 { 91 res = append(res, x) 92 } 93 for _, x := range cfg.DNSSDConfigs { 94 res = append(res, x) 95 } 96 for _, x := range cfg.FileSDConfigs { 97 res = append(res, x) 98 } 99 for _, x := range cfg.ConsulSDConfigs { 100 res = append(res, x) 101 } 102 for _, x := range cfg.ConsulAgentSDConfigs { 103 res = append(res, x) 104 } 105 for _, x := range cfg.DigitalOceanSDConfigs { 106 res = append(res, x) 107 } 108 for _, x := range cfg.DockerSwarmSDConfigs { 109 res = append(res, x) 110 } 111 for _, x := range cfg.ServersetSDConfigs { 112 res = append(res, x) 113 } 114 for _, x := range cfg.NerveSDConfigs { 115 res = append(res, x) 116 } 117 for _, x := range cfg.MarathonSDConfigs { 118 res = append(res, x) 119 } 120 for _, x := range cfg.KubernetesSDConfigs { 121 res = append(res, x) 122 } 123 for _, x := range cfg.GCESDConfigs { 124 res = append(res, x) 125 } 126 for _, x := range cfg.EC2SDConfigs { 127 res = append(res, x) 128 } 129 for _, x := range cfg.OpenstackSDConfigs { 130 res = append(res, x) 131 } 132 for _, x := range cfg.AzureSDConfigs { 133 res = append(res, x) 134 } 135 for _, x := range cfg.TritonSDConfigs { 136 res = append(res, x) 137 } 138 return res 139 } 140 141 // JournalTargetConfig describes systemd journal records to scrape. 142 type JournalTargetConfig struct { 143 // MaxAge determines the oldest relative time from process start that will 144 // be read and sent to Loki. Values like 14h means no entry older than 145 // 14h will be read. If unspecified, defaults to 7h. 146 // 147 // A relative time specified here takes precedence over the saved position; 148 // if the cursor is older than the MaxAge value, it will not be used. 149 MaxAge string `yaml:"max_age"` 150 151 // JSON forces the output message of entries read from the journal to be 152 // JSON. The message will contain all original fields from the source 153 // journal entry. 154 JSON bool `yaml:"json"` 155 156 // Labels optionally holds labels to associate with each record coming out 157 // of the journal. 158 Labels model.LabelSet `yaml:"labels"` 159 160 // Path to a directory to read journal entries from. Defaults to system path 161 // if empty. 162 Path string `yaml:"path"` 163 } 164 165 // SyslogTargetConfig describes a scrape config that listens for log lines over syslog. 166 type SyslogTargetConfig struct { 167 // ListenAddress is the address to listen on for syslog messages. 168 ListenAddress string `yaml:"listen_address"` 169 170 // ListenProtocol is the protocol used to listen for syslog messages. 171 // Must be either `tcp` (default) or `udp` 172 ListenProtocol string `yaml:"listen_protocol"` 173 174 // IdleTimeout is the idle timeout for tcp connections. 175 IdleTimeout time.Duration `yaml:"idle_timeout"` 176 177 // LabelStructuredData sets if the structured data part of a syslog message 178 // is translated to a label. 179 // [example@99999 test="yes"] => {__syslog_message_sd_example_99999_test="yes"} 180 LabelStructuredData bool `yaml:"label_structured_data"` 181 182 // Labels optionally holds labels to associate with each record read from syslog. 183 Labels model.LabelSet `yaml:"labels"` 184 185 // UseIncomingTimestamp sets the timestamp to the incoming syslog messages 186 // timestamp if it's set. 187 UseIncomingTimestamp bool `yaml:"use_incoming_timestamp"` 188 189 // UseRFC5424Message defines whether the full RFC5424 formatted syslog 190 // message should be pushed to Loki 191 UseRFC5424Message bool `yaml:"use_rfc5424_message"` 192 193 // MaxMessageLength sets the maximum limit to the length of syslog messages 194 MaxMessageLength int `yaml:"max_message_length"` 195 196 TLSConfig promconfig.TLSConfig `yaml:"tls_config,omitempty"` 197 } 198 199 // WindowsEventsTargetConfig describes a scrape config that listen for windows event logs. 200 type WindowsEventsTargetConfig struct { 201 202 // LCID (Locale ID) for event rendering 203 // - 1033 to force English language 204 // - 0 to use default Windows locale 205 Locale uint32 `yaml:"locale"` 206 207 // Name of eventlog, used only if xpath_query is empty 208 // Example: "Application" 209 EventlogName string `yaml:"eventlog_name"` 210 211 // xpath_query can be in defined short form like "Event/System[EventID=999]" 212 // or you can form a XML Query. Refer to the Consuming Events article: 213 // https://docs.microsoft.com/en-us/windows/win32/wes/consuming-events 214 // XML query is the recommended form, because it is most flexible 215 // You can create or debug XML Query by creating Custom View in Windows Event Viewer 216 // and then copying resulting XML here 217 Query string `yaml:"xpath_query"` 218 219 // UseIncomingTimestamp sets the timestamp to the incoming windows messages 220 // timestamp if it's set. 221 UseIncomingTimestamp bool `yaml:"use_incoming_timestamp"` 222 223 // BookmarkPath sets the bookmark location on the filesystem. 224 // The bookmark contains the current position of the target in XML. 225 // When restarting or rollingout promtail, the target will continue to scrape events where it left off based on the bookmark position. 226 // The position is updated after each entry processed. 227 BookmarkPath string `yaml:"bookmark_path"` 228 229 // PollInterval is the interval at which we're looking if new events are available. By default the target will check every 3seconds. 230 PollInterval time.Duration `yaml:"poll_interval"` 231 232 // ExcludeEventData allows to exclude the xml event data. 233 ExcludeEventData bool `yaml:"exclude_event_data"` 234 235 // ExcludeUserData allows to exclude the user data of each windows event. 236 ExcludeUserData bool `yaml:"exclude_user_data"` 237 238 // Labels optionally holds labels to associate with each log line. 239 Labels model.LabelSet `yaml:"labels"` 240 } 241 242 type KafkaTargetConfig struct { 243 // Labels optionally holds labels to associate with each log line. 244 Labels model.LabelSet `yaml:"labels"` 245 246 // UseIncomingTimestamp sets the timestamp to the incoming kafka messages 247 // timestamp if it's set. 248 UseIncomingTimestamp bool `yaml:"use_incoming_timestamp"` 249 250 // The list of brokers to connect to kafka (Required). 251 Brokers []string `yaml:"brokers"` 252 253 // The consumer group id (Required). 254 GroupID string `yaml:"group_id"` 255 256 // Kafka Topics to consume (Required). 257 Topics []string `yaml:"topics"` 258 259 // Kafka version. Default to 2.2.1 260 Version string `yaml:"version"` 261 262 // Rebalancing strategy to use. (e.g sticky, roundrobin or range) 263 Assignor string `yaml:"assignor"` 264 265 // Authentication strategy with Kafka brokers 266 Authentication KafkaAuthentication `yaml:"authentication"` 267 } 268 269 // KafkaAuthenticationType specifies method to authenticate with Kafka brokers 270 type KafkaAuthenticationType string 271 272 const ( 273 // KafkaAuthenticationTypeNone represents using no authentication 274 KafkaAuthenticationTypeNone = "none" 275 // KafkaAuthenticationTypeSSL represents using SSL/TLS to authenticate 276 KafkaAuthenticationTypeSSL = "ssl" 277 // KafkaAuthenticationTypeSASL represents using SASL to authenticate 278 KafkaAuthenticationTypeSASL = "sasl" 279 ) 280 281 // KafkaAuthentication describe the configuration for authentication with Kafka brokers 282 type KafkaAuthentication struct { 283 // Type is authentication type 284 // Possible values: none, sasl and ssl (defaults to none). 285 Type KafkaAuthenticationType `yaml:"type"` 286 287 // TLSConfig is used for TLS encryption and authentication with Kafka brokers 288 TLSConfig promconfig.TLSConfig `yaml:"tls_config,omitempty"` 289 290 // SASLConfig is used for SASL authentication with Kafka brokers 291 SASLConfig KafkaSASLConfig `yaml:"sasl_config,omitempty"` 292 } 293 294 // KafkaSASLConfig describe the SASL configuration for authentication with Kafka brokers 295 type KafkaSASLConfig struct { 296 // SASL mechanism. Supports PLAIN, SCRAM-SHA-256 and SCRAM-SHA-512 297 Mechanism sarama.SASLMechanism `yaml:"mechanism"` 298 299 // SASL Username 300 User string `yaml:"user"` 301 302 // SASL Password for the User 303 Password flagext.Secret `yaml:"password"` 304 305 // UseTLS sets whether TLS is used with SASL 306 UseTLS bool `yaml:"use_tls"` 307 308 // TLSConfig is used for SASL over TLS. It is used only when UseTLS is true 309 TLSConfig promconfig.TLSConfig `yaml:",inline"` 310 } 311 312 // GelfTargetConfig describes a scrape config that read GELF messages on UDP. 313 type GelfTargetConfig struct { 314 // ListenAddress is the address to listen on UDP for gelf messages. (Default to `:12201`) 315 ListenAddress string `yaml:"listen_address"` 316 317 // Labels optionally holds labels to associate with each record read from gelf messages. 318 Labels model.LabelSet `yaml:"labels"` 319 320 // UseIncomingTimestamp sets the timestamp to the incoming gelf messages 321 // timestamp if it's set. 322 UseIncomingTimestamp bool `yaml:"use_incoming_timestamp"` 323 } 324 325 type CloudflareConfig struct { 326 // APIToken is the API key for the Cloudflare account. 327 APIToken string `yaml:"api_token"` 328 // ZoneID is the ID of the zone to use. 329 ZoneID string `yaml:"zone_id"` 330 // Labels optionally holds labels to associate with each record read from cloudflare logs. 331 Labels model.LabelSet `yaml:"labels"` 332 // The amount of workers to use for parsing cloudflare logs. Default to 3. 333 Workers int `yaml:"workers"` 334 // The timerange to fetch for each pull request that will be spread across workers. Default 1m. 335 PullRange model.Duration `yaml:"pull_range"` 336 // Fields to fetch from cloudflare logs. 337 // Default to default fields. 338 // Available fields type: 339 // - default 340 // - minimal 341 // - extended 342 // - all 343 FieldsType string `yaml:"fields_type"` 344 } 345 346 // GcplogTargetConfig describes a scrape config to pull logs from any pubsub topic. 347 type GcplogTargetConfig struct { 348 // ProjectID is the Cloud project id 349 ProjectID string `yaml:"project_id"` 350 351 // Subscription is the subscription name we use to pull logs from a pubsub topic. 352 Subscription string `yaml:"subscription"` 353 354 // Labels are the additional labels to be added to log entry while pushing it to Loki server. 355 Labels model.LabelSet `yaml:"labels"` 356 357 // UseIncomingTimestamp represents whether to keep the timestamp same as actual log entry coming in or replace it with 358 // current timestamp at the time of processing. 359 // Its default value(`false`) denotes, replace it with current timestamp at the time of processing. 360 UseIncomingTimestamp bool `yaml:"use_incoming_timestamp"` 361 362 // SubscriptionType decides if the target works with a `pull` or `push` subscription type. 363 // Defaults to `pull` for backwards compatibility reasons. 364 SubscriptionType string `yaml:"subscription_type"` 365 366 // Server is the weaveworks server config for listening connections. Used just for `push` subscription type. 367 Server server.Config `yaml:"server"` 368 } 369 370 // HerokuDrainTargetConfig describes a scrape config to listen and consume heroku logs, in the HTTPS drain manner. 371 type HerokuDrainTargetConfig struct { 372 // Server is the weaveworks server config for listening connections 373 Server server.Config `yaml:"server"` 374 375 // Labels optionally holds labels to associate with each record received on the push api. 376 Labels model.LabelSet `yaml:"labels"` 377 378 // UseIncomingTimestamp sets the timestamp to the incoming heroku log entry timestamp. If false, 379 // promtail will assign the current timestamp to the log entry when it was processed. 380 UseIncomingTimestamp bool `yaml:"use_incoming_timestamp"` 381 } 382 383 // PushTargetConfig describes a scrape config that listens for Loki push messages. 384 type PushTargetConfig struct { 385 // Server is the weaveworks server config for listening connections 386 Server server.Config `yaml:"server"` 387 388 // Labels optionally holds labels to associate with each record received on the push api. 389 Labels model.LabelSet `yaml:"labels"` 390 391 // If promtail should maintain the incoming log timestamp or replace it with the current time. 392 KeepTimestamp bool `yaml:"use_incoming_timestamp"` 393 } 394 395 // DefaultScrapeConfig is the default Config. 396 var DefaultScrapeConfig = Config{ 397 PipelineStages: stages.PipelineStages{}, 398 } 399 400 // HasServiceDiscoveryConfig checks to see if the service discovery used for 401 // file targets is non-zero. 402 func (c *Config) HasServiceDiscoveryConfig() bool { 403 return !reflect.DeepEqual(c.ServiceDiscoveryConfig, ServiceDiscoveryConfig{}) 404 } 405 406 // UnmarshalYAML implements the yaml.Unmarshaler interface. 407 func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { 408 *c = DefaultScrapeConfig 409 410 type plain Config 411 if err := unmarshal((*plain)(c)); err != nil { 412 return err 413 } 414 415 if len(c.JobName) == 0 { 416 return fmt.Errorf("job_name is empty") 417 } 418 419 return nil 420 }