|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package extension_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "go.opentelemetry.io/collector/component" |
| 11 | + "go.opentelemetry.io/collector/extension" |
| 12 | +) |
| 13 | + |
| 14 | +// exampleExtension is a minimal extension that reports its lifecycle. |
| 15 | +type exampleExtension struct { |
| 16 | + id component.ID |
| 17 | +} |
| 18 | + |
| 19 | +// Start is called by the Collector when the extension is started. |
| 20 | +func (e *exampleExtension) Start(_ context.Context, _ component.Host) error { |
| 21 | + fmt.Printf("extension %q started\n", e.id) |
| 22 | + return nil |
| 23 | +} |
| 24 | + |
| 25 | +// Shutdown is called by the Collector when the extension is stopped. |
| 26 | +func (e *exampleExtension) Shutdown(_ context.Context) error { |
| 27 | + fmt.Printf("extension %q stopped\n", e.id) |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +func createExampleConfig() component.Config { |
| 32 | + return struct{}{} |
| 33 | +} |
| 34 | + |
| 35 | +func createExampleExtension(_ context.Context, set extension.Settings, _ component.Config) (extension.Extension, error) { |
| 36 | + return &exampleExtension{id: set.ID}, nil |
| 37 | +} |
| 38 | + |
| 39 | +// Example demonstrates how a Factory creates an extension using Settings.ID. |
| 40 | +func Example() { |
| 41 | + extType := component.MustNewType("example") |
| 42 | + |
| 43 | + factory := extension.NewFactory( |
| 44 | + extType, |
| 45 | + createExampleConfig, |
| 46 | + createExampleExtension, |
| 47 | + component.StabilityLevelDevelopment, |
| 48 | + ) |
| 49 | + |
| 50 | + settings := extension.Settings{ |
| 51 | + ID: component.NewID(extType), |
| 52 | + } |
| 53 | + |
| 54 | + ext, err := factory.Create(context.Background(), settings, factory.CreateDefaultConfig()) |
| 55 | + if err != nil { |
| 56 | + panic(err) |
| 57 | + } |
| 58 | + |
| 59 | + _ = ext.Start(context.Background(), nil) |
| 60 | + _ = ext.Shutdown(context.Background()) |
| 61 | + |
| 62 | + // Output: |
| 63 | + // extension "example" started |
| 64 | + // extension "example" stopped |
| 65 | +} |
0 commit comments