-
Notifications
You must be signed in to change notification settings - Fork 35
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
Integrate collector with new metadata api #858
Changes from 33 commits
604c35e
6d41b38
48dd1a4
a2e614a
9721c1f
50b7943
23cff5a
36bbd10
3e0ba59
c0cdb3c
579472b
ed15a8a
0178f36
88597f3
b8313c6
5111f9c
3c5b28d
32c9738
3e2c33d
e4f907f
070b226
d8c09f0
a2fe469
f969d6c
45869b6
aebdaac
53767ea
e4cef4a
b299ded
c19315a
49d47d6
ec4c28a
d8f213e
366bace
01f589a
f2d6458
354f86f
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright The OpenTelemetry 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 api | ||
|
||
type OpenMetadataHostDetails struct { | ||
Name string `json:"name"` | ||
OsName string `json:"osName"` | ||
OsVersion string `json:"osVersion"` | ||
Environment string `json:"environment"` | ||
} | ||
|
||
type OpenMetadataCollectorDetails struct { | ||
RunningVersion string `json:"runningVersion"` | ||
} | ||
|
||
type OpenMetadataNetworkDetails struct { | ||
HostIpAddress string `json:"hostIpAddress"` | ||
} | ||
|
||
type OpenMetadataRequestPayload struct { | ||
HostDetails OpenMetadataHostDetails `json:"hostDetails"` | ||
CollectorDetails OpenMetadataCollectorDetails `json:"collectorDetails"` | ||
NetworkDetails OpenMetadataNetworkDetails `json:"networkDetails"` | ||
TagDetails map[string]interface{} `json:"tagDetails"` | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
@@ -31,9 +32,11 @@ import ( | |
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/google/uuid" | ||
"github.com/shirou/gopsutil/v3/host" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/extension/auth" | ||
"go.opentelemetry.io/collector/featuregate" | ||
"go.uber.org/zap" | ||
grpccredentials "google.golang.org/grpc/credentials" | ||
|
||
|
@@ -57,6 +60,7 @@ type SumologicExtension struct { | |
hashKey string | ||
httpClient *http.Client | ||
registrationInfo api.OpenRegisterResponsePayload | ||
updateMetadata bool | ||
|
||
closeChan chan struct{} | ||
closeOnce sync.Once | ||
|
@@ -66,6 +70,7 @@ type SumologicExtension struct { | |
|
||
const ( | ||
heartbeatUrl = "/api/v1/collector/heartbeat" | ||
metadataUrl = "/api/v1/otCollectors/metadata" | ||
registerUrl = "/api/v1/collector/register" | ||
|
||
collectorIdField = "collector_id" | ||
|
@@ -74,9 +79,21 @@ const ( | |
) | ||
|
||
const ( | ||
updateCollectorMetadataID = "extension.sumologic.updateCollectorMetadata" | ||
updateCollectorMetadataStage = featuregate.StageAlpha | ||
|
||
DefaultHeartbeatInterval = 15 * time.Second | ||
) | ||
|
||
func init() { | ||
featuregate.GetRegistry().MustRegisterID( | ||
updateCollectorMetadataID, | ||
updateCollectorMetadataStage, | ||
featuregate.WithRegisterDescription("When enabled, the collector will updated its Sumo Logic metadata."), | ||
portertech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
featuregate.WithRegisterReferenceURL("https://github.com/SumoLogic/sumologic-otel-collector/pull/858"), | ||
) | ||
} | ||
|
||
var errGRPCNotSupported = fmt.Errorf("gRPC is not supported by sumologicextension") | ||
|
||
// SumologicExtension implements ClientAuthenticator | ||
|
@@ -134,6 +151,7 @@ func newSumologicExtension(conf *Config, logger *zap.Logger, id component.ID) (* | |
logger: logger, | ||
hashKey: hashKey, | ||
credentialsStore: credentialsStore, | ||
updateMetadata: featuregate.GetRegistry().IsEnabled(updateCollectorMetadataID), | ||
closeChan: make(chan struct{}), | ||
backOff: backOff, | ||
id: id, | ||
|
@@ -166,6 +184,13 @@ func (se *SumologicExtension) Start(ctx context.Context, host component.Host) er | |
zap.String(collectorIdField, colCreds.Credentials.CollectorId), | ||
) | ||
|
||
if se.updateMetadata { | ||
err = se.updateMetadataWithBackoff(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
go se.heartbeatLoop() | ||
|
||
return nil | ||
|
@@ -539,6 +564,7 @@ func (se *SumologicExtension) heartbeatLoop() { | |
} | ||
|
||
var errUnauthorizedHeartbeat = errors.New("heartbeat unauthorized") | ||
var errUnauthorizedMetadata = errors.New("metadata update unauthorized") | ||
|
||
type ErrorAPI struct { | ||
status int | ||
|
@@ -592,6 +618,135 @@ func (se *SumologicExtension) sendHeartbeatWithHTTPClient(ctx context.Context, h | |
return nil | ||
} | ||
|
||
func getHostIpAddress() (string, error) { | ||
c, err := net.Dial("udp", "8.8.8.8:53") | ||
portertech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
defer c.Close() | ||
a := c.LocalAddr().(*net.UDPAddr) | ||
h, _, err := net.SplitHostPort(a.String()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return h, nil | ||
} | ||
|
||
func (se *SumologicExtension) updateMetadataWithHTTPClient(ctx context.Context, httpClient *http.Client) error { | ||
u, err := url.Parse(se.BaseUrl() + metadataUrl) | ||
if err != nil { | ||
return fmt.Errorf("unable to parse metadata URL %w", err) | ||
} | ||
|
||
info, err := host.Info() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ip, err := getHostIpAddress() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
td := se.conf.CollectorFields | ||
if td == nil { | ||
td = map[string]interface{}{} | ||
} | ||
|
||
var buff bytes.Buffer | ||
if err = json.NewEncoder(&buff).Encode(api.OpenMetadataRequestPayload{ | ||
HostDetails: api.OpenMetadataHostDetails{ | ||
Name: info.Hostname, | ||
OsName: info.OS, | ||
OsVersion: info.PlatformVersion, | ||
Environment: se.conf.CollectorEnvironment, | ||
}, | ||
CollectorDetails: api.OpenMetadataCollectorDetails{ | ||
RunningVersion: "1.0.0", | ||
portertech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
NetworkDetails: api.OpenMetadataNetworkDetails{ | ||
HostIpAddress: ip, | ||
}, | ||
TagDetails: td, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), &buff) | ||
if err != nil { | ||
return fmt.Errorf("unable to create HTTP request %w", err) | ||
} | ||
|
||
addJSONHeaders(req) | ||
|
||
se.logger.Info("Updating collector metadata", | ||
zap.String("URL", u.String()), | ||
zap.String("body", buff.String())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're going to log this, we should make sure that it's human-readable. Setting an indentation level on the encoder should suffice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zap String() doesn't pretty print it as expected, for example: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logging is really for our own testing, it should moved to debug or be removed when we remove the feature gate. |
||
|
||
res, err := httpClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("unable to send HTTP request: %w", err) | ||
} | ||
defer res.Body.Close() | ||
|
||
switch res.StatusCode { | ||
default: | ||
var buff bytes.Buffer | ||
if _, err := io.Copy(&buff, res.Body); err != nil { | ||
return fmt.Errorf( | ||
"failed to copy collector metadata response body, status code: %d, err: %w", | ||
res.StatusCode, err, | ||
) | ||
} | ||
|
||
se.logger.Warn("Metadata API error response", | ||
zap.Int("status", res.StatusCode), | ||
zap.String("body", buff.String())) | ||
|
||
return fmt.Errorf("collector metadata request failed: %w", | ||
ErrorAPI{ | ||
status: res.StatusCode, | ||
body: buff.String(), | ||
}, | ||
) | ||
|
||
case http.StatusUnauthorized: | ||
return errUnauthorizedMetadata | ||
|
||
case http.StatusNoContent: | ||
portertech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case http.StatusOK: | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (se *SumologicExtension) updateMetadataWithBackoff(ctx context.Context) error { | ||
se.backOff.Reset() | ||
for { | ||
err := se.updateMetadataWithHTTPClient(ctx, se.httpClient) | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
nbo := se.backOff.NextBackOff() | ||
// Return error if backoff reaches the limit or uncoverable error is spotted | ||
if _, ok := err.(*backoff.PermanentError); nbo == se.backOff.Stop || ok { | ||
return fmt.Errorf("collector metadata update failed: %w", err) | ||
} | ||
|
||
t := time.NewTimer(nbo) | ||
defer t.Stop() | ||
|
||
select { | ||
case <-t.C: | ||
case <-ctx.Done(): | ||
return fmt.Errorf("collector metadata update cancelled: %w", ctx.Err()) | ||
} | ||
} | ||
} | ||
|
||
func (se *SumologicExtension) ComponentID() component.ID { | ||
return se.id | ||
} | ||
|
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.
Are we going to change api in the futher? Do we need any kind of versioning?
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 don't believe so, there may be additional fields. I used registration for reference.