-
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 2 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") | ||
|
|
||
| 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/100 >= 4 { | ||
mx-psi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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...) | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.