Skip to content

Commit

Permalink
[chore] move collectdreceiver shared code to an internal package (#28856
Browse files Browse the repository at this point in the history
)

This allows the collectdreceiver to pass checkapi.
  • Loading branch information
atoulme authored Nov 6, 2023
1 parent 1bfbb18 commit ad6fa27
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 110 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ extension/storage/dbstorage/ @open-te
extension/storage/filestorage/ @open-telemetry/collector-contrib-approvers @djaglowski

internal/aws/ @open-telemetry/collector-contrib-approvers @Aneurysm9 @mxiamxia
internal/collectd/ @open-telemetry/collector-contrib-approvers @atoulme
internal/coreinternal/ @open-telemetry/collector-contrib-approvers @open-telemetry/collector-approvers
internal/datadog/ @open-telemetry/collector-contrib-approvers @mx-psi @gbbr @dineshg13
internal/docker/ @open-telemetry/collector-contrib-approvers @rmfitzpatrick @jamesmoessis
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ body:
- extension/storage/dbstorage
- extension/storage/filestorage
- internal/aws
- internal/collectd
- internal/core
- internal/datadog
- internal/docker
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ body:
- extension/storage/dbstorage
- extension/storage/filestorage
- internal/aws
- internal/collectd
- internal/core
- internal/datadog
- internal/docker
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ body:
- extension/storage/dbstorage
- extension/storage/filestorage
- internal/aws
- internal/collectd
- internal/core
- internal/datadog
- internal/docker
Expand Down
10 changes: 5 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ updates:
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/internal/collectd"
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/internal/coreinternal"
schedule:
Expand Down Expand Up @@ -1012,11 +1017,6 @@ updates:
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/receiver/splunkenterprisereceiver"
schedule:
interval: "weekly"
day: "wednesday"
- package-ecosystem: "gomod"
directory: "/receiver/splunkhecreceiver"
schedule:
Expand Down
1 change: 0 additions & 1 deletion cmd/checkapi/allowlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ connector/servicegraphconnector
extension/encoding
extension/observer
processor/servicegraphprocessor
receiver/collectdreceiver
receiver/dockerstatsreceiver
receiver/journaldreceiver
receiver/kafkareceiver
Expand Down
3 changes: 3 additions & 0 deletions cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/metrics v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/proxy v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd v0.0.0-00010101000000-000000000000 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/kafka v0.88.0 // indirect
Expand Down Expand Up @@ -1126,3 +1127,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib => ../..
replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver => ../../receiver/azureeventhubreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd => ../../internal/collectd
1 change: 1 addition & 0 deletions cmd/otelcontribcol/builder-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -421,3 +421,4 @@ replaces:
- github.com/outcaste-io/ristretto v0.2.0 => github.com/outcaste-io/ristretto v0.2.1
- github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure => ../../pkg/translator/azure
- github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/skywalking => ../../pkg/translator/skywalking
- github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd => ../../internal/collectd
3 changes: 3 additions & 0 deletions cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/metrics v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/proxy v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd v0.0.0-00010101000000-000000000000 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter v0.88.0 // indirect
Expand Down Expand Up @@ -1143,3 +1144,5 @@ replace github.com/outcaste-io/ristretto v0.2.0 => github.com/outcaste-io/ristre
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure => ../../pkg/translator/azure

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/skywalking => ../../pkg/translator/skywalking

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd => ../../internal/collectd
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/metrics v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/proxy v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/xray v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd v0.0.0-00010101000000-000000000000 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.88.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog v0.88.0 // indirect
Expand Down Expand Up @@ -1130,3 +1131,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/snowf
replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azureeventhubreceiver => ./receiver/azureeventhubreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ./pkg/golden

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd => ./internal/collectd
1 change: 1 addition & 0 deletions internal/collectd/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
3 changes: 3 additions & 0 deletions internal/collectd/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd

go 1.20
52 changes: 52 additions & 0 deletions internal/collectd/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package collectd // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd"

import (
"strings"
)

// LabelsFromName tries to pull out dimensions out of name in the format
// "name[k=v,f=x]-more_name".
// For the example above it would return "name-more_name" and extract dimensions
// (k,v) and (f,x).
// If something unexpected is encountered it returns the original metric name.
//
// The code tries to avoid allocation by using local slices and avoiding calls
// to functions like strings.Slice.
func LabelsFromName(val *string) (metricName string, labels map[string]string) {
metricName = *val
index := strings.Index(*val, "[")
if index > -1 {
left := (*val)[:index]
rest := (*val)[index+1:]
index = strings.Index(rest, "]")
if index > -1 {
working := make(map[string]string)
dimensions := rest[:index]
rest = rest[index+1:]
cindex := strings.Index(dimensions, ",")
prev := 0
for {
if cindex < prev {
cindex = len(dimensions)
}
piece := dimensions[prev:cindex]
tindex := strings.Index(piece, "=")
if tindex == -1 || strings.Contains(piece[tindex+1:], "=") {
return
}
working[piece[:tindex]] = piece[tindex+1:]
if cindex == len(dimensions) {
break
}
prev = cindex + 1
cindex = strings.Index(dimensions[prev:], ",") + prev
}
labels = working
metricName = left + rest
}
}
return
}
3 changes: 3 additions & 0 deletions internal/collectd/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
status:
codeowners:
active: [atoulme]
50 changes: 3 additions & 47 deletions receiver/collectdreceiver/collectd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package collectdreceiver // import "github.com/open-telemetry/opentelemetry-coll
import (
"encoding/json"
"fmt"
"strings"
"time"

"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/sanitize"
)

Expand Down Expand Up @@ -187,7 +187,7 @@ func (cdr *collectDRecord) pointTypeInstance(attrs map[string]string, parts []by
return parts
}

instanceName, extractedAttrs := LabelsFromName(cdr.TypeInstance)
instanceName, extractedAttrs := collectd.LabelsFromName(cdr.TypeInstance)
if instanceName != "" {
if len(parts) > 0 {
parts = append(parts, '.')
Expand All @@ -203,50 +203,6 @@ func (cdr *collectDRecord) pointTypeInstance(attrs map[string]string, parts []by
return parts
}

// LabelsFromName tries to pull out dimensions out of name in the format
// "name[k=v,f=x]-more_name".
// For the example above it would return "name-more_name" and extract dimensions
// (k,v) and (f,x).
// If something unexpected is encountered it returns the original metric name.
//
// The code tries to avoid allocation by using local slices and avoiding calls
// to functions like strings.Slice.
func LabelsFromName(val *string) (metricName string, labels map[string]string) {
metricName = *val
index := strings.Index(*val, "[")
if index > -1 {
left := (*val)[:index]
rest := (*val)[index+1:]
index = strings.Index(rest, "]")
if index > -1 {
working := make(map[string]string)
dimensions := rest[:index]
rest = rest[index+1:]
cindex := strings.Index(dimensions, ",")
prev := 0
for {
if cindex < prev {
cindex = len(dimensions)
}
piece := dimensions[prev:cindex]
tindex := strings.Index(piece, "=")
if tindex == -1 || strings.Contains(piece[tindex+1:], "=") {
return
}
working[piece[:tindex]] = piece[tindex+1:]
if cindex == len(dimensions) {
break
}
prev = cindex + 1
cindex = strings.Index(dimensions[prev:], ",") + prev
}
labels = working
metricName = left + rest
}
}
return
}

func isNilOrEmpty(str *string) bool {
return str == nil || *str == ""
}
Expand All @@ -263,7 +219,7 @@ func parseAndAddLabels(labels map[string]string, pluginInstance *string, host *s
}

func parseNameForLabels(labels map[string]string, key string, val *string) {
instanceName, toAddDims := LabelsFromName(val)
instanceName, toAddDims := collectd.LabelsFromName(val)

for k, v := range toAddDims {
if _, exists := labels[k]; !exists {
Expand Down
3 changes: 3 additions & 0 deletions receiver/collectdreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collec
go 1.20

require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.88.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.88.0
Expand Down Expand Up @@ -77,3 +78,5 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd => ../../internal/collectd
26 changes: 2 additions & 24 deletions receiver/wavefrontreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/wavefr
go 1.20

require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd v0.0.0-00010101000000-000000000000
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.88.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver v0.88.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.88.1-0.20231026220224-6405e152a2d9
go.opentelemetry.io/collector/config/confignet v0.88.1-0.20231026220224-6405e152a2d9
Expand All @@ -17,16 +17,10 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
Expand All @@ -36,20 +30,10 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.10.1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/config/configauth v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/config/configcompression v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/config/confighttp v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/config/configopaque v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/config/configtls v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/config/internal v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/extension v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/extension/auth v0.88.1-0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0017.0.20231026220224-6405e152a2d9 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
Expand All @@ -64,8 +48,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/collectdreceiver => ../collectdreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver => ../carbonreceiver

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/common => ../../internal/common
Expand All @@ -76,8 +58,4 @@ retract (
v0.65.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../pkg/golden
replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/collectd => ../../internal/collectd
Loading

0 comments on commit ad6fa27

Please sign in to comment.