forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The HTTP Check Receiver can be used for synthethic checks against HTTP endpoints. This receiver will make a request to the specified `endpoint` using the configured `method`. Fixes #10607
- Loading branch information
Alex Boten
committed
Oct 14, 2022
1 parent
b846f2d
commit 401fcca
Showing
24 changed files
with
1,693 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: new_component | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: httpcheckreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "New HTTP Check receiver allows users to run synthethic checks from the collector" | ||
|
||
# One or more tracking issues related to the change | ||
issues: [10607] | ||
|
||
# (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: |
Validating CODEOWNERS rules …
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
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 @@ | ||
include ../../Makefile.Common |
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,37 @@ | ||
# HTTP Check Receiver | ||
|
||
| Status | | | ||
| ------------------------ |------------------| | ||
| Stability | [in development] | | ||
| Supported pipeline types | metrics | | ||
| Distributions | none | | ||
|
||
The HTTP Check Receiver can be used for synthethic checks against HTTP endpoints. This receiver will make a request to the specified `endpoint` using the configured `method`. | ||
|
||
## Configuration | ||
|
||
The following configuration settings are required: | ||
|
||
- `endpoint`: The URL of the endpoint to be monitored. | ||
|
||
The following configuration settings are optional: | ||
|
||
- `method` (default: `GET`): The method used to call the endpoint. | ||
- `collection_interval` (default = `60s`): This receiver collects metrics on an interval. Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`. | ||
|
||
### Example Configuration | ||
|
||
```yaml | ||
receivers: | ||
http_check: | ||
endpoint: http://endpoint:80 | ||
method: GET | ||
collection_interval: 10s | ||
``` | ||
## Metrics | ||
Details about the metrics produced by this receiver can be found in [documentation.md](./documentation.md) | ||
[in development]: https://github.com/open-telemetry/opentelemetry-collector#in-development | ||
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib |
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,55 @@ | ||
// 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 httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
|
||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/receiver/scraperhelper" | ||
"go.uber.org/multierr" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver/internal/metadata" | ||
) | ||
|
||
// Predefined error responses for configuration validation failures | ||
var ( | ||
errInvalidEndpoint = errors.New(`"endpoint" must be in the form of <scheme>://<hostname>:<port>`) | ||
) | ||
|
||
const defaultEndpoint = "http://localhost:80" | ||
|
||
// Config defines the configuration for the various elements of the receiver agent. | ||
type Config struct { | ||
scraperhelper.ScraperControllerSettings `mapstructure:",squash"` | ||
confighttp.HTTPClientSettings `mapstructure:",squash"` | ||
Metrics metadata.MetricsSettings `mapstructure:"metrics"` | ||
Method string `mapstructure:"method"` | ||
} | ||
|
||
// Validate validates the configuration by checking for missing or invalid fields | ||
func (cfg *Config) Validate() error { | ||
var err error | ||
|
||
_, parseErr := url.Parse(cfg.Endpoint) | ||
if parseErr != nil { | ||
wrappedErr := fmt.Errorf("%s: %w", errInvalidEndpoint.Error(), parseErr) | ||
err = multierr.Append(err, wrappedErr) | ||
} | ||
|
||
return err | ||
} |
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,66 @@ | ||
// 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 httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver" | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
func TestValidate(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
cfg *Config | ||
expectedErr error | ||
}{ | ||
{ | ||
desc: "invalid endpoint", | ||
cfg: &Config{ | ||
HTTPClientSettings: confighttp.HTTPClientSettings{ | ||
Endpoint: "invalid://endpoint: 12efg", | ||
}, | ||
}, | ||
expectedErr: multierr.Combine( | ||
fmt.Errorf("%s: %w", errInvalidEndpoint, errors.New(`parse "invalid://endpoint: 12efg": invalid port ": 12efg" after host`)), | ||
), | ||
}, | ||
{ | ||
desc: "valid config", | ||
cfg: &Config{ | ||
HTTPClientSettings: confighttp.HTTPClientSettings{ | ||
Endpoint: defaultEndpoint, | ||
}, | ||
}, | ||
expectedErr: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
actualErr := tc.cfg.Validate() | ||
if tc.expectedErr != nil { | ||
require.EqualError(t, actualErr, tc.expectedErr.Error()) | ||
} else { | ||
require.NoError(t, actualErr) | ||
} | ||
|
||
}) | ||
} | ||
} |
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,17 @@ | ||
// 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. | ||
|
||
//go:generate mdatagen metadata.yaml | ||
|
||
package httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver" |
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,29 @@ | ||
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.) | ||
|
||
# httpcheckreceiver | ||
|
||
## Metrics | ||
|
||
These are the metrics available for this scraper. | ||
|
||
| Name | Description | Unit | Type | Attributes | | ||
| ---- | ----------- | ---- | ---- | ---------- | | ||
| **httpcheck.duration** | Measures the duration of the HTTP check. | ms | Gauge(Int) | <ul> <li>http.url</li> </ul> | | ||
| **httpcheck.status** | 1 if the endpoint resulted in status http.status_code, otherwise 0. | 1 | Sum(Int) | <ul> <li>http.url</li> <li>http.status_code</li> <li>http.method</li> </ul> | | ||
|
||
**Highlighted metrics** are emitted by default. Other metrics are optional and not emitted by default. | ||
Any metric can be enabled or disabled with the following scraper configuration: | ||
|
||
```yaml | ||
metrics: | ||
<metric_name>: | ||
enabled: <true|false> | ||
``` | ||
## Metric attributes | ||
| Name | Description | Values | | ||
| ---- | ----------- | ------ | | ||
| http.method | HTTP request method | | | ||
| http.status_code | HTTP response status code | | | ||
| http.url | Full HTTP request URL. | | |
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,74 @@ | ||
// 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 httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver" | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/receiver/scraperhelper" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/httpcheckreceiver/internal/metadata" | ||
) | ||
|
||
const ( | ||
typeStr = "httpcheck" | ||
stability = component.StabilityLevelInDevelopment | ||
) | ||
|
||
var errConfigNotHTTPCheck = errors.New("config was not a HTTP check receiver config") | ||
|
||
// NewFactory creates a new receiver factory | ||
func NewFactory() component.ReceiverFactory { | ||
return component.NewReceiverFactory( | ||
typeStr, | ||
createDefaultConfig, | ||
component.WithMetricsReceiver(createMetricsReceiver, stability)) | ||
} | ||
|
||
func createDefaultConfig() config.Receiver { | ||
return &Config{ | ||
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{ | ||
ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)), | ||
CollectionInterval: 10 * time.Second, | ||
}, | ||
HTTPClientSettings: confighttp.HTTPClientSettings{ | ||
Endpoint: defaultEndpoint, | ||
Timeout: 10 * time.Second, | ||
}, | ||
Metrics: metadata.DefaultMetricsSettings(), | ||
Method: "GET", | ||
} | ||
} | ||
|
||
func createMetricsReceiver(ctx context.Context, params component.ReceiverCreateSettings, rConf config.Receiver, consumer consumer.Metrics) (component.MetricsReceiver, error) { | ||
cfg, ok := rConf.(*Config) | ||
if !ok { | ||
return nil, errConfigNotHTTPCheck | ||
} | ||
|
||
httpcheckScraper := newScraper(cfg, params) | ||
scraper, err := scraperhelper.NewScraper(typeStr, httpcheckScraper.scrape, scraperhelper.WithStart(httpcheckScraper.start)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return scraperhelper.NewScraperControllerReceiver(&cfg.ScraperControllerSettings, params, consumer, scraperhelper.AddScraper(scraper)) | ||
} |
Oops, something went wrong.