From 609be0a4ed4a0a8a3db694cc88ef6ad015177c69 Mon Sep 17 00:00:00 2001 From: Dakota Paasman <122491662+dpaasman00@users.noreply.github.com> Date: Fri, 16 Feb 2024 10:58:44 -0500 Subject: [PATCH 001/189] [pkg/stanza] Update KeyValue parser to use parseutils pkg (#31291) **Description:** Updates the KeyValue parser to use the parseutils pkg and subsequent functions **Link to tracking Issue:** N/A Follows up on this [comment](https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/31035#discussion_r1481919680) about merging functionality between Stanza and OTTL key value parsing **Testing:** Unit tests still pass, had to update one because of different wording in err message **Documentation:** N/A --- .../operator/parser/keyvalue/keyvalue.go | 64 +------------------ .../operator/parser/keyvalue/keyvalue_test.go | 2 +- 2 files changed, 4 insertions(+), 62 deletions(-) diff --git a/pkg/stanza/operator/parser/keyvalue/keyvalue.go b/pkg/stanza/operator/parser/keyvalue/keyvalue.go index 7598aafdeb73..634965acc5b4 100644 --- a/pkg/stanza/operator/parser/keyvalue/keyvalue.go +++ b/pkg/stanza/operator/parser/keyvalue/keyvalue.go @@ -7,11 +7,10 @@ import ( "context" "errors" "fmt" - "strings" - "go.uber.org/multierr" "go.uber.org/zap" + "github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/parseutils" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" @@ -98,67 +97,10 @@ func (kv *Parser) parser(input string, delimiter string, pairDelimiter string) ( return nil, fmt.Errorf("parse from field %s is empty", kv.ParseFrom.String()) } - pairs, err := splitPairs(input, pairDelimiter) + pairs, err := parseutils.SplitString(input, pairDelimiter) if err != nil { return nil, fmt.Errorf("failed to parse pairs from input: %w", err) } - parsed := make(map[string]any) - - for _, raw := range pairs { - m := strings.SplitN(raw, delimiter, 2) - if len(m) != 2 { - e := fmt.Errorf("expected '%s' to split by '%s' into two items, got %d", raw, delimiter, len(m)) - err = multierr.Append(err, e) - continue - } - - key := strings.TrimSpace(m[0]) - value := strings.TrimSpace(m[1]) - - parsed[key] = value - } - - return parsed, err -} - -// splitPairs will split the input on the pairDelimiter and return the resulting slice. -// `strings.Split` is not used because it does not respect quotes and will split if the delimiter appears in a quoted value -func splitPairs(input, pairDelimiter string) ([]string, error) { - var result []string - currentPair := "" - delimiterLength := len(pairDelimiter) - quoteChar := "" // "" means we are not in quotes - - for i := 0; i < len(input); i++ { - if quoteChar == "" && i+delimiterLength <= len(input) && input[i:i+delimiterLength] == pairDelimiter { // delimiter - if currentPair == "" { // leading || trailing delimiter; ignore - continue - } - result = append(result, currentPair) - currentPair = "" - i += delimiterLength - 1 - continue - } - - if quoteChar == "" && (input[i] == '"' || input[i] == '\'') { // start of quote - quoteChar = string(input[i]) - continue - } - if string(input[i]) == quoteChar { // end of quote - quoteChar = "" - continue - } - - currentPair += string(input[i]) - } - - if quoteChar != "" { // check for closed quotes - return nil, fmt.Errorf("never reached end of a quoted value") - } - if currentPair != "" { // avoid adding empty value bc of a trailing delimiter - return append(result, currentPair), nil - } - - return result, nil + return parseutils.ParseKeyValuePairs(pairs, delimiter) } diff --git a/pkg/stanza/operator/parser/keyvalue/keyvalue_test.go b/pkg/stanza/operator/parser/keyvalue/keyvalue_test.go index 41cf7593d577..4feefa2bb914 100644 --- a/pkg/stanza/operator/parser/keyvalue/keyvalue_test.go +++ b/pkg/stanza/operator/parser/keyvalue/keyvalue_test.go @@ -147,7 +147,7 @@ func TestParserStringFailure(t *testing.T) { parser := newTestParser(t) _, err := parser.parse("invalid") require.Error(t, err) - require.Contains(t, err.Error(), fmt.Sprintf("expected '%s' to split by '%s' into two items, got", "invalid", parser.delimiter)) + require.Contains(t, err.Error(), fmt.Sprintf("cannot split %q into 2 items, got 1 item(s)", "invalid")) } func TestParserInvalidType(t *testing.T) { From 3e385c8ec2229f9c1cbef233680a29f55a410200 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 16 Feb 2024 09:37:36 -0800 Subject: [PATCH 002/189] Cgroups (#29621) **Description:** Adds `process.cgroup` resource attribute to process metrics **Link to tracking Issue:** Fixes #29282 --------- Co-authored-by: Andrzej Stencel --- .chloggen/cgroups.yaml | 27 +++++++++ NOTICE | 27 +++++++++ receiver/hostmetricsreceiver/README.md | 1 + .../internal/scraper/processscraper/config.go | 4 ++ .../scraper/processscraper/documentation.md | 1 + .../internal/metadata/generated_config.go | 4 ++ .../metadata/generated_config_test.go | 4 ++ .../metadata/generated_metrics_test.go | 1 + .../internal/metadata/generated_resource.go | 7 +++ .../metadata/generated_resource_test.go | 10 +++- .../internal/metadata/testdata/config.yaml | 4 ++ .../scraper/processscraper/metadata.yaml | 4 ++ .../scraper/processscraper/process.go | 55 +++++++++++++++++-- .../scraper/processscraper/process_scraper.go | 9 ++- .../processscraper/process_scraper_darwin.go | 4 ++ .../processscraper/process_scraper_linux.go | 9 +++ .../processscraper/process_scraper_others.go | 4 ++ .../processscraper/process_scraper_test.go | 19 +++++++ .../processscraper/process_scraper_windows.go | 4 ++ 19 files changed, 190 insertions(+), 8 deletions(-) create mode 100755 .chloggen/cgroups.yaml create mode 100644 NOTICE diff --git a/.chloggen/cgroups.yaml b/.chloggen/cgroups.yaml new file mode 100755 index 000000000000..92c9abb4b61e --- /dev/null +++ b/.chloggen/cgroups.yaml @@ -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: Add a new optional resource attribute `process.cgroup` to the `process` scraper of the `hostmetrics` receiver. + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [29282] + +# (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: [] diff --git a/NOTICE b/NOTICE new file mode 100644 index 000000000000..72a751368a46 --- /dev/null +++ b/NOTICE @@ -0,0 +1,27 @@ +receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go contains code originating from gopsutil under internal/common/common.go. + +Copyright (c) 2014, WAKAYAMA Shirou +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of the gopsutil authors nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/receiver/hostmetricsreceiver/README.md b/receiver/hostmetricsreceiver/README.md index 739a38d2e768..d522eae54ac1 100644 --- a/receiver/hostmetricsreceiver/README.md +++ b/receiver/hostmetricsreceiver/README.md @@ -119,6 +119,7 @@ process: mute_process_exe_error: mute_process_io_error: mute_process_user_error: + mute_process_cgroup_error: scrape_process_delay: