-
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.
Problem: Ensure the exporter library correctly sends telemetry data via OTel Solution: Add a functional test: - Exporter sends telemetry data to an OTel collector - Ensure the collector successfully receives the data by looking at its logs - Include the test into GitHub pipeline. CLOSES #7
- Loading branch information
Showing
9 changed files
with
424 additions
and
1 deletion.
There are no files selected for viewing
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,3 @@ | ||
# syntax=docker/dockerfile:1.6 | ||
# this is here so we can grab the latest version of the collector and have dependabot keep it up to date | ||
FROM otel/opentelemetry-collector-contrib:0.88.0 |
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,30 @@ | ||
exporters: | ||
debug: | ||
verbosity: detailed | ||
logging: {} | ||
extensions: | ||
health_check: {} | ||
memory_ballast: | ||
size_in_percentage: 40 | ||
processors: | ||
batch: {} | ||
memory_limiter: | ||
check_interval: 5s | ||
limit_percentage: 80 | ||
spike_limit_percentage: 25 | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
endpoint: 0.0.0.0:4317 | ||
# http: | ||
# endpoint: 0.0.0.0:4318 | ||
service: | ||
extensions: | ||
- health_check | ||
pipelines: | ||
traces: | ||
exporters: | ||
- debug | ||
receivers: | ||
- otlp |
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,190 @@ | ||
package tests | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"log/slog" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/go-logr/logr" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/wait" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc" | ||
|
||
"github.com/nginxinc/telemetry-exporter/pkg/telemetry" | ||
) | ||
|
||
type telemetryData struct { | ||
ResourceCount int | ||
} | ||
|
||
func (d *telemetryData) Attributes() []attribute.KeyValue { | ||
return []attribute.KeyValue{ | ||
attribute.Int("resourceCount", d.ResourceCount), | ||
} | ||
} | ||
|
||
type matchingLogConsumer struct { | ||
expectedSubstrings map[string]struct{} | ||
sync sync.Mutex | ||
} | ||
|
||
func (c *matchingLogConsumer) Accept(log testcontainers.Log) { | ||
c.sync.Lock() | ||
defer c.sync.Unlock() | ||
|
||
line := string(log.Content) | ||
|
||
for k := range c.expectedSubstrings { | ||
if strings.Contains(line, k) { | ||
delete(c.expectedSubstrings, k) | ||
break | ||
} | ||
} | ||
} | ||
|
||
func (c *matchingLogConsumer) setExpectedSubstrings(substrings []string) { | ||
c.sync.Lock() | ||
defer c.sync.Unlock() | ||
|
||
c.expectedSubstrings = make(map[string]struct{}, len(substrings)) | ||
for _, s := range substrings { | ||
c.expectedSubstrings[s] = struct{}{} | ||
} | ||
} | ||
|
||
func (c *matchingLogConsumer) unmatchedCount() int { | ||
c.sync.Lock() | ||
defer c.sync.Unlock() | ||
return len(c.expectedSubstrings) | ||
} | ||
|
||
func getCollectorImageFromDockerfile() (string, error) { | ||
dockerFile, err := os.Open("Dockerfile") | ||
if err != nil { | ||
return "", fmt.Errorf("failed to open Dockerfile: %w", err) | ||
} | ||
defer dockerFile.Close() | ||
|
||
reader := bufio.NewReader(dockerFile) | ||
|
||
for { | ||
line, err := reader.ReadString('\n') | ||
if err == io.EOF { | ||
return "", fmt.Errorf("FROM not found in Dockerfile") | ||
} | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read Dockerfile: %w", err) | ||
} | ||
|
||
if !strings.HasPrefix(line, "FROM ") { | ||
continue | ||
} | ||
|
||
return strings.TrimSpace(strings.TrimPrefix(line, "FROM ")), nil | ||
} | ||
} | ||
|
||
var _ = Describe("Exporter", func() { | ||
var ( | ||
lc *matchingLogConsumer | ||
exporter *telemetry.Exporter | ||
collector testcontainers.Container | ||
ctx context.Context | ||
) | ||
|
||
BeforeEach(func() { | ||
ctx := context.Background() | ||
|
||
// Run the collector container | ||
|
||
image, err := getCollectorImageFromDockerfile() | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
const collectorCfgName = "collector.yaml" | ||
|
||
lc = &matchingLogConsumer{} | ||
|
||
req := testcontainers.ContainerRequest{ | ||
Image: image, | ||
Files: []testcontainers.ContainerFile{ | ||
{ | ||
HostFilePath: "./" + collectorCfgName, | ||
ContainerFilePath: "/" + collectorCfgName, | ||
FileMode: 0o444, | ||
}, | ||
}, | ||
ExposedPorts: []string{"4317/tcp"}, | ||
WaitingFor: wait.ForLog("Everything is ready. Begin running and processing data."), | ||
LogConsumerCfg: &testcontainers.LogConsumerConfig{ | ||
Consumers: []testcontainers.LogConsumer{lc}, | ||
}, | ||
Cmd: []string{"--config=/" + collectorCfgName}, | ||
} | ||
|
||
collector, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ | ||
ContainerRequest: req, | ||
Started: true, | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Create the exporter | ||
|
||
ip, err := collector.Host(ctx) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
port, err := collector.MappedPort(ctx, "4317") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
endpoint := fmt.Sprintf("%s:%s", ip, port.Port()) | ||
|
||
logger := logr.FromSlogHandler(slog.Default().Handler()) | ||
|
||
errorHandler := telemetry.NewErrorHandler() | ||
|
||
exporter, err = telemetry.NewExporter( | ||
telemetry.ExporterConfig{ | ||
SpanProvider: telemetry.CreateOTLPSpanProvider( | ||
otlptracegrpc.WithEndpoint(endpoint), | ||
otlptracegrpc.WithInsecure(), | ||
), | ||
}, | ||
telemetry.WithGlobalOTelLogger(logger), | ||
telemetry.WithGlobalOTelErrorHandler(errorHandler), | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
AfterEach(func() { | ||
if collector != nil { | ||
err := collector.Terminate(ctx) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
if exporter != nil { | ||
err := exporter.Shutdown(ctx) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
}) | ||
|
||
It("exports data successfully", func() { | ||
lc.setExpectedSubstrings([]string{ | ||
"resourceCount: Int(1)", | ||
}) | ||
|
||
data := &telemetryData{ | ||
ResourceCount: 1, | ||
} | ||
|
||
err := exporter.Export(ctx, data) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Eventually(lc.unmatchedCount, "10s").Should(BeZero()) | ||
}) | ||
}) |
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,13 @@ | ||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestExporter(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Tests Suite") | ||
} |