-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
… host
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: hostmetricsreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "allow configuring log pipeline to send host EntityState event" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [33927] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package hostmetricsreceiver | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/receiver" | ||
"go.uber.org/zap" | ||
|
||
metadataPkg "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent" | ||
) | ||
|
||
const entityType = "host" | ||
|
||
type hostMetricsReceiver struct { | ||
cfg *Config | ||
|
||
scraperHelper component.Component | ||
nextLogs consumer.Logs | ||
shutdownWG sync.WaitGroup | ||
cancel context.CancelFunc | ||
|
||
settings *receiver.Settings | ||
} | ||
|
||
func (hmr *hostMetricsReceiver) Start(ctx context.Context, host component.Host) error { | ||
ctx, hmr.cancel = context.WithCancel(ctx) | ||
if hmr.nextLogs != nil { | ||
hmr.sendEntityEvent(ctx) | ||
if hmr.cfg.MetadataCollectionInterval != 0 { | ||
ticker := time.NewTicker(hmr.cfg.MetadataCollectionInterval) | ||
go func() { | ||
for { | ||
select { | ||
case <-ticker.C: | ||
hmr.sendEntityEvent(ctx) | ||
case <-ctx.Done(): | ||
ticker.Stop() | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
} | ||
|
||
if hmr.scraperHelper != nil { | ||
return hmr.scraperHelper.Start(ctx, host) | ||
} | ||
return nil | ||
} | ||
|
||
func (hmr *hostMetricsReceiver) Shutdown(ctx context.Context) error { | ||
if hmr.cancel != nil { | ||
hmr.cancel() | ||
} | ||
if hmr.scraperHelper != nil { | ||
return hmr.scraperHelper.Shutdown(ctx) | ||
} | ||
return nil | ||
} | ||
|
||
func newHostMetricsReceiver(cfg *Config, set *receiver.Settings) (*hostMetricsReceiver, error) { | ||
r := &hostMetricsReceiver{ | ||
cfg: cfg, | ||
nextLogs: nil, | ||
settings: set, | ||
} | ||
|
||
return r, nil | ||
} | ||
|
||
func (hmr *hostMetricsReceiver) sendEntityEvent(ctx context.Context) { | ||
timestamp := pcommon.NewTimestampFromTime(time.Now()) | ||
|
||
out := metadataPkg.NewEntityEventsSlice() | ||
entityEvent := out.AppendEmpty() | ||
entityEvent.SetTimestamp(timestamp) | ||
state := entityEvent.SetEntityState() | ||
state.SetEntityType(entityType) | ||
|
||
logs := out.ConvertAndMoveToLogs() | ||
|
||
err := hmr.nextLogs.ConsumeLogs(ctx, logs) | ||
if err != nil { | ||
hmr.settings.Logger.Error("Error sending entity event to the consumer", zap.Error(err)) | ||
} | ||
|
||
// Note: receiver contract says that we need to retry sending if the | ||
// returned error is not Permanent. However, we are not doing it here. | ||
// Instead, we rely on the fact the metadata is collected periodically | ||
// and the entity events will be delivered on the next cycle. This is | ||
// fine because we deliver cumulative entity state. | ||
// This allows us to avoid stressing the Collector or its destination | ||
// unnecessarily (typically non-Permanent errors happen in stressed conditions). | ||
} | ||
|
||
// This is the map of already created hostmetric receivers for particular configurations. | ||
// We maintain this map because the Factory is asked log and metric receivers separately | ||
// when it gets CreateLogsReceiver() and CreateMetricsReceiver() but they must not | ||
// create separate objects, they must use one receiver object per configuration. | ||
var receivers = sharedcomponent.NewSharedComponents() |