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.
[receiver/riak] Riak Metric Receiver (open-telemetry#8548)
* Add riakreceiver
- Loading branch information
Showing
34 changed files
with
5,554 additions
and
1 deletion.
There are no files selected for viewing
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
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,36 @@ | ||
# Riak Receiver | ||
|
||
Riak metrics will be collected from the [/stats](https://docs.riak.com/riak/kv/2.2.3/developing/api/http/status.1.html) endpoint. | ||
|
||
This Riak receiver will collect metrics for [3.x+](https://github.com/basho/riak/releases) | ||
|
||
Supported pipeline types: `metrics` | ||
|
||
## Configuration | ||
|
||
The following configuration settings are required: | ||
|
||
- `username` | ||
- `password` | ||
|
||
The following configuration settings are optional: | ||
|
||
- `endpoint` (default: `http:https://localhost:8098`): The URL of the node to be monitored. | ||
- `collection_interval` (default = `60s`): This receiver collects metrics on an interval. Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`. | ||
- `tls` (defaults defined [here](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/README.md)): TLS control. By default insecure settings are rejected and certificate verification is on. | ||
|
||
### Example Configuration | ||
|
||
```yaml | ||
receivers: | ||
riak: | ||
endpoint: http:https://localhost:8098 | ||
username: otelu | ||
password: $RIAK_PASSWORD | ||
collection_interval: 60s | ||
``` | ||
|
||
## Metrics | ||
|
||
Details about the metrics produced by this receiver can be found in [metadata.yaml](./metadata.yaml) | ||
|
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,125 @@ | ||
// 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:https://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 riakreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver" | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/model" | ||
) | ||
|
||
// statsPath is the path to stats endpoint | ||
const statsPath = "/stats" | ||
|
||
type client interface { | ||
// GetStats calls "/stats" endpoint to get list of stats for the target node | ||
GetStats(ctx context.Context) (*model.Stats, error) | ||
} | ||
|
||
var _ client = (*riakClient)(nil) | ||
|
||
type riakClient struct { | ||
client *http.Client | ||
hostEndpoint string | ||
creds riakCredentials | ||
logger *zap.Logger | ||
} | ||
|
||
type riakCredentials struct { | ||
username string | ||
password string | ||
} | ||
|
||
func newClient(cfg *Config, host component.Host, settings component.TelemetrySettings, logger *zap.Logger) (client, error) { | ||
httpClient, err := cfg.ToClient(host.GetExtensions(), settings) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create HTTP Client: %w", err) | ||
} | ||
|
||
return &riakClient{ | ||
client: httpClient, | ||
hostEndpoint: cfg.Endpoint, | ||
creds: riakCredentials{ | ||
username: cfg.Username, | ||
password: cfg.Password, | ||
}, | ||
logger: logger, | ||
}, nil | ||
} | ||
|
||
func (c *riakClient) GetStats(ctx context.Context) (*model.Stats, error) { | ||
var stats *model.Stats | ||
|
||
if err := c.get(ctx, statsPath, &stats); err != nil { | ||
c.logger.Debug("Failed to retrieve stats", zap.Error(err)) | ||
return nil, err | ||
} | ||
|
||
return stats, nil | ||
} | ||
|
||
func (c *riakClient) get(ctx context.Context, path string, respObj interface{}) error { | ||
// Construct endpoint and create request | ||
url := c.hostEndpoint + path | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) | ||
if err != nil { | ||
return fmt.Errorf("failed to create get request for path %s: %w", path, err) | ||
} | ||
|
||
// Set user/pass authentication | ||
req.SetBasicAuth(c.creds.username, c.creds.password) | ||
|
||
// Make request | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to make http request: %w", err) | ||
} | ||
|
||
// Defer body close | ||
defer func() { | ||
if closeErr := resp.Body.Close(); closeErr != nil { | ||
c.logger.Warn("failed to close response body", zap.Error(closeErr)) | ||
} | ||
}() | ||
|
||
// Check for OK status code | ||
if resp.StatusCode != http.StatusOK { | ||
c.logger.Debug("riak API non-200", zap.Error(err), zap.Int("status_code", resp.StatusCode)) | ||
|
||
// Attempt to extract the error payload | ||
payloadData, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
c.logger.Debug("failed to read payload error message", zap.Error(err)) | ||
} else { | ||
c.logger.Debug("riak API Error", zap.ByteString("api_error", payloadData)) | ||
} | ||
|
||
return fmt.Errorf("non 200 code returned %d", resp.StatusCode) | ||
} | ||
|
||
// Decode the payload into the passed in response object | ||
if err := json.NewDecoder(resp.Body).Decode(respObj); err != nil { | ||
return fmt.Errorf("failed to decode response payload: %w", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.