-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proof-of-Concept for partial export err returns #3153
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package trace // import "go.opentelemetry.io/otel/sdk/trace" | ||
|
||
// Copied from https://github.com/MrAlias/flow for demo purposes. | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/internal/global" | ||
) | ||
|
||
const ( | ||
startedState = "started" | ||
endedState = "ended" | ||
|
||
// DefaultListenPort is the port the HTTP server listens on if not | ||
// configured with the WithListenAddress option. | ||
DefaultListenPort = 41820 | ||
// DefaultListenAddress is the listen address of the HTTP server if not | ||
// configured with the WithListenAddress option. | ||
DefaultListenAddress = ":41820" | ||
) | ||
|
||
type spanProcessor struct { | ||
wrapped SpanExporter | ||
|
||
idleConnsClosed chan struct{} | ||
server *http.Server | ||
spanCounter *prometheus.CounterVec | ||
exportErrCounter *prometheus.CounterVec | ||
} | ||
|
||
// Wrap returns a wrapped version of the downstream SpanExporter with | ||
// telemetry flow reporting. All calls to the returned SpanProcessor will | ||
// introspected for telemetry data and then forwarded to downstream. | ||
func Wrap(downstream SpanExporter, options ...Option) SpanProcessor { | ||
mux := http.NewServeMux() | ||
registry := prometheus.NewRegistry() | ||
mux.Handle("/metrics", promhttp.InstrumentMetricHandler( | ||
registry, | ||
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}), | ||
)) | ||
|
||
c := newConfig(options) | ||
sp := &spanProcessor{ | ||
wrapped: downstream, | ||
idleConnsClosed: make(chan struct{}), | ||
server: &http.Server{Addr: c.address, Handler: mux}, | ||
spanCounter: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "spans_total", | ||
Help: "The total number of processed spans", | ||
}, []string{"state"}), | ||
exportErrCounter: prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
Name: "failed_export_spans_total", | ||
Help: "The total number of spans that failed to export", | ||
}, []string{}), | ||
} | ||
registry.MustRegister(sp.spanCounter) | ||
registry.MustRegister(sp.exportErrCounter) | ||
|
||
go func() { | ||
switch err := sp.server.ListenAndServe(); err { | ||
case nil, http.ErrServerClosed: | ||
default: | ||
otel.Handle(err) | ||
} | ||
close(sp.idleConnsClosed) | ||
}() | ||
|
||
return sp | ||
} | ||
|
||
// OnStart is called when a span is started. | ||
func (sp *spanProcessor) OnStart(parent context.Context, s ReadWriteSpan) { | ||
sp.spanCounter.WithLabelValues(startedState).Inc() | ||
} | ||
|
||
// OnEnd is called when span is finished. | ||
func (sp *spanProcessor) OnEnd(s ReadOnlySpan) { | ||
sp.spanCounter.WithLabelValues(endedState).Inc() | ||
spans := []ReadOnlySpan{s} | ||
err := sp.wrapped.ExportSpans(context.TODO(), spans) | ||
var errPart *PartialExportError | ||
if errors.As(err, &errPart) { | ||
sp.exportErrCounter.WithLabelValues().Add(float64(errPart.RejectedN)) | ||
} else { | ||
global.Error(err, "failed export", "span-count", len(spans)) | ||
} | ||
} | ||
|
||
// Shutdown is called when the SDK shuts down. The telemetry reporting process | ||
// will be halted when this is called. | ||
func (sp *spanProcessor) Shutdown(ctx context.Context) error { | ||
errCh := make(chan error, 1) | ||
go func() { | ||
errCh <- sp.wrapped.Shutdown(ctx) | ||
}() | ||
|
||
err := sp.server.Shutdown(ctx) | ||
select { | ||
case <-ctx.Done(): | ||
// Abandon idle conns if context has expired. | ||
if err == nil { | ||
return ctx.Err() | ||
} | ||
return err | ||
case <-sp.idleConnsClosed: | ||
} | ||
|
||
// Downstream honors ctx timeout, no need to include in select above. | ||
if e := <-errCh; e != nil { | ||
// Prioritize downstream error over server shutdown error. | ||
err = e | ||
} | ||
return err | ||
} | ||
|
||
// ForceFlush dones nothing. | ||
func (sp *spanProcessor) ForceFlush(ctx context.Context) error { return nil } | ||
|
||
type config struct { | ||
// address is the listen address for the HTTP server. | ||
address string | ||
} | ||
|
||
func newConfig(options []Option) config { | ||
c := config{ | ||
address: DefaultListenAddress, | ||
} | ||
|
||
for _, opt := range options { | ||
c = opt.apply(c) | ||
} | ||
|
||
return c | ||
} | ||
|
||
// Option configures the flow SpanProcessor. | ||
type Option interface { | ||
apply(config) config | ||
} | ||
|
||
type addressOpt string | ||
|
||
func (o addressOpt) apply(c config) config { | ||
c.address = string(o) | ||
return c | ||
} | ||
|
||
// WithListenAddress sets the listen address of the HTTP server. | ||
func WithListenAddress(addr string) Option { | ||
return addressOpt(addr) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,10 @@ | |
|
||
package trace // import "go.opentelemetry.io/otel/sdk/trace" | ||
|
||
import "context" | ||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
// SpanExporter handles the delivery of spans to external receivers. This is | ||
// the final component in the trace export pipeline. | ||
|
@@ -45,3 +48,14 @@ type SpanExporter interface { | |
// DO NOT CHANGE: any modification will not be backwards compatible and | ||
// must never be done outside of a new major release. | ||
} | ||
|
||
type PartialExportError struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this different from having the error in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed in SIG meeting, but for others interested: There are likely other exporters that support partial successes. For instance, there is the sumologic exporter in the collector. |
||
RejectedN int64 | ||
Err error | ||
} | ||
|
||
func (e *PartialExportError) Error() string { | ||
return fmt.Sprintf("%d spans not exported: %s", e.RejectedN, e.Err) | ||
} | ||
|
||
func (e *PartialExportError) Unwrap() error { return e.Err } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you want to return a
SpanExporter
here? That way you can still use the batch processor, and you would just be instrumenting the calls to aroundExportSpans()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that could makes sense. This was (quickly) adapted from the flow
SpanProcessor
, which exports metrics about started/stopped spans. I can definitely see another package where this is a simple wrapper of aSpanExporter
and only reports dropped values.