| 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 |
// prevent unkeyed literal initialization |
| 33 |
_ struct{} |
| 34 |
} |
| 35 |
|
| 36 |
// GetHTTPClientRoundTripper attempts to select the appropriate |
| 37 |
// extensionmiddleware.HTTPClient from the map of extensions, and |
| 38 |
// returns the HTTP client wrapper function. If a middleware is not |
| 39 |
// found, an error is returned. This should only be used by HTTP |
| 40 |
// clients. |
| 41 |
func (m Config) GetHTTPClientRoundTripper(_ context.Context, extensions map[component.ID]component.Component) (func(http.RoundTripper) (http.RoundTripper, error), error) { |
| 42 |
if ext, found := extensions[m.ID]; found { |
| 43 |
if client, ok := ext.(extensionmiddleware.HTTPClient); ok { |
| 44 |
return client.GetHTTPRoundTripper, nil |
| 45 |
} |
| 46 |
return nil, errNotHTTPClient |
| 47 |
} |
| 48 |
return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 49 |
} |
| 50 |
|
| 51 |
// GetHTTPServerHandler attempts to select the appropriate |
| 52 |
// extensionmiddleware.HTTPServer from the map of extensions, and |
| 53 |
// returns the http.Handler wrapper function. If a middleware is not |
| 54 |
// found, an error is returned. This should only be used by HTTP |
| 55 |
// servers. |
| 56 |
func (m Config) GetHTTPServerHandler(_ context.Context, extensions map[component.ID]component.Component) (func(http.Handler) (http.Handler, error), error) { |
| 57 |
if ext, found := extensions[m.ID]; found { |
| 58 |
if server, ok := ext.(extensionmiddleware.HTTPServer); ok { |
| 59 |
return server.GetHTTPHandler, nil |
| 60 |
} |
| 61 |
return nil, errNotHTTPServer |
| 62 |
} |
| 63 |
|
| 64 |
return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 65 |
} |
| 66 |
|
| 67 |
// GetGRPCClientOptions attempts to select the appropriate |
| 68 |
// extensionmiddleware.GRPCClient from the map of extensions, and |
| 69 |
// returns the gRPC dial options. If a middleware is not found, an |
| 70 |
// error is returned. This should only be used by gRPC clients. |
| 71 |
func (m Config) GetGRPCClientOptions(_ context.Context, extensions map[component.ID]component.Component) ([]grpc.DialOption, error) { |
| 72 |
if ext, found := extensions[m.ID]; found { |
| 73 |
if client, ok := ext.(extensionmiddleware.GRPCClient); ok { |
| 74 |
return client.GetGRPCClientOptions() |
| 75 |
} |
| 76 |
return nil, errNotGRPCClient |
| 77 |
} |
| 78 |
return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 79 |
} |
| 80 |
|
| 81 |
// GetGRPCServerOptions attempts to select the appropriate |
| 82 |
// extensionmiddleware.GRPCServer from the map of extensions, and |
| 83 |
// returns the gRPC server options. If a middleware is not found, an |
| 84 |
// error is returned. This should only be used by gRPC servers. |
| 85 |
func (m Config) GetGRPCServerOptions(_ context.Context, extensions map[component.ID]component.Component) ([]grpc.ServerOption, error) { |
| 86 |
if ext, found := extensions[m.ID]; found { |
| 87 |
if server, ok := ext.(extensionmiddleware.GRPCServer); ok { |
| 88 |
return server.GetGRPCServerOptions() |
| 89 |
} |
| 90 |
return nil, errNotGRPCServer |
| 91 |
} |
| 92 |
|
| 93 |
return nil, fmt.Errorf("failed to resolve middleware %q: %w", m.ID, errMiddlewareNotFound) |
| 94 |
} |