Skip to content
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

[receiver/httpcheck] add metrics check whether substring contains in response body #27015

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/httpcheckreceiver-match-content.yaml
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: httpcheckreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add metrics testing if the HTTP response body exact matches a specific string.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24913]

# (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]
2 changes: 2 additions & 0 deletions receiver/httpcheckreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Each target has the following properties:

- `endpoint` (required): the URL to be monitored
- `method` (optional, default: `GET`): The HTTP method used to call the endpoint
- `body` (optional, default: ""): If set, the receiver will emit metrics based on whether the response body exact matches this string.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could carry a different meaning when method is POST where the body content will be sent as part of the request.

- `name` (optional, default: ""): The name of this receiver.

Additionally, each target supports the client configuration options of [confighttp].

Expand Down
2 changes: 2 additions & 0 deletions receiver/httpcheckreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Config struct {
type targetConfig struct {
confighttp.HTTPClientSettings `mapstructure:",squash"`
Method string `mapstructure:"method"`
Body string `mapstructure:"body"`
Name string `mapstructure:"name"`
}

// Validate validates the configuration by checking for missing or invalid fields
Expand Down
25 changes: 25 additions & 0 deletions receiver/httpcheckreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,28 @@ Records errors occurring during HTTP check.
| http.status_code | HTTP response status code | Any Int |
| http.method | HTTP request method | Any Str |
| http.status_class | HTTP response status class | Any Str |

## Optional Metrics

The following metrics are not emitted by default. Each of them can be enabled by applying the following configuration:

```yaml
metrics:
<metric_name>:
enabled: true
```

### httpcheck.body

Reports 1 if the HTTP response body exact matches the `body` configuration item, 0 otherwise.

| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| 1 | Sum | Int | Cumulative | false |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| http.url | Full HTTP request URL. | Any Str |
| name | Optional name of this check. | Any Str |

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.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
default:
all_set:
metrics:
httpcheck.body:
enabled: true
httpcheck.duration:
enabled: true
httpcheck.error:
Expand All @@ -9,6 +11,8 @@ all_set:
enabled: true
none_set:
metrics:
httpcheck.body:
enabled: false
httpcheck.duration:
enabled: false
httpcheck.error:
Expand Down
12 changes: 12 additions & 0 deletions receiver/httpcheckreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ attributes:
error.message:
description: Error message recorded during check
type: string
name:
description: Optional name of this check.
type: string
atoulme marked this conversation as resolved.
Show resolved Hide resolved

metrics:
httpcheck.status:
Expand Down Expand Up @@ -54,6 +57,15 @@ metrics:
monotonic: false
unit: "{error}"
attributes: [http.url, error.message]
httpcheck.body:
description: Reports 1 if the HTTP response body exact matches the `body` configuration item, 0 otherwise.
enabled: false
sum:
value_type: int
aggregation_temporality: cumulative
monotonic: false
unit: 1
attributes: [http.url, name]

tests:
config:
19 changes: 18 additions & 1 deletion receiver/httpcheckreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package httpcheckreceiver // import "github.com/open-telemetry/opentelemetry-col
import (
"context"
"errors"
"io"
"net/http"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -62,7 +64,7 @@ func (h *httpcheckScraper) scrape(ctx context.Context) (pmetric.Metrics, error)

req, err := http.NewRequestWithContext(ctx, h.cfg.Targets[targetIndex].Method, h.cfg.Targets[targetIndex].Endpoint, http.NoBody)
if err != nil {
h.settings.Logger.Error("failed to create request", zap.Error(err))
h.settings.Logger.Error("failed to create request", zap.String("target endpoint", h.cfg.Targets[targetIndex].Endpoint), zap.Error(err))
return
}

Expand All @@ -85,6 +87,21 @@ func (h *httpcheckScraper) scrape(ctx context.Context) (pmetric.Metrics, error)
h.mb.RecordHttpcheckStatusDataPoint(now, int64(0), h.cfg.Targets[targetIndex].Endpoint, int64(statusCode), req.Method, class)
}
}

if err == nil && len(h.cfg.Targets[targetIndex].Body) > 0 {
if body, err := io.ReadAll(resp.Body); err != nil {
h.settings.Logger.Error("failed to read response", zap.String("target endpoint", h.cfg.Targets[targetIndex].Endpoint), zap.Error(err))
} else {
defer resp.Body.Close()

if strings.Contains(string(body), h.cfg.Targets[targetIndex].Body) {
h.mb.RecordHttpcheckBodyDataPoint(now, int64(1), h.cfg.Targets[targetIndex].Endpoint, h.cfg.Targets[targetIndex].Name)
} else {
h.mb.RecordHttpcheckBodyDataPoint(now, int64(0), h.cfg.Targets[targetIndex].Endpoint, h.cfg.Targets[targetIndex].Name)
}
}
}

mux.Unlock()
}(client, idx)
}
Expand Down
Loading