orca.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2022 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // Package orca implements Open Request Cost Aggregation, which is an open
  17. // standard for request cost aggregation and reporting by backends and the
  18. // corresponding aggregation of such reports by L7 load balancers (such as
  19. // Envoy) on the data plane. In a proxyless world with gRPC enabled
  20. // applications, aggregation of such reports will be done by the gRPC client.
  21. //
  22. // # Experimental
  23. //
  24. // Notice: All APIs is this package are EXPERIMENTAL and may be changed or
  25. // removed in a later release.
  26. package orca
  27. import (
  28. "google.golang.org/grpc/grpclog"
  29. "google.golang.org/grpc/internal/balancerload"
  30. "google.golang.org/grpc/metadata"
  31. "google.golang.org/grpc/orca/internal"
  32. )
  33. var logger = grpclog.Component("orca-backend-metrics")
  34. // loadParser implements the Parser interface defined in `internal/balancerload`
  35. // package. This interface is used by the client stream to parse load reports
  36. // sent by the server in trailer metadata. The parsed loads are then sent to
  37. // balancers via balancer.DoneInfo.
  38. //
  39. // The grpc package cannot directly call toLoadReport() as that would cause an
  40. // import cycle. Hence this roundabout method is used.
  41. type loadParser struct{}
  42. func (loadParser) Parse(md metadata.MD) interface{} {
  43. lr, err := internal.ToLoadReport(md)
  44. if err != nil {
  45. logger.Infof("Parse failed: %v", err)
  46. }
  47. if lr == nil && logger.V(2) {
  48. logger.Infof("Missing ORCA load report data")
  49. }
  50. return lr
  51. }
  52. func init() {
  53. balancerload.SetParser(loadParser{})
  54. }