-
Notifications
You must be signed in to change notification settings - Fork 2
Push host metadata on startup #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5a715dc
95a4c5c
61d67fa
dc36058
e29dd46
1314caa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,12 @@ | |
| package datadogexporter | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "go.opentelemetry.io/collector/consumer/pdata" | ||
| "go.uber.org/zap" | ||
|
|
@@ -26,23 +31,47 @@ type metricsExporter struct { | |
| logger *zap.Logger | ||
| cfg *Config | ||
| client *datadog.Client | ||
| tags []string | ||
| } | ||
|
|
||
| func newMetricsExporter(logger *zap.Logger, cfg *Config) (*metricsExporter, error) { | ||
| client := datadog.NewClient(cfg.API.Key, "") | ||
| client.SetBaseUrl(cfg.Metrics.TCPAddr.Endpoint) | ||
|
|
||
| // Calculate tags at startup | ||
| tags := cfg.TagsConfig.GetTags(false) | ||
| return &metricsExporter{logger, cfg, client}, nil | ||
| } | ||
|
|
||
| // pushHostMetadata sends a host metadata payload to the "/intake" endpoint | ||
| func (exp *metricsExporter) pushHostMetadata(metadata hostMetadata) error { | ||
| path := exp.cfg.Metrics.TCPAddr.Endpoint + "/intake" | ||
| buf, _ := json.Marshal(metadata) | ||
| req, _ := http.NewRequest(http.MethodPost, path, bytes.NewBuffer(buf)) | ||
| req.Header.Set("DD-API-KEY", exp.cfg.API.Key) | ||
| req.Header.Set("Content-Type", "application/json") | ||
mx-psi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| client := &http.Client{Timeout: 10 * time.Second} | ||
|
||
| resp, err := client.Do(req) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| defer resp.Body.Close() | ||
|
|
||
| return &metricsExporter{logger, cfg, client, tags}, nil | ||
| if resp.StatusCode >= 400 { | ||
| return fmt.Errorf( | ||
| "'%d - %s' error when sending metadata payload to %s", | ||
| resp.StatusCode, | ||
| resp.Status, | ||
| path, | ||
| ) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (exp *metricsExporter) processMetrics(metrics []datadog.Metric) { | ||
| addNamespace := exp.cfg.Metrics.Namespace != "" | ||
| overrideHostname := exp.cfg.Hostname != "" | ||
| addTags := len(exp.tags) > 0 | ||
|
|
||
| for i := range metrics { | ||
| if addNamespace { | ||
|
|
@@ -53,11 +82,6 @@ func (exp *metricsExporter) processMetrics(metrics []datadog.Metric) { | |
| if overrideHostname || metrics[i].GetHost() == "" { | ||
| metrics[i].Host = GetHost(exp.cfg) | ||
| } | ||
|
|
||
| if addTags { | ||
| metrics[i].Tags = append(metrics[i].Tags, exp.tags...) | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be done separately/tracked as an improvement, but ideally this should also send a host metadata payload every 30 minutes, so that:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will do this separately, right now it doesn't change but I want to add support for EC2 tags so this makes sense. Thanks!