-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/ntp] add initial implementation (#35850)
#### Description Adds initial implementation of ntpreceiver. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue #34375
- Loading branch information
Showing
12 changed files
with
233 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ntpreceiver | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/receiver/scraperhelper" | ||
) | ||
|
||
func TestValidate(t *testing.T) { | ||
for _, tt := range []struct { | ||
name string | ||
c *Config | ||
errorExpected string | ||
}{ | ||
{ | ||
name: "no host", | ||
c: &Config{ | ||
Version: 4, | ||
Endpoint: "", | ||
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: 45 * time.Minute}, | ||
}, | ||
errorExpected: "missing port in address", | ||
}, | ||
{ | ||
name: "no port", | ||
c: &Config{ | ||
Version: 4, | ||
Endpoint: "pool.ntp.org", | ||
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: 45 * time.Minute}, | ||
}, | ||
errorExpected: "address pool.ntp.org: missing port in address", | ||
}, | ||
{ | ||
name: "valid", | ||
c: &Config{ | ||
Version: 4, | ||
Endpoint: "pool.ntp.org:123", | ||
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: 45 * time.Minute}, | ||
}, | ||
}, | ||
{ | ||
name: "interval too small", | ||
c: &Config{ | ||
Version: 4, | ||
Endpoint: "pool.ntp.org:123", | ||
ControllerConfig: scraperhelper.ControllerConfig{CollectionInterval: 29 * time.Minute}, | ||
}, | ||
errorExpected: "collection interval 29m0s is less than minimum 30m", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.c.Validate() | ||
if tt.errorExpected == "" { | ||
require.NoError(t, err) | ||
} else { | ||
require.EqualError(t, err, tt.errorExpected) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ntpreceiver | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateDefaultConfig(t *testing.T) { | ||
c := createDefaultConfig().(*Config) | ||
require.Equal(t, 4, c.Version) | ||
require.Equal(t, "pool.ntp.org:123", c.Endpoint) | ||
require.Equal(t, 30*time.Minute, c.CollectionInterval) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
receiver/ntpreceiver/internal/metadata/generated_metrics_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ntpreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ntpreceiver" | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/beevik/ntp" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/receiver" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/ntpreceiver/internal/metadata" | ||
) | ||
|
||
type scraper struct { | ||
logger *zap.Logger | ||
mb *metadata.MetricsBuilder | ||
version int | ||
timeout time.Duration | ||
endpoint string | ||
} | ||
|
||
func (s *scraper) scrape(context.Context) (pmetric.Metrics, error) { | ||
options := ntp.QueryOptions{Version: s.version, Timeout: s.timeout} | ||
response, err := ntp.QueryWithOptions(s.endpoint, options) | ||
if err != nil { | ||
return pmetric.Metrics{}, err | ||
} | ||
s.mb.RecordNtpOffsetDataPoint(pcommon.NewTimestampFromTime(time.Now()), response.ClockOffset.Nanoseconds()) | ||
s.mb.NewResourceBuilder().SetNtpHost(s.endpoint) | ||
return s.mb.Emit(), nil | ||
} | ||
|
||
func newScraper(cfg *Config, settings receiver.Settings) *scraper { | ||
return &scraper{ | ||
logger: settings.TelemetrySettings.Logger, | ||
mb: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings), | ||
version: cfg.Version, | ||
timeout: cfg.ControllerConfig.Timeout, | ||
endpoint: cfg.Endpoint, | ||
} | ||
} |