| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlploggrpc // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"sync/atomic" |
| 10 |
"time" |
| 11 |
|
| 12 |
collogpb "go.opentelemetry.io/proto/otlp/collector/logs/v1" |
| 13 |
logpb "go.opentelemetry.io/proto/otlp/logs/v1" |
| 14 |
"google.golang.org/genproto/googleapis/rpc/errdetails" |
| 15 |
"google.golang.org/grpc" |
| 16 |
"google.golang.org/grpc/backoff" |
| 17 |
"google.golang.org/grpc/codes" |
| 18 |
"google.golang.org/grpc/credentials" |
| 19 |
"google.golang.org/grpc/credentials/insecure" |
| 20 |
"google.golang.org/grpc/encoding/gzip" |
| 21 |
"google.golang.org/grpc/metadata" |
| 22 |
"google.golang.org/grpc/status" |
| 23 |
|
| 24 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal" |
| 25 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal/observ" |
| 26 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal/retry" |
| 27 |
) |
| 28 |
|
| 29 |
// The methods of this type are not expected to be called concurrently. |
| 30 |
type client struct { |
| 31 |
metadata metadata.MD |
| 32 |
exportTimeout time.Duration |
| 33 |
requestFunc retry.RequestFunc |
| 34 |
|
| 35 |
// ourConn keeps track of where conn was created: true if created here in |
| 36 |
// NewClient, or false if passed with an option. This is important on |
| 37 |
// Shutdown as conn should only be closed if we created it. Otherwise, |
| 38 |
// it is up to the processes that passed conn to close it. |
| 39 |
ourConn bool |
| 40 |
conn *grpc.ClientConn |
| 41 |
lsc collogpb.LogsServiceClient |
| 42 |
|
| 43 |
instrumentation *observ.Instrumentation |
| 44 |
} |
| 45 |
|
| 46 |
// Used for testing. |
| 47 |
var newGRPCClientFn = grpc.NewClient |
| 48 |
|
| 49 |
// newClient creates a new gRPC log client. |
| 50 |
func newClient(cfg config) (*client, error) { |
| 51 |
c := &client{ |
| 52 |
exportTimeout: cfg.timeout.Value, |
| 53 |
requestFunc: cfg.retryCfg.Value.RequestFunc(retryable), |
| 54 |
conn: cfg.gRPCConn.Value, |
| 55 |
} |
| 56 |
|
| 57 |
if len(cfg.headers.Value) > 0 { |
| 58 |
c.metadata = metadata.New(cfg.headers.Value) |
| 59 |
} |
| 60 |
|
| 61 |
if c.conn == nil { |
| 62 |
// If the caller did not provide a ClientConn when the client was |
| 63 |
// created, create one using the configuration they did provide. |
| 64 |
dialOpts := newGRPCDialOptions(cfg) |
| 65 |
|
| 66 |
conn, err := newGRPCClientFn(cfg.endpoint.Value, dialOpts...) |
| 67 |
if err != nil { |
| 68 |
return nil, err |
| 69 |
} |
| 70 |
// Keep track that we own the lifecycle of this conn and need to close |
| 71 |
// it on Shutdown. |
| 72 |
c.ourConn = true |
| 73 |
c.conn = conn |
| 74 |
} |
| 75 |
|
| 76 |
c.lsc = collogpb.NewLogsServiceClient(c.conn) |
| 77 |
|
| 78 |
var err error |
| 79 |
id := nextExporterID() |
| 80 |
c.instrumentation, err = observ.NewInstrumentation(id, c.conn.CanonicalTarget()) |
| 81 |
return c, err |
| 82 |
} |
| 83 |
|
| 84 |
var exporterN atomic.Int64 |
| 85 |
|
| 86 |
// nextExporterID returns the next unique ID for an exporter. |
| 87 |
func nextExporterID() int64 { |
| 88 |
const inc = 1 |
| 89 |
return exporterN.Add(inc) - inc |
| 90 |
} |
| 91 |
|
| 92 |
func newGRPCDialOptions(cfg config) []grpc.DialOption { |
| 93 |
userAgent := "OTel Go OTLP over gRPC logs exporter/" + Version() |
| 94 |
dialOpts := []grpc.DialOption{grpc.WithUserAgent(userAgent)} |
| 95 |
dialOpts = append(dialOpts, cfg.dialOptions.Value...) |
| 96 |
|
| 97 |
// Convert other grpc configs to the dial options. |
| 98 |
// Service config |
| 99 |
if cfg.serviceConfig.Value != "" { |
| 100 |
dialOpts = append(dialOpts, grpc.WithDefaultServiceConfig(cfg.serviceConfig.Value)) |
| 101 |
} |
| 102 |
// Prioritize GRPCCredentials over Insecure (passing both is an error). |
| 103 |
switch { |
| 104 |
case cfg.gRPCCredentials.Value != nil: |
| 105 |
dialOpts = append(dialOpts, grpc.WithTransportCredentials(cfg.gRPCCredentials.Value)) |
| 106 |
case cfg.insecure.Value: |
| 107 |
dialOpts = append(dialOpts, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 108 |
default: |
| 109 |
// Default to using the host's root CA. |
| 110 |
dialOpts = append(dialOpts, grpc.WithTransportCredentials( |
| 111 |
credentials.NewTLS(nil), |
| 112 |
)) |
| 113 |
} |
| 114 |
// Compression |
| 115 |
if cfg.compression.Value == GzipCompression { |
| 116 |
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.UseCompressor(gzip.Name))) |
| 117 |
} |
| 118 |
// Reconnection period |
| 119 |
if cfg.reconnectionPeriod.Value != 0 { |
| 120 |
p := grpc.ConnectParams{ |
| 121 |
Backoff: backoff.DefaultConfig, |
| 122 |
MinConnectTimeout: cfg.reconnectionPeriod.Value, |
| 123 |
} |
| 124 |
dialOpts = append(dialOpts, grpc.WithConnectParams(p)) |
| 125 |
} |
| 126 |
|
| 127 |
return dialOpts |
| 128 |
} |
| 129 |
|
| 130 |
// UploadLogs sends proto logs to connected endpoint. |
| 131 |
// |
| 132 |
// Retryable errors from the server will be handled according to any |
| 133 |
// RetryConfig the client was created with. |
| 134 |
// |
| 135 |
// The otlplog.Exporter synchronizes access to client methods, and |
| 136 |
// ensures this is not called after the Exporter is shutdown. Only thing |
| 137 |
// to do here is send data. |
| 138 |
func (c *client) UploadLogs(ctx context.Context, rl []*logpb.ResourceLogs) (uploadErr error) { |
| 139 |
select { |
| 140 |
case <-ctx.Done(): |
| 141 |
// Do not upload if the context is already expired. |
| 142 |
return ctx.Err() |
| 143 |
default: |
| 144 |
} |
| 145 |
|
| 146 |
ctx, cancel := c.exportContext(ctx) |
| 147 |
defer cancel() |
| 148 |
|
| 149 |
count := int64(len(rl)) |
| 150 |
if c.instrumentation != nil { |
| 151 |
eo := c.instrumentation.ExportLogs(ctx, count) |
| 152 |
defer func() { |
| 153 |
eo.End(uploadErr) |
| 154 |
}() |
| 155 |
} |
| 156 |
|
| 157 |
return errors.Join(uploadErr, c.requestFunc(ctx, func(ctx context.Context) error { |
| 158 |
resp, err := c.lsc.Export(ctx, &collogpb.ExportLogsServiceRequest{ |
| 159 |
ResourceLogs: rl, |
| 160 |
}) |
| 161 |
if resp != nil && resp.PartialSuccess != nil { |
| 162 |
msg := resp.PartialSuccess.GetErrorMessage() |
| 163 |
n := resp.PartialSuccess.GetRejectedLogRecords() |
| 164 |
if n != 0 || msg != "" { |
| 165 |
err := internal.LogPartialSuccessError(n, msg) |
| 166 |
uploadErr = errors.Join(uploadErr, err) |
| 167 |
} |
| 168 |
} |
| 169 |
// nil is converted to OK. |
| 170 |
if status.Code(err) == codes.OK { |
| 171 |
// Success. |
| 172 |
return nil |
| 173 |
} |
| 174 |
return err |
| 175 |
})) |
| 176 |
} |
| 177 |
|
| 178 |
// Shutdown shuts down the client, freeing all resources. |
| 179 |
// |
| 180 |
// Any active connections to a remote endpoint are closed if they were created |
| 181 |
// by the client. Any gRPC connection passed during creation using |
| 182 |
// WithGRPCConn will not be closed. It is the caller's responsibility to |
| 183 |
// handle cleanup of that resource. |
| 184 |
// |
| 185 |
// The otlplog.Exporter synchronizes access to client methods and |
| 186 |
// ensures this is called only once. The only thing that needs to be done |
| 187 |
// here is to release any computational resources the client holds. |
| 188 |
func (c *client) Shutdown(ctx context.Context) error { |
| 189 |
c.metadata = nil |
| 190 |
c.requestFunc = nil |
| 191 |
c.lsc = nil |
| 192 |
|
| 193 |
// Release the connection if we created it. |
| 194 |
err := ctx.Err() |
| 195 |
if c.ourConn { |
| 196 |
closeErr := c.conn.Close() |
| 197 |
// A context timeout error takes precedence over this error. |
| 198 |
if err == nil && closeErr != nil { |
| 199 |
err = closeErr |
| 200 |
} |
| 201 |
} |
| 202 |
c.conn = nil |
| 203 |
return err |
| 204 |
} |
| 205 |
|
| 206 |
// exportContext returns a copy of parent with an appropriate deadline and |
| 207 |
// cancellation function based on the clients configured export timeout. |
| 208 |
// |
| 209 |
// It is the callers responsibility to cancel the returned context once its |
| 210 |
// use is complete, via the parent or directly with the returned CancelFunc, to |
| 211 |
// ensure all resources are correctly released. |
| 212 |
func (c *client) exportContext(parent context.Context) (context.Context, context.CancelFunc) { |
| 213 |
var ( |
| 214 |
ctx context.Context |
| 215 |
cancel context.CancelFunc |
| 216 |
) |
| 217 |
|
| 218 |
if c.exportTimeout > 0 { |
| 219 |
ctx, cancel = context.WithTimeoutCause(parent, c.exportTimeout, errors.New("exporter export timeout")) |
| 220 |
} else { |
| 221 |
ctx, cancel = context.WithCancel(parent) //nolint:gosec // cancel is handled by caller. |
| 222 |
} |
| 223 |
|
| 224 |
if c.metadata.Len() > 0 { |
| 225 |
md := c.metadata |
| 226 |
if outMD, ok := metadata.FromOutgoingContext(ctx); ok { |
| 227 |
md = metadata.Join(md, outMD) |
| 228 |
} |
| 229 |
|
| 230 |
ctx = metadata.NewOutgoingContext(ctx, md) |
| 231 |
} |
| 232 |
|
| 233 |
return ctx, cancel |
| 234 |
} |
| 235 |
|
| 236 |
type noopClient struct{} |
| 237 |
|
| 238 |
func newNoopClient() *noopClient { |
| 239 |
return &noopClient{} |
| 240 |
} |
| 241 |
|
| 242 |
func (*noopClient) UploadLogs(context.Context, []*logpb.ResourceLogs) error { return nil } |
| 243 |
|
| 244 |
func (*noopClient) Shutdown(context.Context) error { return nil } |
| 245 |
|
| 246 |
// retryable returns if err identifies a request that can be retried and a |
| 247 |
// duration to wait for if an explicit throttle time is included in err. |
| 248 |
func retryable(err error) (bool, time.Duration) { |
| 249 |
s := status.Convert(err) |
| 250 |
return retryableGRPCStatus(s) |
| 251 |
} |
| 252 |
|
| 253 |
func retryableGRPCStatus(s *status.Status) (bool, time.Duration) { |
| 254 |
switch s.Code() { |
| 255 |
// Follows the retryable error codes defined in |
| 256 |
// https://opentelemetry.io/docs/specs/otlp/#failures |
| 257 |
case codes.Canceled, |
| 258 |
codes.DeadlineExceeded, |
| 259 |
codes.Aborted, |
| 260 |
codes.OutOfRange, |
| 261 |
codes.Unavailable, |
| 262 |
codes.DataLoss: |
| 263 |
// Additionally, handle RetryInfo. |
| 264 |
_, d := throttleDelay(s) |
| 265 |
return true, d |
| 266 |
case codes.ResourceExhausted: |
| 267 |
// Retry only if the server signals that the recovery from resource exhaustion is possible. |
| 268 |
return throttleDelay(s) |
| 269 |
} |
| 270 |
|
| 271 |
// Not a retry-able error. |
| 272 |
return false, 0 |
| 273 |
} |
| 274 |
|
| 275 |
// throttleDelay returns if the status is RetryInfo |
| 276 |
// and the duration to wait for if an explicit throttle time is included. |
| 277 |
func throttleDelay(s *status.Status) (bool, time.Duration) { |
| 278 |
for _, detail := range s.Details() { |
| 279 |
if t, ok := detail.(*errdetails.RetryInfo); ok { |
| 280 |
return true, t.RetryDelay.AsDuration() |
| 281 |
} |
| 282 |
} |
| 283 |
return false, 0 |
| 284 |
} |