Skip to content

Commit

Permalink
Move top-level packages under the internal package (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-bradley authored May 17, 2024
1 parent 9ff6074 commit c67aa3d
Show file tree
Hide file tree
Showing 60 changed files with 183 additions and 29 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ jobs:
fail-fast: false
matrix:
usecase:
- testbed/integration/k8senrichment
- testbed/integration/prometheus
- k8senrichment
- prometheus
runs-on: ubuntu-latest
needs: docker-build
steps:
Expand Down Expand Up @@ -132,7 +132,7 @@ jobs:
- name: Run e2e tests
run: |
cd ${{ matrix.usecase }}
cd internal/testbed/integration/${{ matrix.usecase }}
go test -v --tags=e2e
eec-confmap-provider:
Expand Down Expand Up @@ -170,5 +170,5 @@ jobs:

- name: Run e2e tests
run: |
cd confmap/provider/eecprovider
cd internal/confmap/provider/eecprovider
go test -v --tags=e2e
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ SRC_ROOT := $(shell git rev-parse --show-toplevel)
# ALL_MODULES includes ./* dirs (excludes . dir)
ALL_MODULES := $(shell find . -type f -name "go.mod" -not -path "./build/*" -not -path "./internal/tools/*" -exec dirname {} \; | sort | grep -E '^./' )
# Append root module to all modules
GOMODULES = $(ALL_MODULES) $(PWD)
GOMODULES = $(ALL_MODULES)

SOURCES := $(shell find confmap -type f | sort )
SOURCES := $(shell find internal/confmap -type f | sort )


BIN = $(BIN_DIR)/dynatrace-otel-collector
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (
"github.com/stretchr/testify/require"
)

const CollectorBinary = "../../../../bin/dynatrace-otel-collector"

func TestGetsConfig(t *testing.T) {
f, err := os.ReadFile("./testdata/otel-config.yaml")
require.NoError(t, err)
fs := &fileserver{config: f}
ts := httptest.NewServer(http.HandlerFunc(fs.HandleRequest))
cmd := exec.Command("../../../bin/dynatrace-otel-collector", fmt.Sprintf("--config=%s", configureProvider(t, ts.URL)))
cmd := exec.Command(CollectorBinary, fmt.Sprintf("--config=%s", configureProvider(t, ts.URL)))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
Expand Down Expand Up @@ -51,7 +53,7 @@ func TestReloadsConfig(t *testing.T) {
require.NoError(t, err)
fs := &fileserver{config: f}
ts := httptest.NewServer(http.HandlerFunc(fs.HandleRequest))
cmd := exec.Command("../../../bin/dynatrace-otel-collector", fmt.Sprintf("--config=%s", configureProvider(t, ts.URL)))
cmd := exec.Command(CollectorBinary, fmt.Sprintf("--config=%s", configureProvider(t, ts.URL)))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
Expand Down Expand Up @@ -96,7 +98,7 @@ func TestReloadsConfig(t *testing.T) {
func TestFailsOnBadConfig(t *testing.T) {
fs := &fileserver{}
ts := httptest.NewServer(http.HandlerFunc(fs.HandleRequest))
cmd := exec.Command("../../../bin/dynatrace-otel-collector", fmt.Sprintf("--config=%s", configureProvider(t, ts.URL)))
cmd := exec.Command(CollectorBinary, fmt.Sprintf("--config=%s", configureProvider(t, ts.URL)))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Dynatrace/dynatrace-otel-collector/confmap/provider/eecprovider
module github.com/Dynatrace/dynatrace-otel-collector/internal/confmap/provider/eecprovider

go 1.21

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions go.mod → internal/testbed/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/Dynatrace/dynatrace-otel-collector
module github.com/Dynatrace/dynatrace-otel-collector/internal/testbed

go 1.21.0

Expand Down Expand Up @@ -210,4 +210,4 @@ require (

replace cloud.google.com/go => cloud.google.com/go v0.112.2

replace github.com/Dynatrace/dynatrace-otel-collector/internal/k8stest v0.0.0 => ./internal/k8stest
replace github.com/Dynatrace/dynatrace-otel-collector/internal/k8stest v0.0.0 => ../k8stest
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"os"
"path"
"strings"
"testing"
"time"
Expand All @@ -13,16 +14,20 @@ import (
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
semconv "go.opentelemetry.io/collector/semconv/v1.18.0"

"github.com/Dynatrace/dynatrace-otel-collector/internal/testbed/testutil"
)

const ConfigExamplesDir = "../../../config_examples"

func TestConfigTailSampling(t *testing.T) {
// arrange
col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath))
cfg, err := os.ReadFile("../../config_examples/tail_sampling.yaml")
cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "tail_sampling.yaml"))
require.NoError(t, err)

receiverPort := testbed.GetAvailablePort(t)
exporterPort := testbed.GetAvailablePort(t)
receiverPort := testutil.GetAvailablePort(t)
exporterPort := testutil.GetAvailablePort(t)

parsedConfig := string(cfg)
parsedConfig = replaceOtlpGrpcReceiverPort(parsedConfig, receiverPort)
Expand Down Expand Up @@ -115,11 +120,11 @@ func TestConfigTailSampling(t *testing.T) {
func TestConfigJaegerGrpc(t *testing.T) {
// arrange
col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath))
cfg, err := os.ReadFile("../../config_examples/jaeger.yaml")
cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "jaeger.yaml"))
require.NoError(t, err)

grpcReceiverPort := testbed.GetAvailablePort(t)
exporterPort := testbed.GetAvailablePort(t)
grpcReceiverPort := testutil.GetAvailablePort(t)
exporterPort := testutil.GetAvailablePort(t)

parsedConfig := string(cfg)
parsedConfig = replaceJaegerGrpcReceiverPort(parsedConfig, grpcReceiverPort)
Expand Down Expand Up @@ -208,11 +213,11 @@ func TestConfigJaegerGrpc(t *testing.T) {
func TestConfigHistogramTransform(t *testing.T) {
// arrange
col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath))
cfg, err := os.ReadFile("../../config_examples/split_histogram.yaml")
cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "split_histogram.yaml"))
require.NoError(t, err)

receiverPort := testbed.GetAvailablePort(t)
exporterPort := testbed.GetAvailablePort(t)
receiverPort := testutil.GetAvailablePort(t)
exporterPort := testutil.GetAvailablePort(t)

parsedConfig := string(cfg)
parsedConfig = replaceOtlpGrpcReceiverPort(parsedConfig, receiverPort)
Expand Down Expand Up @@ -318,11 +323,11 @@ func TestConfigHistogramTransform(t *testing.T) {
func TestConfigMetricsFromPreSampledTraces(t *testing.T) {
// arrange
col := testbed.NewChildProcessCollector(testbed.WithAgentExePath(CollectorTestsExecPath))
cfg, err := os.ReadFile("../../config_examples/spanmetrics.yaml")
cfg, err := os.ReadFile(path.Join(ConfigExamplesDir, "spanmetrics.yaml"))
require.NoError(t, err)

receiverPort := testbed.GetAvailablePort(t)
exporterPort := testbed.GetAvailablePort(t)
receiverPort := testutil.GetAvailablePort(t)
exporterPort := testutil.GetAvailablePort(t)

parsedConfig := string(cfg)
parsedConfig = replaceOtlpGrpcReceiverPort(parsedConfig, receiverPort)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
)

const CollectorTestsExecPath string = "../../bin/dynatrace-otel-collector"
const CollectorTestsExecPath string = "../../../bin/dynatrace-otel-collector"

func replaceOtlpGrpcReceiverPort(cfg string, receiverPort int) string {
return strings.Replace(cfg, "4317", strconv.Itoa(receiverPort), 1)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"gopkg.in/yaml.v3"
)

var execPath = "../../bin/dynatrace-otel-collector"
var execPath = "../../../bin/dynatrace-otel-collector"

func TestCollectorStarts(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestCollectorStarts(t *testing.T) {

func TestCollectorIsBuiltFromManifest(t *testing.T) {
components := getComponents(t)
b, err := os.ReadFile("../../manifest.yaml")
b, err := os.ReadFile("../../../manifest.yaml")
require.NoError(t, err)
manifestComponents := manifest{}
err = yaml.Unmarshal(b, &manifestComponents)
Expand Down
File renamed without changes.
File renamed without changes.
147 changes: 147 additions & 0 deletions internal/testbed/testutil/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"net"
"os/exec"
"runtime"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/featuregate"
)

type portpair struct {
first string
last string
}

// GetAvailableLocalAddress finds an available local port on tcp network and returns an endpoint
// describing it. The port is available for opening when this function returns
// provided that there is no race by some other code to grab the same port
// immediately.
func GetAvailableLocalAddress(t testing.TB) string {
return GetAvailableLocalNetworkAddress(t, "tcp")
}

// GetAvailableLocalNetworkAddress finds an available local port on specified network and returns an endpoint
// describing it. The port is available for opening when this function returns
// provided that there is no race by some other code to grab the same port
// immediately.
func GetAvailableLocalNetworkAddress(t testing.TB, network string) string {
// Retry has been added for windows as net.Listen can return a port that is not actually available. Details can be
// found in https://github.com/docker/for-win/issues/3171 but to summarize Hyper-V will reserve ranges of ports
// which do not show up under the "netstat -ano" but can only be found by
// "netsh interface ipv4 show excludedportrange protocol=tcp". We'll use []exclusions to hold those ranges and
// retry if the port returned by GetAvailableLocalAddress falls in one of those them.
var exclusions []portpair

portFound := false
if runtime.GOOS == "windows" {
exclusions = getExclusionsList(t)
}

var endpoint string
for !portFound {
endpoint = findAvailableAddress(t, network)
_, port, err := net.SplitHostPort(endpoint)
require.NoError(t, err)
portFound = true
if runtime.GOOS == "windows" {
for _, pair := range exclusions {
if port >= pair.first && port <= pair.last {
portFound = false
break
}
}
}
}

return endpoint
}

func findAvailableAddress(t testing.TB, network string) string {
switch network {
// net.Listen supported network strings
case "tcp", "tcp4", "tcp6", "unix", "unixpacket":
ln, err := net.Listen(network, "localhost:0")
require.NoError(t, err, "Failed to get a free local port")
// There is a possible race if something else takes this same port before
// the test uses it, however, that is unlikely in practice.
defer func() {
assert.NoError(t, ln.Close())
}()
return ln.Addr().String()
// net.ListenPacket supported network strings
case "udp", "udp4", "udp6", "unixgram":
ln, err := net.ListenPacket(network, "localhost:0")
require.NoError(t, err, "Failed to get a free local port")
// There is a possible race if something else takes this same port before
// the test uses it, however, that is unlikely in practice.
defer func() {
assert.NoError(t, ln.Close())
}()
return ln.LocalAddr().String()
}
return ""
}

// Get excluded ports on Windows from the command: netsh interface ipv4 show excludedportrange protocol=tcp
func getExclusionsList(t testing.TB) []portpair {
cmdTCP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=tcp")
outputTCP, errTCP := cmdTCP.CombinedOutput()
require.NoError(t, errTCP)
exclusions := createExclusionsList(t, string(outputTCP))

cmdUDP := exec.Command("netsh", "interface", "ipv4", "show", "excludedportrange", "protocol=udp")
outputUDP, errUDP := cmdUDP.CombinedOutput()
require.NoError(t, errUDP)
exclusions = append(exclusions, createExclusionsList(t, string(outputUDP))...)

return exclusions
}

func createExclusionsList(t testing.TB, exclusionsText string) []portpair {
var exclusions []portpair

parts := strings.Split(exclusionsText, "--------")
require.Equal(t, len(parts), 3)
portsText := strings.Split(parts[2], "*")
require.Greater(t, len(portsText), 1) // original text may have a suffix like " - Administered port exclusions."
lines := strings.Split(portsText[0], "\n")
for _, line := range lines {
if strings.TrimSpace(line) != "" {
entries := strings.Fields(strings.TrimSpace(line))
require.Equal(t, len(entries), 2)
pair := portpair{entries[0], entries[1]}
exclusions = append(exclusions, pair)
}
}
return exclusions
}

// Force the state of feature gate for a test
// usage: defer SetFeatureGateForTest("gateName", true)()
func SetFeatureGateForTest(t testing.TB, gate *featuregate.Gate, enabled bool) func() {
originalValue := gate.IsEnabled()
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), enabled))
return func() {
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), originalValue))
}
}

func GetAvailablePort(t testing.TB) int {
endpoint := GetAvailableLocalAddress(t)
_, port, err := net.SplitHostPort(endpoint)
require.NoError(t, err)

portInt, err := strconv.Atoi(port)
require.NoError(t, err)

return portInt
}
4 changes: 2 additions & 2 deletions manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ providers:
- gomod: go.opentelemetry.io/collector/confmap/provider/httpprovider v0.100.0
- gomod: go.opentelemetry.io/collector/confmap/provider/httpsprovider v0.100.0
- gomod: go.opentelemetry.io/collector/confmap/provider/yamlprovider v0.100.0
- gomod: github.com/Dynatrace/dynatrace-otel-collector/confmap/provider/eecprovider v0.0.0
- gomod: github.com/Dynatrace/dynatrace-otel-collector/internal/confmap/provider/eecprovider v0.0.0

replaces:
- github.com/Dynatrace/dynatrace-otel-collector/confmap/provider/eecprovider => ../confmap/provider/eecprovider
- github.com/Dynatrace/dynatrace-otel-collector/internal/confmap/provider/eecprovider => ../internal/confmap/provider/eecprovider

0 comments on commit c67aa3d

Please sign in to comment.