1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-11 00:50:34 +02:00
Files
opentelemetry-go/sdk/metric/controller/basic/pull_test.go
Joshua MacDonald dfece3d2b9 Combine the Push and Pull metric controllers (#1378)
* Push->basic

* Repackage

* Rename away from push

* Make exporter optional; export from a separate goroutine

* Move pull_test into controller_test

* Precommit pass

* New OTLP/Prom example

* Precommit

* Fix the example

* Shorten the example

* Test starting controller w/o exporter

* Test export timeout

* Remove ancient example & lint

* go.mod revert & tidy

* Comments

* Tidy a diff

* Tidy a diff

* Move export kind selector in the new example

* Split this test into its original parts

* Reduce diff size

* Changelog

* Remove extra Add/Done pair

* Remove unused stopCh param; document the Stop behavior

* Typo

* Use ctx

* Missed v0.15

* Apply PR feedback

* Precommit pass

* 0.14 -> 0.15 in new file

* Remove diff chunk markers

* Fix OTLP example

* Upstream

* dashpole comments

* aneurysm9 feedback

* Tidy go.sum
2021-01-13 17:07:44 -05:00

119 lines
3.5 KiB
Go

// 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 basic_test
import (
"context"
"runtime"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/metric"
export "go.opentelemetry.io/otel/sdk/export/metric"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
"go.opentelemetry.io/otel/sdk/metric/controller/controllertest"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
)
func TestPullNoCollect(t *testing.T) {
puller := controller.New(
processor.New(
processortest.AggregatorSelector(),
export.CumulativeExportKindSelector(),
processor.WithMemory(true),
),
controller.WithCollectPeriod(0),
)
ctx := context.Background()
meter := puller.MeterProvider().Meter("nocache")
counter := metric.Must(meter).NewInt64Counter("counter.sum")
counter.Add(ctx, 10, label.String("A", "B"))
require.NoError(t, puller.Collect(ctx))
records := processortest.NewOutput(label.DefaultEncoder())
require.NoError(t, puller.ForEach(export.CumulativeExportKindSelector(), records.AddRecord))
require.EqualValues(t, map[string]float64{
"counter.sum/A=B/": 10,
}, records.Map())
counter.Add(ctx, 10, label.String("A", "B"))
require.NoError(t, puller.Collect(ctx))
records = processortest.NewOutput(label.DefaultEncoder())
require.NoError(t, puller.ForEach(export.CumulativeExportKindSelector(), records.AddRecord))
require.EqualValues(t, map[string]float64{
"counter.sum/A=B/": 20,
}, records.Map())
}
func TestPullWithCollect(t *testing.T) {
puller := controller.New(
processor.New(
processortest.AggregatorSelector(),
export.CumulativeExportKindSelector(),
processor.WithMemory(true),
),
controller.WithCollectPeriod(time.Second),
)
mock := controllertest.NewMockClock()
puller.SetClock(mock)
ctx := context.Background()
meter := puller.MeterProvider().Meter("nocache")
counter := metric.Must(meter).NewInt64Counter("counter.sum")
counter.Add(ctx, 10, label.String("A", "B"))
require.NoError(t, puller.Collect(ctx))
records := processortest.NewOutput(label.DefaultEncoder())
require.NoError(t, puller.ForEach(export.CumulativeExportKindSelector(), records.AddRecord))
require.EqualValues(t, map[string]float64{
"counter.sum/A=B/": 10,
}, records.Map())
counter.Add(ctx, 10, label.String("A", "B"))
// Cached value!
require.NoError(t, puller.Collect(ctx))
records = processortest.NewOutput(label.DefaultEncoder())
require.NoError(t, puller.ForEach(export.CumulativeExportKindSelector(), records.AddRecord))
require.EqualValues(t, map[string]float64{
"counter.sum/A=B/": 10,
}, records.Map())
mock.Add(time.Second)
runtime.Gosched()
// Re-computed value!
require.NoError(t, puller.Collect(ctx))
records = processortest.NewOutput(label.DefaultEncoder())
require.NoError(t, puller.ForEach(export.CumulativeExportKindSelector(), records.AddRecord))
require.EqualValues(t, map[string]float64{
"counter.sum/A=B/": 20,
}, records.Map())
}