| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package otlpexporter |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"runtime" |
| 10 |
|
| 11 |
"go.uber.org/zap" |
| 12 |
"google.golang.org/genproto/googleapis/rpc/errdetails" |
| 13 |
"google.golang.org/grpc" |
| 14 |
"google.golang.org/grpc/codes" |
| 15 |
"google.golang.org/grpc/metadata" |
| 16 |
"google.golang.org/grpc/status" |
| 17 |
|
| 18 |
"go.opentelemetry.io/collector/component" |
| 19 |
"go.opentelemetry.io/collector/config/configgrpc" |
| 20 |
"go.opentelemetry.io/collector/consumer/consumererror" |
| 21 |
"go.opentelemetry.io/collector/exporter" |
| 22 |
"go.opentelemetry.io/collector/exporter/exporterhelper" |
| 23 |
"go.opentelemetry.io/collector/internal/statusutil" |
| 24 |
"go.opentelemetry.io/collector/pdata/plog" |
| 25 |
"go.opentelemetry.io/collector/pdata/plog/plogotlp" |
| 26 |
"go.opentelemetry.io/collector/pdata/pmetric" |
| 27 |
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" |
| 28 |
"go.opentelemetry.io/collector/pdata/pprofile" |
| 29 |
"go.opentelemetry.io/collector/pdata/pprofile/pprofileotlp" |
| 30 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 31 |
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp" |
| 32 |
) |
| 33 |
|
| 34 |
type baseExporter struct { |
| 35 |
|
| 36 |
config *Config |
| 37 |
|
| 38 |
|
| 39 |
traceExporter ptraceotlp.GRPCClient |
| 40 |
metricExporter pmetricotlp.GRPCClient |
| 41 |
logExporter plogotlp.GRPCClient |
| 42 |
profileExporter pprofileotlp.GRPCClient |
| 43 |
clientConn *grpc.ClientConn |
| 44 |
metadata metadata.MD |
| 45 |
callOptions []grpc.CallOption |
| 46 |
|
| 47 |
settings component.TelemetrySettings |
| 48 |
|
| 49 |
|
| 50 |
userAgent string |
| 51 |
} |
| 52 |
|
| 53 |
func newExporter(cfg component.Config, set exporter.Settings) *baseExporter { |
| 54 |
oCfg := cfg.(*Config) |
| 55 |
|
| 56 |
userAgent := fmt.Sprintf("%s/%s (%s/%s)", |
| 57 |
set.BuildInfo.Description, set.BuildInfo.Version, runtime.GOOS, runtime.GOARCH) |
| 58 |
|
| 59 |
return &baseExporter{config: oCfg, settings: set.TelemetrySettings, userAgent: userAgent} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
func (e *baseExporter) start(ctx context.Context, host component.Host) (err error) { |
| 65 |
agentOpt := configgrpc.WithGrpcDialOption(grpc.WithUserAgent(e.userAgent)) |
| 66 |
if e.clientConn, err = e.config.ClientConfig.ToClientConn(ctx, host.GetExtensions(), e.settings, agentOpt); err != nil { |
| 67 |
return err |
| 68 |
} |
| 69 |
e.traceExporter = ptraceotlp.NewGRPCClient(e.clientConn) |
| 70 |
e.metricExporter = pmetricotlp.NewGRPCClient(e.clientConn) |
| 71 |
e.logExporter = plogotlp.NewGRPCClient(e.clientConn) |
| 72 |
e.profileExporter = pprofileotlp.NewGRPCClient(e.clientConn) |
| 73 |
headers := map[string]string{} |
| 74 |
for k, v := range e.config.ClientConfig.Headers.Iter { |
| 75 |
headers[k] = string(v) |
| 76 |
} |
| 77 |
e.metadata = metadata.New(headers) |
| 78 |
e.callOptions = []grpc.CallOption{ |
| 79 |
grpc.WaitForReady(e.config.ClientConfig.WaitForReady), |
| 80 |
} |
| 81 |
|
| 82 |
return err |
| 83 |
} |
| 84 |
|
| 85 |
func (e *baseExporter) shutdown(context.Context) error { |
| 86 |
if e.clientConn != nil { |
| 87 |
return e.clientConn.Close() |
| 88 |
} |
| 89 |
return nil |
| 90 |
} |
| 91 |
|
| 92 |
func (e *baseExporter) pushTraces(ctx context.Context, td ptrace.Traces) error { |
| 93 |
req := ptraceotlp.NewExportRequestFromTraces(td) |
| 94 |
resp, respErr := e.traceExporter.Export(ctx, req, e.callOptions...) |
| 95 |
if err := processError(respErr); err != nil { |
| 96 |
return err |
| 97 |
} |
| 98 |
partialSuccess := resp.PartialSuccess() |
| 99 |
if partialSuccess.ErrorMessage() != "" || partialSuccess.RejectedSpans() != 0 { |
| 100 |
e.settings.Logger.Warn("Partial success response", |
| 101 |
zap.String("message", resp.PartialSuccess().ErrorMessage()), |
| 102 |
zap.Int64("dropped_spans", resp.PartialSuccess().RejectedSpans()), |
| 103 |
) |
| 104 |
} |
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
func (e *baseExporter) pushMetrics(ctx context.Context, md pmetric.Metrics) error { |
| 109 |
req := pmetricotlp.NewExportRequestFromMetrics(md) |
| 110 |
resp, respErr := e.metricExporter.Export(ctx, req, e.callOptions...) |
| 111 |
if err := processError(respErr); err != nil { |
| 112 |
return err |
| 113 |
} |
| 114 |
partialSuccess := resp.PartialSuccess() |
| 115 |
if partialSuccess.ErrorMessage() != "" || partialSuccess.RejectedDataPoints() != 0 { |
| 116 |
e.settings.Logger.Warn("Partial success response", |
| 117 |
zap.String("message", resp.PartialSuccess().ErrorMessage()), |
| 118 |
zap.Int64("dropped_data_points", resp.PartialSuccess().RejectedDataPoints()), |
| 119 |
) |
| 120 |
} |
| 121 |
return nil |
| 122 |
} |
| 123 |
|
| 124 |
func (e *baseExporter) pushLogs(ctx context.Context, ld plog.Logs) error { |
| 125 |
req := plogotlp.NewExportRequestFromLogs(ld) |
| 126 |
resp, respErr := e.logExporter.Export(ctx, req, e.callOptions...) |
| 127 |
if err := processError(respErr); err != nil { |
| 128 |
return err |
| 129 |
} |
| 130 |
partialSuccess := resp.PartialSuccess() |
| 131 |
if partialSuccess.ErrorMessage() != "" || partialSuccess.RejectedLogRecords() != 0 { |
| 132 |
e.settings.Logger.Warn("Partial success response", |
| 133 |
zap.String("message", resp.PartialSuccess().ErrorMessage()), |
| 134 |
zap.Int64("dropped_log_records", resp.PartialSuccess().RejectedLogRecords()), |
| 135 |
) |
| 136 |
} |
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
func (e *baseExporter) pushProfiles(ctx context.Context, td pprofile.Profiles) error { |
| 141 |
req := pprofileotlp.NewExportRequestFromProfiles(td) |
| 142 |
resp, respErr := e.profileExporter.Export(ctx, req, e.callOptions...) |
| 143 |
if err := processError(respErr); err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
partialSuccess := resp.PartialSuccess() |
| 147 |
if partialSuccess.ErrorMessage() != "" || partialSuccess.RejectedProfiles() != 0 { |
| 148 |
e.settings.Logger.Warn("Partial success response", |
| 149 |
zap.String("message", resp.PartialSuccess().ErrorMessage()), |
| 150 |
zap.Int64("dropped_profiles", resp.PartialSuccess().RejectedProfiles()), |
| 151 |
) |
| 152 |
} |
| 153 |
return nil |
| 154 |
} |
| 155 |
|
| 156 |
func processError(err error) error { |
| 157 |
if err == nil { |
| 158 |
|
| 159 |
return nil |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
st := status.Convert(err) |
| 164 |
if st.Code() == codes.OK { |
| 165 |
|
| 166 |
return nil |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
retryInfo := statusutil.GetRetryInfo(st) |
| 171 |
|
| 172 |
if !shouldRetry(st.Code(), retryInfo) { |
| 173 |
|
| 174 |
return consumererror.NewPermanent(err) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
throttleDuration := retryInfo.GetRetryDelay().AsDuration() |
| 179 |
if throttleDuration != 0 { |
| 180 |
|
| 181 |
return exporterhelper.NewThrottleRetry(err, throttleDuration) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
return err |
| 186 |
} |
| 187 |
|
| 188 |
func shouldRetry(code codes.Code, retryInfo *errdetails.RetryInfo) bool { |
| 189 |
switch code { |
| 190 |
case codes.Canceled, |
| 191 |
codes.DeadlineExceeded, |
| 192 |
codes.Aborted, |
| 193 |
codes.OutOfRange, |
| 194 |
codes.Unavailable, |
| 195 |
codes.DataLoss: |
| 196 |
|
| 197 |
return true |
| 198 |
case codes.ResourceExhausted: |
| 199 |
|
| 200 |
|
| 201 |
return retryInfo != nil |
| 202 |
} |
| 203 |
|
| 204 |
return false |
| 205 |
} |