forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathend_to_end_test.go
More file actions
194 lines (177 loc) · 7.45 KB
/
end_to_end_test.go
File metadata and controls
194 lines (177 loc) · 7.45 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prometheusexporter
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"testing"
"time"
promconfig "github.com/prometheus/prometheus/config"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/receiver/prometheusreceiver"
)
func TestEndToEndSummarySupport(t *testing.T) {
if testing.Short() {
t.Skip("This test can take a couple of seconds")
}
// 1. Create the Prometheus scrape endpoint.
waitForScrape := make(chan bool, 1)
shutdown := make(chan bool, 1)
dropWizardServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
select {
case <-shutdown:
return
case waitForScrape <- true:
// Serve back the metrics as if they were from DropWizard.
_, err := rw.Write([]byte(dropWizardResponse))
require.NoError(t, err)
}
}))
defer dropWizardServer.Close()
defer close(shutdown)
srvURL, err := url.Parse(dropWizardServer.URL)
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// 2. Create the Prometheus metrics exporter that'll receive and verify the metrics produced.
exporterCfg := &Config{
ExporterSettings: config.NewExporterSettings(config.NewID(typeStr)),
Namespace: "test",
Endpoint: ":8787",
SendTimestamps: true,
MetricExpiration: 2 * time.Hour,
}
exporterFactory := NewFactory()
set := component.ExporterCreateSettings{Logger: zap.NewNop()}
exporter, err := exporterFactory.CreateMetricsExporter(ctx, set, exporterCfg)
if err != nil {
t.Fatal(err)
}
if err = exporter.Start(ctx, nil); err != nil {
t.Fatalf("Failed to start the Prometheus receiver: %v", err)
}
t.Cleanup(func() { require.NoError(t, exporter.Shutdown(ctx)) })
// 3. Create the Prometheus receiver scraping from the DropWizard mock server and
// it'll feed scraped and converted metrics then pass them to the Prometheus exporter.
yamlConfig := []byte(fmt.Sprintf(`
global:
scrape_interval: 2ms
scrape_configs:
- job_name: 'otel-collector'
scrape_interval: 2ms
static_configs:
- targets: ['%s']
`, srvURL.Host))
receiverConfig := new(promconfig.Config)
if err = yaml.Unmarshal(yamlConfig, receiverConfig); err != nil {
t.Fatal(err)
}
receiverFactory := prometheusreceiver.NewFactory()
receiverCreateSet := component.ReceiverCreateSettings{
Logger: zap.NewNop(),
}
rcvCfg := &prometheusreceiver.Config{
PrometheusConfig: receiverConfig,
ReceiverSettings: config.NewReceiverSettings(config.NewID("prometheus")),
}
// 3.5 Create the Prometheus receiver and pass in the preivously created Prometheus exporter.
prometheusReceiver, err := receiverFactory.CreateMetricsReceiver(ctx, receiverCreateSet, rcvCfg, exporter)
if err != nil {
t.Fatal(err)
}
if err = prometheusReceiver.Start(ctx, nil); err != nil {
t.Fatalf("Failed to start the Prometheus receiver: %v", err)
}
t.Cleanup(func() { require.NoError(t, prometheusReceiver.Shutdown(ctx)) })
// 4. Scrape from the Prometheus exporter to ensure that we export summary metrics
// We shall let the Prometheus exporter scrape the DropWizard mock server, at least 9 times.
for i := 0; i < 8; i++ {
<-waitForScrape
}
res, err := http.Get("http://localhost" + exporterCfg.Endpoint + "/metrics")
if err != nil {
t.Fatalf("Failed to scrape from the exporter: %v", err)
}
prometheusExporterScrape, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
// 5. Verify that we have the summary metrics and that their values make sense.
wantLineRegexps := []string{
`. HELP test_jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.`,
`. TYPE test_jvm_gc_collection_seconds summary`,
`test_jvm_gc_collection_seconds_sum.gc="G1 Old Generation". 0.*`,
`test_jvm_gc_collection_seconds_count.gc="G1 Old Generation". 0.*`,
`test_jvm_gc_collection_seconds_sum.gc="G1 Young Generation". 0.*`,
`test_jvm_gc_collection_seconds_count.gc="G1 Young Generation". 9.*`,
`. HELP test_jvm_info JVM version info`,
`. TYPE test_jvm_info gauge`,
`test_jvm_info.vendor="Oracle Corporation",version="9.0.4.11". 1.*`,
`. HELP test_jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool.`,
`. TYPE test_jvm_memory_pool_bytes_used gauge`,
`test_jvm_memory_pool_bytes_used.pool="CodeHeap 'non.nmethods'". 1.277952e.06.*`,
`test_jvm_memory_pool_bytes_used.pool="CodeHeap 'non.profiled nmethods'". 2.869376e.06.*`,
`test_jvm_memory_pool_bytes_used.pool="CodeHeap 'profiled nmethods'". 6.871168e.06.*`,
`test_jvm_memory_pool_bytes_used.pool="Compressed Class Space". 2.751312e.06.*`,
`test_jvm_memory_pool_bytes_used.pool="G1 Eden Space". 4.4040192e.07.*`,
`test_jvm_memory_pool_bytes_used.pool="G1 Old Gen". 4.385408e.06.*`,
`test_jvm_memory_pool_bytes_used.pool="G1 Survivor Space". 8.388608e.06.*`,
`test_jvm_memory_pool_bytes_used.pool="Metaspace". 2.6218176e.07.*`,
}
// 5.5: Perform a complete line by line prefix verification to ensure we extract back the inputs
// we'd expect after scraping Prometheus.
for _, wantLineRegexp := range wantLineRegexps {
reg := regexp.MustCompile(wantLineRegexp)
prometheusExporterScrape = reg.ReplaceAll(prometheusExporterScrape, []byte(""))
}
// After this replacement, there should ONLY be newlines present.
prometheusExporterScrape = bytes.ReplaceAll(prometheusExporterScrape, []byte("\n"), []byte(""))
// Now assert that NO output was left over.
if len(prometheusExporterScrape) != 0 {
t.Fatalf("Left-over unmatched Prometheus scrape content: %q\n", prometheusExporterScrape)
}
}
const dropWizardResponse = `
# HELP jvm_memory_pool_bytes_used Used bytes of a given JVM memory pool.
# TYPE jvm_memory_pool_bytes_used gauge
jvm_memory_pool_bytes_used{pool="CodeHeap 'non-nmethods'",} 1277952.0
jvm_memory_pool_bytes_used{pool="Metaspace",} 2.6218176E7
jvm_memory_pool_bytes_used{pool="CodeHeap 'profiled nmethods'",} 6871168.0
jvm_memory_pool_bytes_used{pool="Compressed Class Space",} 2751312.0
jvm_memory_pool_bytes_used{pool="G1 Eden Space",} 4.4040192E7
jvm_memory_pool_bytes_used{pool="G1 Old Gen",} 4385408.0
jvm_memory_pool_bytes_used{pool="G1 Survivor Space",} 8388608.0
jvm_memory_pool_bytes_used{pool="CodeHeap 'non-profiled nmethods'",} 2869376.0
# HELP jvm_info JVM version info
# TYPE jvm_info gauge
jvm_info{version="9.0.4+11",vendor="Oracle Corporation",} 1.0
# HELP jvm_gc_collection_seconds Time spent in a given JVM garbage collector in seconds.
# TYPE jvm_gc_collection_seconds summary
jvm_gc_collection_seconds_count{gc="G1 Young Generation",} 9.0
jvm_gc_collection_seconds_sum{gc="G1 Young Generation",} 0.229
jvm_gc_collection_seconds_count{gc="G1 Old Generation",} 0.0
jvm_gc_collection_seconds_sum{gc="G1 Old Generation",} 0.0`