-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathtelemetry_test.go
101 lines (89 loc) · 3.14 KB
/
telemetry_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023 Datadog, Inc.
package telemetrytest
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/DataDog/dd-trace-go.v1/contrib/gorilla/mux"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry/telemetrytest"
"github.com/stretchr/testify/require"
)
// TestIntegrationInfo verifies that an integration leveraging instrumentation telemetry
// sends the correct data to the telemetry client.
func TestIntegrationInfo(t *testing.T) {
// mux.NewRouter() uses the net/http and gorilla/mux integration
client := new(telemetrytest.RecordClient)
telemetry.StartApp(client)
_ = mux.NewRouter()
assert.Contains(t, client.Integrations, telemetry.Integration{Name: "net/http", Version: "", Error: ""})
assert.Contains(t, client.Integrations, telemetry.Integration{Name: "gorilla/mux", Version: "", Error: ""})
}
type contribPkg struct {
ImportPath string
Root string
Name string
Imports []string
Dir string
}
var TelemetryImport = "gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
func readPackage(t *testing.T, path string) contribPkg {
cmd := exec.Command("go", "list", "-json", path)
cmd.Stderr = os.Stderr
output, err := cmd.Output()
require.NoError(t, err)
p := contribPkg{}
err = json.Unmarshal(output, &p)
require.NoError(t, err)
return p
}
func (p *contribPkg) hasTelemetryImport(t *testing.T) bool {
for _, imp := range p.Imports {
if imp == TelemetryImport {
return true
}
}
// if we didn't find it imported directly, it might be imported in one of sub-package imports
for _, imp := range p.Imports {
if strings.HasPrefix(imp, p.ImportPath) {
p := readPackage(t, imp)
if p.hasTelemetryImport(t) {
return true
}
}
}
return false
}
// TestTelemetryEnabled verifies that the expected contrib packages leverage instrumentation telemetry
func TestTelemetryEnabled(t *testing.T) {
body, err := exec.Command("go", "list", "-json", "../../...").Output()
require.NoError(t, err)
var packages []contribPkg
stream := json.NewDecoder(strings.NewReader(string(body)))
for stream.More() {
var out contribPkg
err := stream.Decode(&out)
require.NoError(t, err)
packages = append(packages, out)
}
for _, pkg := range packages {
if strings.Contains(pkg.ImportPath, "/test") || strings.Contains(pkg.ImportPath, "/internal") || strings.Contains(pkg.ImportPath, "/cmd") {
continue
}
sep := string(os.PathSeparator)
p := strings.Replace(pkg.Dir, pkg.Root, filepath.Join("..", ".."), 1)
if strings.Contains(p, filepath.Join(sep, "contrib", "net", "http", "client")) || strings.Contains(p, filepath.Join(sep, "contrib", "os")) {
continue
}
if !pkg.hasTelemetryImport(t) {
t.Fatalf(`package %q is expected use instrumentation telemetry. For more info see https://github.com/DataDog/dd-trace-go/blob/main/contrib/README.md#instrumentation-telemetry`, pkg.ImportPath)
}
}
}