Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions exporter/exporterhelper/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenterror"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/exporter/exporterhelper/internal"
)

// FactoryOption apply changes to ExporterOptions.
Expand Down Expand Up @@ -78,6 +79,7 @@ func NewFactory(
for _, opt := range options {
opt(f)
}
internal.RegisterMetrics()
return f
}

Expand Down
89 changes: 89 additions & 0 deletions exporter/exporterhelper/internal/observability_experimental.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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.

//go:build enable_unstable
// +build enable_unstable

package internal

import (
"context"
"sync"

"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
)

var (
currentlyDispatchedBatches = stats.Int64(
"/currently_dispatched_batches",
"Number of batches that are currently being sent",
stats.UnitDimensionless)

totalDispatchedBatches = stats.Int64(
"/total_dispatched_batches",
"Total number of batches which were processed",
stats.UnitDimensionless)

queueNameKey = tag.MustNewKey("queue_name")
)

func recordCurrentlyDispatchedBatches(ctx context.Context, dispatchedBatches int, queueName string) {
ctx, err := tag.New(ctx, tag.Insert(queueNameKey, queueName))
if err != nil {
return
}

stats.Record(ctx, currentlyDispatchedBatches.M(int64(dispatchedBatches)))
}

func recordBatchDispatched(ctx context.Context, queueName string) {
ctx, err := tag.New(ctx, tag.Insert(queueNameKey, queueName))
if err != nil {
return
}

stats.Record(ctx, totalDispatchedBatches.M(int64(1)))
}

// ExporterHelperInternalViews return the metrics views according to given telemetry level.
func ExporterHelperInternalViews() []*view.View {

return []*view.View{
{
Name: currentlyDispatchedBatches.Name(),
Description: currentlyDispatchedBatches.Description(),
Measure: currentlyDispatchedBatches,
Aggregation: view.Count(),
TagKeys: []tag.Key{queueNameKey},
},
{
Name: totalDispatchedBatches.Name(),
Description: totalDispatchedBatches.Description(),
Measure: totalDispatchedBatches,
Aggregation: view.Sum(),
TagKeys: []tag.Key{queueNameKey},
},
}
}

var onceMetrics sync.Once

// RegisterMetrics registers a set of metric views used by the internal package
func RegisterMetrics() {
onceMetrics.Do(func() {
_ = view.Register(ExporterHelperInternalViews()...)
})
}
20 changes: 20 additions & 0 deletions exporter/exporterhelper/internal/observability_stable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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.

//go:build !enable_unstable
// +build !enable_unstable

package internal

func RegisterMetrics() {}
4 changes: 4 additions & 0 deletions exporter/exporterhelper/internal/persistent_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ func (pcs *persistentContiguousStorage) itemDispatchingStart(ctx context.Context
pcs.logger.Debug("Failed updating currently dispatched items",
zap.String(zapQueueNameKey, pcs.queueName), zap.Error(err))
}

recordCurrentlyDispatchedBatches(ctx, len(pcs.currentlyDispatchedItems), pcs.queueName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we only record these new metrics for persistent queue or also for in-memory queue when persistent queue is disabled?
I think they are useful regardless of whether persistence is enabled or no.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was wondering about doing that for in-memory queue as well though it seems it's using the old API and metrics would need to be pretty much redone there

}

// itemDispatchingFinish removes the item from the list of currently dispatched items and deletes it from the persistent queue
Expand All @@ -380,6 +382,8 @@ func (pcs *persistentContiguousStorage) itemDispatchingFinish(ctx context.Contex
pcs.logger.Debug("Failed updating currently dispatched items",
zap.String(zapQueueNameKey, pcs.queueName), zap.Error(err))
}

recordBatchDispatched(ctx, pcs.queueName)
}

func (pcs *persistentContiguousStorage) updateReadIndex(ctx context.Context) {
Expand Down
33 changes: 33 additions & 0 deletions exporter/exporterhelper/internal/persistent_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"time"

"github.com/stretchr/testify/require"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -169,6 +171,37 @@ func TestPersistentStorage_CurrentlyProcessedItems(t *testing.T) {
}
}

func TestPersistentStorage_MetricsReported(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)

traces := newTraces(5, 10)
req := newFakeTracesRequest(traces)

ext := createStorageExtension(path)
client := createTestClient(ext)
ps := createTestPersistentStorage(client)

RegisterMetrics()

for i := 0; i < 5; i++ {
err := ps.put(req)
require.NoError(t, err)
}

_ = getItemFromChannel(t, ps)
requireCurrentlyDispatchedItemsEqual(t, ps, []itemIndex{0, 1})

dd, err := view.RetrieveData("/currently_dispatched_batches")
require.NoError(t, err)
require.Equal(t, 1, len(dd))
require.Equal(t, 1, len(dd[0].Tags))
require.Equal(t, tag.Tag{Key: queueNameKey, Value: "foo"}, dd[0].Tags[0])
require.Equal(t, int64(2), dd[0].Data.(*view.CountData).Value)

ps.stop()
}

func TestPersistentStorage_RepeatPutCloseReadClose(t *testing.T) {
path := createTemporaryDirectory()
defer os.RemoveAll(path)
Expand Down