| 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 |
"sync" |
| 9 |
"sync/atomic" |
| 10 |
|
| 11 |
logpb "go.opentelemetry.io/proto/otlp/logs/v1" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc/internal/transform" |
| 14 |
"go.opentelemetry.io/otel/sdk/log" |
| 15 |
) |
| 16 |
|
| 17 |
type logClient interface { |
| 18 |
UploadLogs(ctx context.Context, rl []*logpb.ResourceLogs) error |
| 19 |
Shutdown(context.Context) error |
| 20 |
} |
| 21 |
|
| 22 |
// Exporter is a OpenTelemetry log Exporter. It transports log data encoded as |
| 23 |
// OTLP protobufs using gRPC. |
| 24 |
// All Exporters must be created with [New]. |
| 25 |
type Exporter struct { |
| 26 |
// Ensure synchronous access to the client across all functionality. |
| 27 |
clientMu sync.Mutex |
| 28 |
client logClient |
| 29 |
|
| 30 |
stopped atomic.Bool |
| 31 |
} |
| 32 |
|
| 33 |
// Compile-time check Exporter implements [log.Exporter]. |
| 34 |
var _ log.Exporter = (*Exporter)(nil) |
| 35 |
|
| 36 |
// New returns a new [Exporter]. |
| 37 |
// |
| 38 |
// It is recommended to use it with a [BatchProcessor] |
| 39 |
// or other processor exporting records asynchronously. |
| 40 |
func New(_ context.Context, options ...Option) (*Exporter, error) { |
| 41 |
cfg := newConfig(options) |
| 42 |
c, err := newClient(cfg) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
return newExporter(c), nil |
| 47 |
} |
| 48 |
|
| 49 |
func newExporter(c logClient) *Exporter { |
| 50 |
var e Exporter |
| 51 |
e.client = c |
| 52 |
return &e |
| 53 |
} |
| 54 |
|
| 55 |
var transformResourceLogs = transform.ResourceLogs |
| 56 |
|
| 57 |
// Export transforms and transmits log records to an OTLP receiver. |
| 58 |
// |
| 59 |
// This method returns nil and drops records if called after Shutdown. |
| 60 |
// This method returns an error if the method is canceled by the passed context. |
| 61 |
func (e *Exporter) Export(ctx context.Context, records []log.Record) error { |
| 62 |
if e.stopped.Load() { |
| 63 |
return nil |
| 64 |
} |
| 65 |
|
| 66 |
otlp := transformResourceLogs(records) |
| 67 |
if otlp == nil { |
| 68 |
return nil |
| 69 |
} |
| 70 |
|
| 71 |
e.clientMu.Lock() |
| 72 |
defer e.clientMu.Unlock() |
| 73 |
return e.client.UploadLogs(ctx, otlp) |
| 74 |
} |
| 75 |
|
| 76 |
// Shutdown shuts down the Exporter. Calls to Export or ForceFlush will perform |
| 77 |
// no operation after this is called. |
| 78 |
func (e *Exporter) Shutdown(ctx context.Context) error { |
| 79 |
if e.stopped.Swap(true) { |
| 80 |
return nil |
| 81 |
} |
| 82 |
|
| 83 |
e.clientMu.Lock() |
| 84 |
defer e.clientMu.Unlock() |
| 85 |
|
| 86 |
err := e.client.Shutdown(ctx) |
| 87 |
e.client = newNoopClient() |
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
// ForceFlush does nothing. The Exporter holds no state. |
| 92 |
func (*Exporter) ForceFlush(context.Context) error { |
| 93 |
return nil |
| 94 |
} |