go.uber.org/yarpc@v1.72.1/yarpcconfig/doc.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 // Package yarpcconfig implements a generic configuration system that may be 22 // used to build YARPC Dispatchers from configurations specified in different 23 // markup formats. 24 // 25 // Usage 26 // 27 // To build a Dispatcher, first create a new Configurator. This object will be 28 // responsible for loading your configuration. It does not yet know about the 29 // different transports, peer lists, etc. that you want to use. You can inform 30 // the Configurator about the different transports, peer lists, etc. by 31 // registering them using RegisterTransport, RegisterPeerChooser, 32 // RegisterPeerList, and RegisterPeerListUpdater. 33 // 34 // cfg := config.New() 35 // cfg.MustRegisterTransport(http.TransportSpec()) 36 // cfg.MustRegisterPeerList(roundrobin.Spec()) 37 // 38 // This object is re-usable and may be stored as a singleton in your 39 // application. Custom transports, peer lists, and peer list updaters may be 40 // integrated with the configuration system by registering more 41 // TransportSpecs, PeerChooserSpecs, PeerListSpecs, and PeerListUpdaterSpecs 42 // with it. 43 // 44 // Use LoadConfigFromYAML to load a yarpc.Config from YAML and pass that to 45 // yarpc.NewDispatcher. 46 // 47 // c, err := cfg.LoadConfigFromYAML("myservice", yamlConfig) 48 // if err != nil { 49 // log.Fatal(err) 50 // } 51 // dispatcher := yarpc.NewDispatcher(c) 52 // 53 // If you have already parsed your configuration from a different format, pass 54 // the parsed data to LoadConfig instead. 55 // 56 // var m map[string]interface{} = ... 57 // c, err := cfg.LoadConfig("myservice", m) 58 // if err != nil { 59 // log.Fatal(err) 60 // } 61 // dispatcher := yarpc.NewDispatcher(c) 62 // 63 // NewDispatcher or NewDispatcherFromYAML may be used to get a 64 // yarpc.Dispatcher directly instead of a yarpc.Config. 65 // 66 // dispatcher, err := cfg.NewDispatcherFromYAML("myservice", yamlConfig) 67 // 68 // Configuration parameters for the different transports, inbounds, and 69 // outbounds are defined in the TransportSpecs that were registered against 70 // the Configurator. A TransportSpec uses this information to build the 71 // corresponding Transport, Inbound and Outbound objects. 72 // 73 // Configuration 74 // 75 // The configuration may be specified in YAML or as any Go-level 76 // map[string]interface{}. The examples below use YAML for illustration 77 // purposes but other markup formats may be parsed into map[string]interface{} 78 // as long as the information provided is the same. 79 // 80 // The configuration accepts the following top-level attributes: transports, 81 // inbounds, and outbounds. 82 // 83 // inbounds: 84 // # ... 85 // outbounds: 86 // # ... 87 // transports: 88 // # ... 89 // logging: 90 // # ... 91 // 92 // See the following sections for details on the logging, transports, 93 // inbounds, and outbounds keys in the configuration. 94 // 95 // Inbound Configuration 96 // 97 // The 'inbounds' attribute configures the different ways in which the service 98 // receives requests. It is represented as a mapping between inbound transport 99 // type and its configuration. For example, the following states that we want 100 // to receive requests over HTTP. 101 // 102 // inbounds: 103 // http: 104 // address: :8080 105 // 106 // (For details on the configuration parameters of individual transport types, 107 // check the documentation for the corresponding transport package.) 108 // 109 // If you want multiple inbounds of the same type, specify a different name 110 // for it and add a 'type' attribute to its configuration: 111 // 112 // inbounds: 113 // http: 114 // address: :8081 115 // http-deprecated: 116 // type: http 117 // address: :8080 118 // 119 // Any inbound can be disabled by adding a 'disabled' attribute. 120 // 121 // inbounds: 122 // http: 123 // address: :8080 124 // http-deprecated: 125 // type: http 126 // disabled: true 127 // address: :8081 128 // 129 // Outbound Configuration 130 // 131 // The 'outbounds' attribute configures how this service makes requests to 132 // other YARPC-compatible services. It is represented as mapping between 133 // service name and outbound configuration. 134 // 135 // outbounds: 136 // keyvalue: 137 // # .. 138 // anotherservice: 139 // # .. 140 // 141 // (For details on the configuration parameters of individual transport types, 142 // check the documentation for the corresponding transport package.) 143 // 144 // The outbound configuration for a service has at least one of the following 145 // keys: unary, oneway. These specify the configurations for the corresponding 146 // RPC types for that service. For example, the following specifies that we 147 // make Unary requests to keyvalue service over TChannel and Oneway requests over 148 // HTTP. 149 // 150 // keyvalue: 151 // unary: 152 // tchannel: 153 // peer: 127.0.0.1:4040 154 // oneway: 155 // http: 156 // url: http://127.0.0.1:8080/ 157 // 158 // For convenience, if there is only one outbound configuration for a service, 159 // it may be specified one level higher (without the 'unary' or 'oneway' 160 // attributes). In this case, that transport will be used to send requests for 161 // all compatible RPC types. For example, the HTTP transport supports both, 162 // Unary and Oneway RPC types so the following states that requests for both 163 // RPC types must be made over HTTP. 164 // 165 // keyvalue: 166 // http: 167 // url: http://127.0.0.1:8080/ 168 // 169 // Similarly, the following states that we only make Oneway requests to the 170 // "email" service and those are always made over HTTP. 171 // 172 // email: 173 // http: 174 // url: http://127.0.0.1:8080/ 175 // 176 // When the name of the target service differs from the outbound name, it may 177 // be overridden with the 'service' key. 178 // 179 // keyvalue: 180 // unary: 181 // # ... 182 // oneway: 183 // # ... 184 // keyvalue-staging: 185 // service: keyvalue 186 // unary: 187 // # ... 188 // oneway: 189 // # ... 190 // 191 // Peer Configuration 192 // 193 // Transports that support peer management and selection through YARPC accept 194 // some additional keys in their outbound configuration. 195 // 196 // An explicit peer may be specified for a supported transport by using the 197 // `peer` option. 198 // 199 // keyvalue: 200 // tchannel: 201 // peer: 127.0.0.1:4040 202 // 203 // All requests made to this outbound will be made through this peer. 204 // 205 // If a peer list was registered with the system, the name of the peer list 206 // may be used to specify a more complex peer selection and load balancing 207 // strategy. 208 // 209 // keyvalue: 210 // http: 211 // url: https://host/yarpc 212 // round-robin: 213 // peers: 214 // - 127.0.0.1:8080 215 // - 127.0.0.1:8081 216 // - 127.0.0.1:8082 217 // 218 // In the example above, the system will round-robin the requests between the 219 // different addresses. In case of the HTTP transport, the URL will be used as 220 // a template for the HTTP requests made to these hosts. 221 // 222 // Finally, the TransportSpec for a Transport may include named presets for 223 // peer lists in its definition. These may be referenced by name in the config 224 // using the `with` key. 225 // 226 // # Given a preset "dev-proxy" that was included in the TransportSpec, the 227 // # following is valid. 228 // keyvalue: 229 // http: 230 // url: https://host/yarpc 231 // with: dev-proxy 232 // 233 // Transport Configuration 234 // 235 // The 'transports' attribute configures the Transport objects that are shared 236 // between all inbounds and outbounds of that transport type. It is 237 // represented as a mapping between the transport name and its configuration. 238 // 239 // transports: 240 // http: 241 // keepAlive: 30s 242 // 243 // (For details on the configuration parameters of individual transport types, 244 // check the documentation for the corresponding transport package.) 245 // 246 // Logging Configuration 247 // 248 // The 'logging' attribute configures how YARPC's observability middleware 249 // logs output. 250 // 251 // logging: 252 // levels: 253 // # ... 254 // 255 // The following keys are supported under the 'levels' key, 256 // 257 // success 258 // Configures the level at which successful requests are logged. 259 // Defaults to "debug". 260 // serverError 261 // Configures the level at which server errors are logged. 262 // Defaults to "error". 263 // clientError 264 // Configures the level at which all client errors are logged. 265 // All Thrift exceptions are considered application errors if 266 // they are not annotated with the option rpc.code. 267 // Default is "error". 268 // applicationError 269 // Configures the level at which application errors are logged. 270 // All Thrift exceptions are considered application errors. 271 // Default is "error". 272 // This is deprecated. 273 // failure 274 // Configures the level at which all other failures are logged. 275 // Default is "error". 276 // This is deprecated. 277 // 278 // For example, the following configuration will have the effect of logging 279 // client errors for inbound and outbound calls ("Error handling inbound 280 // request" and "Error making outbound call") at info level instead of error. 281 // 282 // logging: 283 // levels: 284 // clientError: info 285 // 286 // The 'logging' attribute also has 'inbound' and 'outbound' sections 287 // to specify log levels that depend on the traffic direction. 288 // For example, the following configuration will only override the log level 289 // for successful outbound requests. 290 // 291 // logging: 292 // levels: 293 // inbound: 294 // success: debug 295 // 296 // The log levels are: 297 // 298 // debug 299 // info 300 // warn 301 // error 302 // dpanic 303 // panic 304 // fatal 305 // 306 // Customizing Configuration 307 // 308 // When building your own TransportSpec, PeerListSpec, or PeerListUpdaterSpec, 309 // you will define functions accepting structs or pointers to structs which 310 // define the different configuration parameters needed to build that entity. 311 // These configuration parameters will be decoded from the user-specified 312 // configuration using a case-insensitive match on the field names. 313 // 314 // Given the struct, 315 // 316 // type MyConfig struct { 317 // URL string 318 // } 319 // 320 // An object containing a `url`, `URL` or `Url` key with a string value will 321 // be accepted in place of MyConfig. 322 // 323 // Configuration structs can use standard Go primitive types, time.Duration, 324 // maps, slices, and other similar structs. For example only, an outbound 325 // might accept a config containing an array of host:port structs (in 326 // practice, an outbound would use a config.PeerList to build a peer.Chooser). 327 // 328 // type Peer struct { 329 // Host string 330 // Port int 331 // } 332 // 333 // type MyOutboundConfig struct{ Peers []Peer } 334 // 335 // The above will accept the following YAML: 336 // 337 // myoutbound: 338 // peers: 339 // - host: localhost 340 // port: 8080 341 // - host: anotherhost 342 // port: 8080 343 // 344 // Field names can be changed by adding a `config` tag to fields in the 345 // configuration struct. 346 // 347 // type MyInboundConfig struct { 348 // Address string `config:"addr"` 349 // } 350 // 351 // This struct will accept the `addr` key, not `address`. 352 // 353 // In addition to specifying the field name, the `config` tag may also include 354 // an `interpolate` option to request interpolation of variables in the form 355 // ${NAME} or ${NAME:default} at the time the value is decoded. By default, 356 // environment variables are used to fill these variables; this may be changed 357 // with the InterpolationResolver option. 358 // 359 // Interpolation may be requested only for primitive fields and time.Duration. 360 // 361 // type MyConfig struct { 362 // Address string `config:"addr,interpolate"` 363 // Timeout time.Duration `config:",interpolate"` 364 // } 365 // 366 // Note that for the second field, we don't change the name with the tag; we 367 // only indicate that we want interpolation for that variable. 368 // 369 // In the example above, values for both, Address and Timeout may contain 370 // strings in the form ${NAME} or ${NAME:default} anywhere in the value. These 371 // will be replaced with the value of the environment variable or the default 372 // (if specified) if the environment variable was unset. 373 // 374 // addr: localhost:${PORT} 375 // timeout: ${TIMEOUT_SECONDS:5}s 376 package yarpcconfig