| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otlpmetricgrpc // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"sync" |
| 11 |
|
| 12 |
metricpb "go.opentelemetry.io/proto/otlp/metrics/v1" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf" |
| 15 |
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/transform" |
| 16 |
"go.opentelemetry.io/otel/internal/global" |
| 17 |
"go.opentelemetry.io/otel/sdk/metric" |
| 18 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 19 |
) |
| 20 |
|
| 21 |
// Exporter is a OpenTelemetry metric Exporter using gRPC. |
| 22 |
type Exporter struct { |
| 23 |
// Ensure synchronous access to the client across all functionality. |
| 24 |
clientMu sync.Mutex |
| 25 |
client interface { |
| 26 |
UploadMetrics(context.Context, *metricpb.ResourceMetrics) error |
| 27 |
Shutdown(context.Context) error |
| 28 |
} |
| 29 |
|
| 30 |
temporalitySelector metric.TemporalitySelector |
| 31 |
aggregationSelector metric.AggregationSelector |
| 32 |
|
| 33 |
shutdownOnce sync.Once |
| 34 |
} |
| 35 |
|
| 36 |
func newExporter(c *client, cfg oconf.Config) (*Exporter, error) { |
| 37 |
ts := cfg.Metrics.TemporalitySelector |
| 38 |
if ts == nil { |
| 39 |
ts = func(metric.InstrumentKind) metricdata.Temporality { |
| 40 |
return metricdata.CumulativeTemporality |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
as := cfg.Metrics.AggregationSelector |
| 45 |
if as == nil { |
| 46 |
as = metric.DefaultAggregationSelector |
| 47 |
} |
| 48 |
|
| 49 |
return &Exporter{ |
| 50 |
client: c, |
| 51 |
|
| 52 |
temporalitySelector: ts, |
| 53 |
aggregationSelector: as, |
| 54 |
}, nil |
| 55 |
} |
| 56 |
|
| 57 |
// Temporality returns the Temporality to use for an instrument kind. |
| 58 |
func (e *Exporter) Temporality(k metric.InstrumentKind) metricdata.Temporality { |
| 59 |
return e.temporalitySelector(k) |
| 60 |
} |
| 61 |
|
| 62 |
// Aggregation returns the Aggregation to use for an instrument kind. |
| 63 |
func (e *Exporter) Aggregation(k metric.InstrumentKind) metric.Aggregation { |
| 64 |
return e.aggregationSelector(k) |
| 65 |
} |
| 66 |
|
| 67 |
// Export transforms and transmits metric data to an OTLP receiver. |
| 68 |
// |
| 69 |
// This method returns an error if called after Shutdown. |
| 70 |
// This method returns an error if the method is canceled by the passed context. |
| 71 |
func (e *Exporter) Export(ctx context.Context, rm *metricdata.ResourceMetrics) error { |
| 72 |
defer global.Debug("OTLP/gRPC exporter export", "Data", rm) |
| 73 |
|
| 74 |
otlpRm, err := transform.ResourceMetrics(rm) |
| 75 |
// Best effort upload of transformable metrics. |
| 76 |
e.clientMu.Lock() |
| 77 |
upErr := e.client.UploadMetrics(ctx, otlpRm) |
| 78 |
e.clientMu.Unlock() |
| 79 |
if upErr != nil { |
| 80 |
if err == nil { |
| 81 |
return fmt.Errorf("failed to upload metrics: %w", upErr) |
| 82 |
} |
| 83 |
// Merge the two errors. |
| 84 |
return fmt.Errorf("failed to upload incomplete metrics (%w): %w", err, upErr) |
| 85 |
} |
| 86 |
return err |
| 87 |
} |
| 88 |
|
| 89 |
// ForceFlush flushes any metric data held by an exporter. |
| 90 |
// |
| 91 |
// This method returns an error if called after Shutdown. |
| 92 |
// This method returns an error if the method is canceled by the passed context. |
| 93 |
// |
| 94 |
// This method is safe to call concurrently. |
| 95 |
func (*Exporter) ForceFlush(ctx context.Context) error { |
| 96 |
// The exporter and client hold no state, nothing to flush. |
| 97 |
return ctx.Err() |
| 98 |
} |
| 99 |
|
| 100 |
// Shutdown flushes all metric data held by an exporter and releases any held |
| 101 |
// computational resources. |
| 102 |
// |
| 103 |
// This method returns an error if called after Shutdown. |
| 104 |
// This method returns an error if the method is canceled by the passed context. |
| 105 |
// |
| 106 |
// This method is safe to call concurrently. |
| 107 |
func (e *Exporter) Shutdown(ctx context.Context) error { |
| 108 |
err := errShutdown |
| 109 |
e.shutdownOnce.Do(func() { |
| 110 |
e.clientMu.Lock() |
| 111 |
client := e.client |
| 112 |
e.client = shutdownClient{} |
| 113 |
e.clientMu.Unlock() |
| 114 |
err = client.Shutdown(ctx) |
| 115 |
}) |
| 116 |
return err |
| 117 |
} |
| 118 |
|
| 119 |
var errShutdown = errors.New("gRPC exporter is shutdown") |
| 120 |
|
| 121 |
type shutdownClient struct{} |
| 122 |
|
| 123 |
func (shutdownClient) err(ctx context.Context) error { |
| 124 |
if err := ctx.Err(); err != nil { |
| 125 |
return err |
| 126 |
} |
| 127 |
return errShutdown |
| 128 |
} |
| 129 |
|
| 130 |
func (c shutdownClient) UploadMetrics(ctx context.Context, _ *metricpb.ResourceMetrics) error { |
| 131 |
return c.err(ctx) |
| 132 |
} |
| 133 |
|
| 134 |
func (c shutdownClient) Shutdown(ctx context.Context) error { |
| 135 |
return c.err(ctx) |
| 136 |
} |
| 137 |
|
| 138 |
// MarshalLog returns logging data about the Exporter. |
| 139 |
func (*Exporter) MarshalLog() any { |
| 140 |
return struct{ Type string }{Type: "OTLP/gRPC"} |
| 141 |
} |
| 142 |
|
| 143 |
// New returns an OpenTelemetry metric Exporter. The Exporter can be used with |
| 144 |
// a PeriodicReader to export OpenTelemetry metric data to an OTLP receiving |
| 145 |
// endpoint using gRPC. |
| 146 |
// |
| 147 |
// If an already established gRPC ClientConn is not passed in options using |
| 148 |
// WithGRPCConn, a connection to the OTLP endpoint will be established based |
| 149 |
// on options. If a connection cannot be establishes in the lifetime of ctx, |
| 150 |
// an error will be returned. |
| 151 |
func New(ctx context.Context, options ...Option) (*Exporter, error) { |
| 152 |
cfg := oconf.NewGRPCConfig(asGRPCOptions(options)...) |
| 153 |
c, err := newClient(ctx, cfg) |
| 154 |
if err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
return newExporter(c, cfg) |
| 158 |
} |