|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Based on https://github.com/go-kit/kit/blob/3796a6b25f5c6c545454d3ed7187c4ced258083d/tracing/opencensus/endpoint.go |
| 16 | + |
| 17 | +package otelkit |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "go.opentelemetry.io/otel" |
| 23 | + "go.opentelemetry.io/otel/attribute" |
| 24 | + "go.opentelemetry.io/otel/codes" |
| 25 | + "go.opentelemetry.io/otel/trace" |
| 26 | + |
| 27 | + otelcontrib "go.opentelemetry.io/contrib" |
| 28 | + |
| 29 | + "github.com/go-kit/kit/endpoint" |
| 30 | + "github.com/go-kit/kit/sd/lb" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + tracerName = "go.opentelemetry.io/contrib/instrumentation/github.com/go-kit/kit/otelkit" |
| 35 | + |
| 36 | + // defaultSpanName is the default endpoint span name to use. |
| 37 | + defaultSpanName = "gokit/endpoint" |
| 38 | +) |
| 39 | + |
| 40 | +// EndpointMiddleware returns an Endpoint middleware, tracing a Go kit endpoint. |
| 41 | +// This endpoint middleware should be used in combination with a Go kit Transport |
| 42 | +// tracing middleware, generic OpenTelemetry transport middleware or custom before |
| 43 | +// and after transport functions. |
| 44 | +func EndpointMiddleware(options ...Option) endpoint.Middleware { |
| 45 | + cfg := &config{} |
| 46 | + |
| 47 | + for _, o := range options { |
| 48 | + o(cfg) |
| 49 | + } |
| 50 | + |
| 51 | + if cfg.TracerProvider == nil { |
| 52 | + cfg.TracerProvider = otel.GetTracerProvider() |
| 53 | + } |
| 54 | + |
| 55 | + tracer := cfg.TracerProvider.Tracer( |
| 56 | + tracerName, |
| 57 | + trace.WithInstrumentationVersion(otelcontrib.SemVersion()), |
| 58 | + ) |
| 59 | + |
| 60 | + return func(next endpoint.Endpoint) endpoint.Endpoint { |
| 61 | + return func(ctx context.Context, request interface{}) (response interface{}, err error) { |
| 62 | + operation := cfg.Operation |
| 63 | + if cfg.GetOperation != nil { |
| 64 | + if newOperation := cfg.GetOperation(ctx, operation); newOperation != "" { |
| 65 | + operation = newOperation |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + spanName := operation |
| 70 | + if spanName == "" { |
| 71 | + spanName = defaultSpanName |
| 72 | + } |
| 73 | + |
| 74 | + opts := []trace.SpanOption{ |
| 75 | + trace.WithAttributes(cfg.Attributes...), |
| 76 | + trace.WithSpanKind(trace.SpanKindServer), |
| 77 | + } |
| 78 | + |
| 79 | + if cfg.GetAttributes != nil { |
| 80 | + opts = append(opts, trace.WithAttributes(cfg.GetAttributes(ctx)...)) |
| 81 | + } |
| 82 | + |
| 83 | + ctx, span := tracer.Start(ctx, spanName, opts...) |
| 84 | + defer span.End() |
| 85 | + |
| 86 | + defer func() { |
| 87 | + if err != nil { |
| 88 | + if lberr, ok := err.(lb.RetryError); ok { |
| 89 | + // Handle errors originating from lb.Retry. |
| 90 | + for idx, rawErr := range lberr.RawErrors { |
| 91 | + span.RecordError(rawErr, trace.WithAttributes( |
| 92 | + attribute.Int("gokit.lb.retry.count", idx+1), |
| 93 | + )) |
| 94 | + } |
| 95 | + |
| 96 | + span.RecordError(lberr.Final) |
| 97 | + |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // generic error |
| 102 | + span.RecordError(err) |
| 103 | + |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + // Test for business error. Business errors are often |
| 108 | + // successful requests carrying a business failure that |
| 109 | + // the client can act upon and therefore do not count |
| 110 | + // as failed requests. |
| 111 | + if res, ok := response.(endpoint.Failer); ok && res.Failed() != nil { |
| 112 | + span.RecordError(res.Failed()) |
| 113 | + |
| 114 | + if cfg.IgnoreBusinessError { |
| 115 | + span.SetStatus(codes.Unset, "") |
| 116 | + } |
| 117 | + |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + // no errors identified |
| 122 | + }() |
| 123 | + |
| 124 | + response, err = next(ctx, request) |
| 125 | + |
| 126 | + return |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments