Skip to content

Commit

Permalink
Move extensions for internal to public package (#5606)
Browse files Browse the repository at this point in the history
Updates #5564

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Jul 25, 2022
1 parent c769779 commit ce1603b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 28 deletions.
22 changes: 22 additions & 0 deletions service/extensions/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 extensions // import "go.opentelemetry.io/collector/service/extensions"

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

// Config represents the ordered list of extensions configured for the service.
type Config []config.ComponentID
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package extensions // import "go.opentelemetry.io/collector/service/internal/extensions"
package extensions // import "go.opentelemetry.io/collector/service/extensions"

import (
"context"
Expand All @@ -37,8 +37,8 @@ type Extensions struct {
extMap map[config.ComponentID]component.Extension
}

// StartAll starts all extensions.
func (bes *Extensions) StartAll(ctx context.Context, host component.Host) error {
// Start starts all extensions.
func (bes *Extensions) Start(ctx context.Context, host component.Host) error {
bes.telemetry.Logger.Info("Starting extensions...")
for extID, ext := range bes.extMap {
extLogger := extensionLogger(bes.telemetry.Logger, extID)
Expand All @@ -51,8 +51,8 @@ func (bes *Extensions) StartAll(ctx context.Context, host component.Host) error
return nil
}

// ShutdownAll stops all extensions.
func (bes *Extensions) ShutdownAll(ctx context.Context) error {
// Shutdown stops all extensions.
func (bes *Extensions) Shutdown(ctx context.Context) error {
bes.telemetry.Logger.Info("Stopping extensions...")
var errs error
for _, ext := range bes.extMap {
Expand Down Expand Up @@ -128,18 +128,15 @@ type Settings struct {

// Factories maps extension type names in the config to the respective component.ExtensionFactory.
Factories map[config.Type]component.ExtensionFactory

// ServiceExtensions are the ordered list of extensions configured for the service.
ServiceExtensions []config.ComponentID
}

// Build builds Extensions from config.
func Build(ctx context.Context, set Settings) (*Extensions, error) {
// New creates a new Extensions from Config.
func New(ctx context.Context, set Settings, cfg Config) (*Extensions, error) {
exts := &Extensions{
telemetry: set.Telemetry,
extMap: make(map[config.ComponentID]component.Extension),
}
for _, extID := range set.ServiceExtensions {
for _, extID := range cfg {
extCfg, existsCfg := set.Configs[extID]
if !existsCfg {
return nil, fmt.Errorf("extension %q is not configured", extID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@ func TestBuildExtensions(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Build(context.Background(), Settings{
Telemetry: componenttest.NewNopTelemetrySettings(),
BuildInfo: component.NewDefaultBuildInfo(),
Configs: tt.extensionsConfigs,
Factories: tt.factories.Extensions,
ServiceExtensions: tt.serviceExtensions,
})
_, err := New(context.Background(), Settings{
Telemetry: componenttest.NewNopTelemetrySettings(),
BuildInfo: component.NewDefaultBuildInfo(),
Configs: tt.extensionsConfigs,
Factories: tt.factories.Extensions,
}, tt.serviceExtensions)
require.Error(t, err)
assert.EqualError(t, err, tt.wantErrMsg)
})
Expand Down
2 changes: 1 addition & 1 deletion service/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package service // import "go.opentelemetry.io/collector/service"
import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/service/internal/extensions"
"go.opentelemetry.io/collector/service/extensions"
"go.opentelemetry.io/collector/service/internal/pipelines"
)

Expand Down
17 changes: 8 additions & 9 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/service/extensions"
"go.opentelemetry.io/collector/service/internal"
"go.opentelemetry.io/collector/service/internal/extensions"
"go.opentelemetry.io/collector/service/internal/pipelines"
"go.opentelemetry.io/collector/service/internal/telemetry"
"go.opentelemetry.io/collector/service/internal/telemetrylogs"
Expand Down Expand Up @@ -73,13 +73,12 @@ func newService(set *settings) (*service, error) {
srv.telemetrySettings.MeterProvider = srv.telemetryInitializer.mp

extensionsSettings := extensions.Settings{
Telemetry: srv.telemetrySettings,
BuildInfo: srv.buildInfo,
Configs: srv.config.Extensions,
Factories: srv.host.factories.Extensions,
ServiceExtensions: srv.config.Service.Extensions,
Telemetry: srv.telemetrySettings,
BuildInfo: srv.buildInfo,
Configs: srv.config.Extensions,
Factories: srv.host.factories.Extensions,
}
if srv.host.extensions, err = extensions.Build(context.Background(), extensionsSettings); err != nil {
if srv.host.extensions, err = extensions.New(context.Background(), extensionsSettings, srv.config.Service.Extensions); err != nil {
return nil, fmt.Errorf("failed build extensions: %w", err)
}

Expand Down Expand Up @@ -109,7 +108,7 @@ func newService(set *settings) (*service, error) {
}

func (srv *service) Start(ctx context.Context) error {
if err := srv.host.extensions.StartAll(ctx, srv.host); err != nil {
if err := srv.host.extensions.Start(ctx, srv.host); err != nil {
return fmt.Errorf("failed to start extensions: %w", err)
}

Expand All @@ -132,7 +131,7 @@ func (srv *service) Shutdown(ctx context.Context) error {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown pipelines: %w", err))
}

if err := srv.host.extensions.ShutdownAll(ctx); err != nil {
if err := srv.host.extensions.Shutdown(ctx); err != nil {
errs = multierr.Append(errs, fmt.Errorf("failed to shutdown extensions: %w", err))
}

Expand Down

0 comments on commit ce1603b

Please sign in to comment.