Skip to content

Commit

Permalink
STAC-21861: Add new processor to deliver usage metric
Browse files Browse the repository at this point in the history
  • Loading branch information
aacevedoosorio committed Nov 15, 2024
1 parent b510b17 commit ba1869e
Show file tree
Hide file tree
Showing 17 changed files with 956 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
go 1.21.6
go 1.22.0

use (
./common
./connector/stsservicegraphconnector
./exporter/clickhousestsexporter
./exporter/ststopologyexporter
./extension/ingestionapikeyauthextension
./processor/stsusageprocessor
)
399 changes: 399 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions processor/stsusageprocessor/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
22 changes: 22 additions & 0 deletions processor/stsusageprocessor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Resource Processor

<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [beta]: traces, metrics, logs |
| Distributions | [sts] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aprocessor%2Fstsusage%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aprocessor%2Fstsusage) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aprocessor%2Fstsusage%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aprocessor%2Fstsusage) |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | |

[beta]: https://github.com/open-telemetry/opentelemetry-collector/blob/main/docs/component-stability.md#beta
[sts]:
<!-- end autogenerated section -->


Examples:

```yaml
processors:
stsusageprocessor: {}
```
19 changes: 19 additions & 0 deletions processor/stsusageprocessor/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package stsusageprocessor // "import github.com/stackvista/sts-opentelemetry-collector/processor/stsusageprocessor"

import (
"go.opentelemetry.io/collector/component"
)

// Config defines configuration for Resource processor.
type Config struct {
}

var _ component.Config = (*Config)(nil)

// Validate checks if the processor configuration is valid
func (cfg *Config) Validate() error {
return nil
}
8 changes: 8 additions & 0 deletions processor/stsusageprocessor/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

// Package resourceprocessor implements a processor for
// applying changes on resource attributes.
package stsusageprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourceprocessor"
90 changes: 90 additions & 0 deletions processor/stsusageprocessor/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

package stsusageprocessor // import "github.com/stackvista/sts-opentelemetry-collector/processor/stsusageprocessor"

import (
"context"
"github.com/stackvista/sts-opentelemetry-collector/processor/stsusageprocessor/internal/metadata"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/metric"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/processor"
"go.opentelemetry.io/collector/processor/processorhelper"
)

var processorCapabilities = consumer.Capabilities{MutatesData: true}

// NewFactory returns a new factory for the Resource processor.
func NewFactory() processor.Factory {
return processor.NewFactory(
metadata.Type,
createDefaultConfig,
processor.WithTraces(createTracesProcessor, metadata.TracesStability),
processor.WithMetrics(createMetricsProcessor, metadata.MetricsStability),
processor.WithLogs(createLogsProcessor, metadata.LogsStability))
}

// Note: This isn't a valid configuration because the processor would do no work.
func createDefaultConfig() component.Config {
return &Config{}
}

func createTracesProcessor(
ctx context.Context,
set processor.CreateSettings,
cfg component.Config,
nextConsumer consumer.Traces,
) (processor.Traces, error) {
meter := set.MeterProvider.Meter("stsusageprocessor")
tracesBytesCounter, _ := meter.Int64Counter(
"suse_observability_usage_bytes_accepted_traces",
metric.WithDescription("Uncompressed amount of bytes received by any traces"),
metric.WithUnit("By"),
)

proc := &stsUsageProcessor{logger: set.Logger, sizer: &ptrace.ProtoMarshaler{}, tracesBytes: tracesBytesCounter}
return processorhelper.NewTracesProcessor(
ctx,
set,
cfg,
nextConsumer,
proc.processTraces,
processorhelper.WithCapabilities(processorCapabilities))
}

func createMetricsProcessor(
ctx context.Context,
set processor.CreateSettings,
cfg component.Config,
nextConsumer consumer.Metrics,
) (processor.Metrics, error) {
proc := &stsUsageProcessor{logger: set.Logger}
return processorhelper.NewMetricsProcessor(
ctx,
set,
cfg,
nextConsumer,
proc.processMetrics,
processorhelper.WithCapabilities(processorCapabilities))
}

func createLogsProcessor(
ctx context.Context,
set processor.CreateSettings,
cfg component.Config,
nextConsumer consumer.Logs,
) (processor.Logs, error) {
proc := &stsUsageProcessor{logger: set.Logger}
return processorhelper.NewLogsProcessor(
ctx,
set,
cfg,
nextConsumer,
proc.processLogs,
processorhelper.WithCapabilities(processorCapabilities))
}
34 changes: 34 additions & 0 deletions processor/stsusageprocessor/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package stsusageprocessor

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/processor/processortest"
)

func TestCreateDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, componenttest.CheckConfigStruct(cfg))
assert.NotNil(t, cfg)
}

func TestCreateProcessor(t *testing.T) {
factory := NewFactory()
cfg := &Config{}

tp, err := factory.CreateTracesProcessor(context.Background(), processortest.NewNopCreateSettings(), cfg, consumertest.NewNop())
assert.NoError(t, err)
assert.NotNil(t, tp)

mp, err := factory.CreateMetricsProcessor(context.Background(), processortest.NewNopCreateSettings(), cfg, consumertest.NewNop())
assert.NoError(t, err)
assert.NotNil(t, mp)
}
149 changes: 149 additions & 0 deletions processor/stsusageprocessor/generated_component_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions processor/stsusageprocessor/generated_package_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ba1869e

Please sign in to comment.