|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package configmiddleware implements a configuration struct to |
| 5 | +// name middleware extensions. |
| 6 | +package configmiddleware // import "go.opentelemetry.io/collector/config/configmiddleware" |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "net/http" |
| 13 | + |
| 14 | + "google.golang.org/grpc" |
| 15 | + |
| 16 | + "go.opentelemetry.io/collector/component" |
| 17 | + "go.opentelemetry.io/collector/extension/extensionmiddleware" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + errMiddlewareNotFound = errors.New("middleware not found") |
| 22 | + errNotHTTPServer = errors.New("requested extension is not an HTTP server middleware") |
| 23 | + errNotGRPCServer = errors.New("requested extension is not a gRPC server middleware") |
| 24 | + errNotHTTPClient = errors.New("requested extension is not an HTTP client middleware") |
| 25 | + errNotGRPCClient = errors.New("requested extension is not a gRPC client middleware") |
| 26 | +) |
| 27 | + |
| 28 | +// Middleware defines the extension ID for a middleware component. |
| 29 | +type Config struct { |
| 30 | + // ID specifies the name of the extension to use. |
| 31 | + ID component.ID `mapstructure:"id,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +// GetHTTPClientRoundTripper attempts to select the appropriate |
| 35 | +// extensionmiddleware.HTTPClient from the map of extensions, and |
| 36 | +// returns the HTTP client wrapper function. If a middleware is not |
| 37 | +// found, an error is returned. This should only be used by HTTP |
| 38 | +// clients. |
| 39 | +func (m Config) GetHTTPClientRoundTripper(_ context.Context, extensions map[component.ID]component.Component) (func(http.RoundTripper) (http.RoundTripper, error), error) { |
| 40 | + if ext, found := extensions[m.ID]; found { |
| 41 | + if client, ok := ext.(extensionmiddleware.HTTPClient); ok { |
| 42 | + return client.GetHTTPRoundTripper, nil |
| 43 | + } |
| 44 | + return nil, errNotHTTPClient |
| 45 | + } |
| 46 | + return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 47 | +} |
| 48 | + |
| 49 | +// GetHTTPServerHandler attempts to select the appropriate |
| 50 | +// extensionmiddleware.HTTPServer from the map of extensions, and |
| 51 | +// returns the http.Handler wrapper function. If a middleware is not |
| 52 | +// found, an error is returned. This should only be used by HTTP |
| 53 | +// servers. |
| 54 | +func (m Config) GetHTTPServerHandler(_ context.Context, extensions map[component.ID]component.Component) (func(http.Handler) (http.Handler, error), error) { |
| 55 | + if ext, found := extensions[m.ID]; found { |
| 56 | + if server, ok := ext.(extensionmiddleware.HTTPServer); ok { |
| 57 | + return server.GetHTTPHandler, nil |
| 58 | + } |
| 59 | + return nil, errNotHTTPServer |
| 60 | + } |
| 61 | + |
| 62 | + return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 63 | +} |
| 64 | + |
| 65 | +// GetGRPCClientOptions attempts to select the appropriate |
| 66 | +// extensionmiddleware.GRPCClient from the map of extensions, and |
| 67 | +// returns the gRPC dial options. If a middleware is not found, an |
| 68 | +// error is returned. This should only be used by gRPC clients. |
| 69 | +func (m Config) GetGRPCClientOptions(_ context.Context, extensions map[component.ID]component.Component) ([]grpc.DialOption, error) { |
| 70 | + if ext, found := extensions[m.ID]; found { |
| 71 | + if client, ok := ext.(extensionmiddleware.GRPCClient); ok { |
| 72 | + return client.GetGRPCClientOptions() |
| 73 | + } |
| 74 | + return nil, errNotGRPCClient |
| 75 | + } |
| 76 | + return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 77 | +} |
| 78 | + |
| 79 | +// GetGRPCServerOptions attempts to select the appropriate |
| 80 | +// extensionmiddleware.GRPCServer from the map of extensions, and |
| 81 | +// returns the gRPC server options. If a middleware is not found, an |
| 82 | +// error is returned. This should only be used by gRPC servers. |
| 83 | +func (m Config) GetGRPCServerOptions(_ context.Context, extensions map[component.ID]component.Component) ([]grpc.ServerOption, error) { |
| 84 | + if ext, found := extensions[m.ID]; found { |
| 85 | + if server, ok := ext.(extensionmiddleware.GRPCServer); ok { |
| 86 | + return server.GetGRPCServerOptions() |
| 87 | + } |
| 88 | + return nil, errNotGRPCServer |
| 89 | + } |
| 90 | + |
| 91 | + return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 92 | +} |
0 commit comments