| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package client contains generic representations of clients connecting to |
| 5 |
// different receivers. Components, such as processors or exporters, can make |
| 6 |
// use of this information to make decisions related to grouping of batches, |
| 7 |
// tenancy, load balancing, tagging, among others. |
| 8 |
// |
| 9 |
// The structs defined here are typically used within the context that is |
| 10 |
// propagated down the pipeline, with the values being produced by |
| 11 |
// authenticators and/or receivers, and consumed by processors and exporters. |
| 12 |
// |
| 13 |
// # Producers |
| 14 |
// |
| 15 |
// Receivers are responsible for obtaining a client.Info from the current |
| 16 |
// context and enhancing the client.Info with the net.Addr from the peer, |
| 17 |
// storing a new client.Info into the context that it passes down. For HTTP |
| 18 |
// requests, the net.Addr is typically the IP address of the client. |
| 19 |
// |
| 20 |
// Typically, however, receivers would delegate this processing to helpers such |
| 21 |
// as the confighttp or configgrpc packages: both contain interceptors that will |
| 22 |
// enhance the context with the client.Info, such that no actions are needed by |
| 23 |
// receivers that are built using confighttp.HTTPServerSettings or |
| 24 |
// configgrpc.GRPCServerSettings. |
| 25 |
// |
| 26 |
// Authenticators are responsible for obtaining a client.Info from the current |
| 27 |
// context, enhancing the client.Info with an implementation of client.AuthData, |
| 28 |
// and storing a new client.Info into the context that it passes down. The |
| 29 |
// attribute names should be documented with their return types and considered |
| 30 |
// part of the public API for the authenticator. |
| 31 |
// |
| 32 |
// # Consumers |
| 33 |
// |
| 34 |
// Provided that the pipeline does not contain processors that would discard or |
| 35 |
// rewrite the context, such as the batch processor, processors and exporters |
| 36 |
// have access to the client.Info via client.FromContext. Among other usages, |
| 37 |
// this data can be used to: |
| 38 |
// |
| 39 |
// - annotate data points with authentication data (username, tenant, ...) |
| 40 |
// |
| 41 |
// - route data points based on authentication data |
| 42 |
// |
| 43 |
// - rate limit client calls based on IP addresses |
| 44 |
// |
| 45 |
// Processors and exporters relying on the existence of data from the |
| 46 |
// client.Info, especially client.AuthData, should clearly document this as part |
| 47 |
// of the component's README file. The expected pattern for consuming data is to |
| 48 |
// allow users to specify the attribute name to use in the component. The |
| 49 |
// expected data type should also be communicated to users, who should then |
| 50 |
// compare this with the authenticators that are part of the pipeline. For |
| 51 |
// example, assuming that the OIDC authenticator pushes a "subject" string |
| 52 |
// attribute and that we have a hypothetical "authprinter" processor that prints |
| 53 |
// the "username" to the console, this is how an OpenTelemetry Collector |
| 54 |
// configuration would look like: |
| 55 |
// |
| 56 |
// extensions: |
| 57 |
// oidc: |
| 58 |
// issuer_url: http://localhost:8080/auth/realms/opentelemetry |
| 59 |
// audience: collector |
| 60 |
// receivers: |
| 61 |
// otlp: |
| 62 |
// protocols: |
| 63 |
// grpc: |
| 64 |
// auth: |
| 65 |
// authenticator: oidc |
| 66 |
// processors: |
| 67 |
// authprinter: |
| 68 |
// attribute: subject |
| 69 |
// exporters: |
| 70 |
// debug: |
| 71 |
// service: |
| 72 |
// extensions: [oidc] |
| 73 |
// pipelines: |
| 74 |
// traces: |
| 75 |
// receivers: [otlp] |
| 76 |
// processors: [authprinter] |
| 77 |
// exporters: [debug] |
| 78 |
package client // import "go.opentelemetry.io/collector/client" |
| 79 |
|
| 80 |
import ( |
| 81 |
"context" |
| 82 |
"iter" |
| 83 |
"maps" |
| 84 |
"net" |
| 85 |
"strings" |
| 86 |
) |
| 87 |
|
| 88 |
type ctxKey struct{} |
| 89 |
|
| 90 |
// Info contains data related to the clients connecting to receivers. |
| 91 |
type Info struct { |
| 92 |
// Addr for the client connecting to this collector. Available in a |
| 93 |
// best-effort basis, and generally reliable for receivers making use of |
| 94 |
// confighttp.ToServer and configgrpc.ToServerOption. |
| 95 |
Addr net.Addr |
| 96 |
|
| 97 |
// Auth information from the incoming request as provided by |
| 98 |
// configauth.ServerAuthenticator implementations tied to the receiver for |
| 99 |
// this connection. |
| 100 |
Auth AuthData |
| 101 |
|
| 102 |
// Metadata is the request metadata from the client connecting to this connector. |
| 103 |
Metadata Metadata |
| 104 |
|
| 105 |
// prevent unkeyed literal initialization |
| 106 |
_ struct{} |
| 107 |
} |
| 108 |
|
| 109 |
// AuthData represents the authentication data as seen by authenticators tied to |
| 110 |
// the receivers. |
| 111 |
type AuthData interface { |
| 112 |
// GetAttribute returns the value for the given attribute. Authenticator |
| 113 |
// implementations might define different data types for different |
| 114 |
// attributes. While "string" is used most of the time, a key named |
| 115 |
// "membership" might return a list of strings. |
| 116 |
GetAttribute(string) any |
| 117 |
|
| 118 |
// GetAttributeNames returns the names of all attributes in this authentication data. |
| 119 |
GetAttributeNames() []string |
| 120 |
} |
| 121 |
|
| 122 |
const MetadataHostName = "Host" |
| 123 |
|
| 124 |
// NewContext takes an existing context and derives a new context with the |
| 125 |
// client.Info value stored on it. |
| 126 |
func NewContext(ctx context.Context, c Info) context.Context { |
| 127 |
return context.WithValue(ctx, ctxKey{}, c) |
| 128 |
} |
| 129 |
|
| 130 |
// FromContext takes a context and returns a ClientInfo from it. |
| 131 |
// When a ClientInfo isn't present, a new empty one is returned. |
| 132 |
func FromContext(ctx context.Context) Info { |
| 133 |
c, ok := ctx.Value(ctxKey{}).(Info) |
| 134 |
if !ok { |
| 135 |
c = Info{} |
| 136 |
} |
| 137 |
return c |
| 138 |
} |
| 139 |
|
| 140 |
// Metadata is an immutable map, meant to contain request metadata. |
| 141 |
type Metadata struct { |
| 142 |
data map[string][]string |
| 143 |
} |
| 144 |
|
| 145 |
// NewMetadata creates a new Metadata object to use in Info. |
| 146 |
func NewMetadata(md map[string][]string) Metadata { |
| 147 |
c := make(map[string][]string, len(md)) |
| 148 |
for k, v := range md { |
| 149 |
c[strings.ToLower(k)] = v |
| 150 |
} |
| 151 |
return Metadata{ |
| 152 |
data: c, |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// Keys returns an iterator for the metadata keys. |
| 157 |
func (m Metadata) Keys() iter.Seq[string] { |
| 158 |
return maps.Keys(m.data) |
| 159 |
} |
| 160 |
|
| 161 |
// Get gets the value of the key from metadata, returning a copy. |
| 162 |
// The key lookup is case-insensitive. |
| 163 |
func (m Metadata) Get(key string) []string { |
| 164 |
if len(m.data) == 0 { |
| 165 |
return nil |
| 166 |
} |
| 167 |
|
| 168 |
vals := m.data[strings.ToLower(key)] |
| 169 |
if len(vals) == 0 { |
| 170 |
return nil |
| 171 |
} |
| 172 |
|
| 173 |
ret := make([]string, len(vals)) |
| 174 |
copy(ret, vals) |
| 175 |
|
| 176 |
return ret |
| 177 |
} |