Debian Codegraph @

exporters/otlp/otlplog/otlploghttp/exporter.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otlploghttp // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
5
6 import (
7 "context"
8 "sync/atomic"
9
10 "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/transform"
11 "go.opentelemetry.io/otel/sdk/log"
12 )
13
14 // Exporter is a OpenTelemetry log Exporter. It transports log data encoded as
15 // OTLP protobufs using HTTP.
16 // Exporter must be created with [New].
17 type Exporter struct {
18 client atomic.Pointer[client]
19 stopped atomic.Bool
20 }
21
22 // Compile-time check Exporter implements [log.Exporter].
23 var _ log.Exporter = (*Exporter)(nil)
24
25 // New returns a new [Exporter].
26 //
27 // It is recommended to use it with a [BatchProcessor]
28 // or other processor exporting records asynchronously.
29 func New(ctx context.Context, options ...Option) (*Exporter, error) {
30 cfg := newConfig(options)
31 c, err := newHTTPClient(ctx, cfg)
32 if err != nil {
33 return nil, err
34 }
35 return newExporter(c, cfg)
36 }
37
38 func newExporter(c *client, _ config) (*Exporter, error) {
39 e := &Exporter{}
40 e.client.Store(c)
41 return e, nil
42 }
43
44 // Used for testing.
45 var transformResourceLogs = transform.ResourceLogs
46
47 // Export transforms and transmits log records to an OTLP receiver.
48 func (e *Exporter) Export(ctx context.Context, records []log.Record) error {
49 if e.stopped.Load() {
50 return nil
51 }
52 otlp := transformResourceLogs(records)
53 if otlp == nil {
54 return nil
55 }
56 return e.client.Load().UploadLogs(ctx, otlp)
57 }
58
59 // Shutdown shuts down the Exporter. Calls to Export or ForceFlush will perform
60 // no operation after this is called.
61 func (e *Exporter) Shutdown(context.Context) error {
62 if e.stopped.Swap(true) {
63 return nil
64 }
65
66 e.client.Store(newNoopClient())
67 return nil
68 }
69
70 // ForceFlush does nothing. The Exporter holds no state.
71 func (*Exporter) ForceFlush(context.Context) error {
72 return nil
73 }