Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions .chloggen/scraper-profiles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: scraper

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Implement scraper for Profiles.

# One or more tracking issues or pull requests related to the change
issues: [13915]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
40 changes: 36 additions & 4 deletions scraper/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,20 @@ type Factory interface {
// this function returns the error [pipeline.ErrSignalNotSupported].
CreateMetrics(ctx context.Context, set Settings, cfg component.Config) (Metrics, error)

// CreateProfiles creates a Profiles scraper based on this config.
// If the scraper type does not support profiles,
// this function returns the error [pipeline.ErrSignalNotSupported].
CreateProfiles(ctx context.Context, set Settings, cfg component.Config) (Profiles, error)

// LogsStability gets the stability level of the Logs scraper.
LogsStability() component.StabilityLevel

// MetricsStability gets the stability level of the Metrics scraper.
MetricsStability() component.StabilityLevel

// ProfilesStability gets the stability level of the Profiles scraper.
ProfilesStability() component.StabilityLevel

unexportedFactoryFunc()
}

Expand All @@ -68,10 +76,12 @@ func (f factoryOptionFunc) applyOption(o *factory) {
type factory struct {
cfgType component.Type
component.CreateDefaultConfigFunc
createLogsFunc CreateLogsFunc
createMetricsFunc CreateMetricsFunc
logsStabilityLevel component.StabilityLevel
metricsStabilityLevel component.StabilityLevel
createLogsFunc CreateLogsFunc
createMetricsFunc CreateMetricsFunc
createProfilesFunc CreateProfilesFunc
logsStabilityLevel component.StabilityLevel
metricsStabilityLevel component.StabilityLevel
profilesStabilityLevel component.StabilityLevel
}

func (f *factory) Type() component.Type {
Expand All @@ -88,6 +98,10 @@ func (f *factory) MetricsStability() component.StabilityLevel {
return f.metricsStabilityLevel
}

func (f *factory) ProfilesStability() component.StabilityLevel {
return f.profilesStabilityLevel
}

func (f *factory) CreateLogs(ctx context.Context, set Settings, cfg component.Config) (Logs, error) {
if f.createLogsFunc == nil {
return nil, pipeline.ErrSignalNotSupported
Expand All @@ -102,12 +116,22 @@ func (f *factory) CreateMetrics(ctx context.Context, set Settings, cfg component
return f.createMetricsFunc(ctx, set, cfg)
}

func (f *factory) CreateProfiles(ctx context.Context, set Settings, cfg component.Config) (Profiles, error) {
if f.createProfilesFunc == nil {
return nil, pipeline.ErrSignalNotSupported
}
return f.createProfilesFunc(ctx, set, cfg)
}

// CreateLogsFunc is the equivalent of Factory.CreateLogs().
type CreateLogsFunc func(context.Context, Settings, component.Config) (Logs, error)

// CreateMetricsFunc is the equivalent of Factory.CreateMetrics().
type CreateMetricsFunc func(context.Context, Settings, component.Config) (Metrics, error)

// CreateProfilesFunc is the equivalent of Factory.CreateProfiles().
type CreateProfilesFunc func(context.Context, Settings, component.Config) (Profiles, error)

// WithLogs overrides the default "error not supported" implementation for CreateLogs and the default "undefined" stability level.
func WithLogs(createLogs CreateLogsFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
Expand All @@ -124,6 +148,14 @@ func WithMetrics(createMetrics CreateMetricsFunc, sl component.StabilityLevel) F
})
}

// WithProfiles overrides the default "error not supported" implementation for CreateProfiles and the default "undefined" stability level.
func WithProfiles(createProfiles CreateProfilesFunc, sl component.StabilityLevel) FactoryOption {
return factoryOptionFunc(func(o *factory) {
o.profilesStabilityLevel = sl
o.createProfilesFunc = createProfiles
})
}

// NewFactory returns a Factory.
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
f := &factory{
Expand Down
9 changes: 5 additions & 4 deletions scraper/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
go.opentelemetry.io/collector/component v1.42.0
go.opentelemetry.io/collector/component/componenttest v0.136.0
go.opentelemetry.io/collector/pdata v1.42.0
go.opentelemetry.io/collector/pdata/pprofile v0.136.1-0.20250926084501-6ccdc890b16f
go.opentelemetry.io/collector/pipeline v1.42.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
Expand Down Expand Up @@ -34,10 +35,10 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect
go.opentelemetry.io/otel/trace v1.38.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.41.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.26.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250707201910-8d1bb00bc6a7 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.29.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250922171735-9219d122eba9 // indirect
google.golang.org/grpc v1.75.1 // indirect
google.golang.org/protobuf v1.36.9 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
18 changes: 10 additions & 8 deletions scraper/go.sum

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

43 changes: 43 additions & 0 deletions scraper/profiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package scraper // import "go.opentelemetry.io/collector/scraper"

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pprofile"
)

// Profiles is the base interface for profiles scrapers.
type Profiles interface {
component.Component

// ScrapeProfiles is the base interface to indicate that how should profiles be scraped.
ScrapeProfiles(context.Context) (pprofile.Profiles, error)
}

// ScrapeProfilesFunc is a helper function.
type ScrapeProfilesFunc ScrapeFunc[pprofile.Profiles]

func (sf ScrapeProfilesFunc) ScrapeProfiles(ctx context.Context) (pprofile.Profiles, error) {
return sf(ctx)
}

type profiles struct {
baseScraper
ScrapeProfilesFunc
}

// NewProfiles creates a new Profiles scraper.
func NewProfiles(scrape ScrapeProfilesFunc, options ...Option) (Profiles, error) {
if scrape == nil {
return nil, errNilFunc
}
bs := &profiles{
baseScraper: newBaseScraper(options),
ScrapeProfilesFunc: scrape,
}
return bs, nil
}
78 changes: 78 additions & 0 deletions scraper/profiles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package scraper

import (
"context"
"errors"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/pdata/pprofile"
)

func TestNewProfiles(t *testing.T) {
mp, err := NewProfiles(newTestScrapeProfilesFunc(nil))
require.NoError(t, err)

require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost()))
md, err := mp.ScrapeProfiles(context.Background())
require.NoError(t, err)
assert.Equal(t, pprofile.NewProfiles(), md)
require.NoError(t, mp.Shutdown(context.Background()))
}

func TestNewProfiles_WithOptions(t *testing.T) {
want := errors.New("my_error")
mp, err := NewProfiles(newTestScrapeProfilesFunc(nil),
WithStart(func(context.Context, component.Host) error { return want }),
WithShutdown(func(context.Context) error { return want }))
require.NoError(t, err)

assert.Equal(t, want, mp.Start(context.Background(), componenttest.NewNopHost()))
assert.Equal(t, want, mp.Shutdown(context.Background()))
}

func TestNewProfiles_NilRequiredFields(t *testing.T) {
_, err := NewProfiles(nil)
require.Error(t, err)
}

func TestNewProfiles_ProcessProfilesError(t *testing.T) {
want := errors.New("my_error")
mp, err := NewProfiles(newTestScrapeProfilesFunc(want))
require.NoError(t, err)
_, err = mp.ScrapeProfiles(context.Background())
require.ErrorIs(t, err, want)
}

func TestProfilesConcurrency(t *testing.T) {
mp, err := NewProfiles(newTestScrapeProfilesFunc(nil))
require.NoError(t, err)
require.NoError(t, mp.Start(context.Background(), componenttest.NewNopHost()))

var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 10000; j++ {
_, errScrape := mp.ScrapeProfiles(context.Background())
assert.NoError(t, errScrape)
}
}()
}
wg.Wait()
require.NoError(t, mp.Shutdown(context.Background()))
}

func newTestScrapeProfilesFunc(retError error) ScrapeProfilesFunc {
return func(_ context.Context) (pprofile.Profiles, error) {
return pprofile.NewProfiles(), retError
}
}
Loading