Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
telegraf-nftables
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Due to inactivity, this project is scheduled to be deleted on 2035-04-24.
Why is this scheduled?
Show more breadcrumbs
vqgroup
telegraf-nftables
Commits
d6b5f3ef
Commit
d6b5f3ef
authored
9 years ago
by
Jonathan Chauncey
Committed by
Cameron Sparr
9 years ago
Browse files
Options
Downloads
Patches
Plain Diff
fix(prometheus): Add support for bearer token to prometheus input plugin
closes #864 merges #880
parent
b5a43162
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CHANGELOG.md
+1
-0
1 addition, 0 deletions
CHANGELOG.md
plugins/inputs/prometheus/prometheus.go
+44
-7
44 additions, 7 deletions
plugins/inputs/prometheus/prometheus.go
with
45 additions
and
7 deletions
CHANGELOG.md
+
1
−
0
View file @
d6b5f3ef
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
### Features
### Features
-
[
#863
](
https://github.com/influxdata/telegraf/pull/863
)
: AMQP output: allow external auth. Thanks @ekini!
-
[
#863
](
https://github.com/influxdata/telegraf/pull/863
)
: AMQP output: allow external auth. Thanks @ekini!
-
[
#707
](
https://github.com/influxdata/telegraf/pull/707
)
: Improved prometheus plugin. Thanks @titilambert!
-
[
#707
](
https://github.com/influxdata/telegraf/pull/707
)
: Improved prometheus plugin. Thanks @titilambert!
-
[
#880
](
https://github.com/influxdata/telegraf/pull/880
)
: Add the ability to specify the bearer token to the prometheus plugin. Thanks @jchauncey!
### Bugfixes
### Bugfixes
...
...
This diff is collapsed.
Click to expand it.
plugins/inputs/prometheus/prometheus.go
+
44
−
7
View file @
d6b5f3ef
package
prometheus
package
prometheus
import
(
import
(
"crypto/tls"
"errors"
"errors"
"fmt"
"fmt"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"github.com/influxdata/telegraf/plugins/inputs"
"io/ioutil"
"io/ioutil"
"net"
"net/http"
"net/http"
"sync"
"sync"
"time"
"time"
...
@@ -13,18 +15,28 @@ import (
...
@@ -13,18 +15,28 @@ import (
type
Prometheus
struct
{
type
Prometheus
struct
{
Urls
[]
string
Urls
[]
string
// Use SSL but skip chain & host verification
InsecureSkipVerify
bool
// Bearer Token authorization file path
BearerToken
string
`toml:"bearer_token"`
}
}
var
sampleConfig
=
`
var
sampleConfig
=
`
## An array of urls to scrape metrics from.
## An array of urls to scrape metrics from.
urls = ["http://localhost:9100/metrics"]
urls = ["http://localhost:9100/metrics"]
### Use SSL but skip chain & host verification
# insecure_skip_verify = false
### Use bearer token for authorization
# bearer_token = /path/to/bearer/token
`
`
func
(
r
*
Prometheus
)
SampleConfig
()
string
{
func
(
p
*
Prometheus
)
SampleConfig
()
string
{
return
sampleConfig
return
sampleConfig
}
}
func
(
r
*
Prometheus
)
Description
()
string
{
func
(
p
*
Prometheus
)
Description
()
string
{
return
"Read metrics from one or many prometheus clients"
return
"Read metrics from one or many prometheus clients"
}
}
...
@@ -32,16 +44,16 @@ var ErrProtocolError = errors.New("prometheus protocol error")
...
@@ -32,16 +44,16 @@ var ErrProtocolError = errors.New("prometheus protocol error")
// Reads stats from all configured servers accumulates stats.
// Reads stats from all configured servers accumulates stats.
// Returns one of the errors encountered while gather stats (if any).
// Returns one of the errors encountered while gather stats (if any).
func
(
g
*
Prometheus
)
Gather
(
acc
telegraf
.
Accumulator
)
error
{
func
(
p
*
Prometheus
)
Gather
(
acc
telegraf
.
Accumulator
)
error
{
var
wg
sync
.
WaitGroup
var
wg
sync
.
WaitGroup
var
outerr
error
var
outerr
error
for
_
,
serv
:=
range
g
.
Urls
{
for
_
,
serv
:=
range
p
.
Urls
{
wg
.
Add
(
1
)
wg
.
Add
(
1
)
go
func
(
serv
string
)
{
go
func
(
serv
string
)
{
defer
wg
.
Done
()
defer
wg
.
Done
()
outerr
=
g
.
gatherURL
(
serv
,
acc
)
outerr
=
p
.
gatherURL
(
serv
,
acc
)
}(
serv
)
}(
serv
)
}
}
...
@@ -59,9 +71,34 @@ var client = &http.Client{
...
@@ -59,9 +71,34 @@ var client = &http.Client{
Timeout
:
time
.
Duration
(
4
*
time
.
Second
),
Timeout
:
time
.
Duration
(
4
*
time
.
Second
),
}
}
func
(
g
*
Prometheus
)
gatherURL
(
url
string
,
acc
telegraf
.
Accumulator
)
error
{
func
(
p
*
Prometheus
)
gatherURL
(
url
string
,
acc
telegraf
.
Accumulator
)
error
{
collectDate
:=
time
.
Now
()
collectDate
:=
time
.
Now
()
resp
,
err
:=
client
.
Get
(
url
)
var
req
,
err
=
http
.
NewRequest
(
"GET"
,
url
,
nil
)
req
.
Header
=
make
(
http
.
Header
)
var
token
[]
byte
var
resp
*
http
.
Response
var
rt
http
.
RoundTripper
=
&
http
.
Transport
{
Dial
:
(
&
net
.
Dialer
{
Timeout
:
10
*
time
.
Second
,
KeepAlive
:
30
*
time
.
Second
,
})
.
Dial
,
TLSHandshakeTimeout
:
10
*
time
.
Second
,
TLSClientConfig
:
&
tls
.
Config
{
InsecureSkipVerify
:
p
.
InsecureSkipVerify
,
},
ResponseHeaderTimeout
:
time
.
Duration
(
3
*
time
.
Second
),
}
if
p
.
BearerToken
!=
""
{
token
,
err
=
ioutil
.
ReadFile
(
p
.
BearerToken
)
if
err
!=
nil
{
return
err
}
req
.
Header
.
Set
(
"Authorization"
,
"Bearer "
+
string
(
token
))
}
resp
,
err
=
rt
.
RoundTrip
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"error making HTTP request to %s: %s"
,
url
,
err
)
return
fmt
.
Errorf
(
"error making HTTP request to %s: %s"
,
url
,
err
)
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment