-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joseph Woodward <joseph.woodward@xeuse.com>
- Loading branch information
1 parent
53ffffd
commit bac160e
Showing
5 changed files
with
230 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package client | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-openapi/runtime" | ||
"github.com/go-openapi/strfmt" | ||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
type openTelemetryTransport struct { | ||
transport runtime.ClientTransport | ||
host string | ||
opts []trace.SpanStartOption | ||
} | ||
|
||
func newOpenTelemetryTransport(transport runtime.ClientTransport, host string, opts []trace.SpanStartOption, | ||
) runtime.ClientTransport { | ||
return &openTelemetryTransport{ | ||
transport: transport, | ||
host: host, | ||
opts: opts, | ||
} | ||
} | ||
|
||
func (t *openTelemetryTransport) Submit(op *runtime.ClientOperation) (interface{}, error) { | ||
if op.Context == nil { | ||
return t.transport.Submit(op) | ||
} | ||
|
||
params := op.Params | ||
reader := op.Reader | ||
|
||
var span trace.Span | ||
defer func() { | ||
if span != nil { | ||
span.End() | ||
} | ||
}() | ||
|
||
op.Params = runtime.ClientRequestWriterFunc(func(req runtime.ClientRequest, reg strfmt.Registry) error { | ||
span = createOpenTelemetryClientSpan(op, req.GetHeaderParams(), t.host, t.opts) | ||
return params.WriteToRequest(req, reg) | ||
}) | ||
|
||
op.Reader = runtime.ClientResponseReaderFunc(func(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) { | ||
// if span != nil { | ||
// code := response.Code() | ||
// ext.HTTPStatusCode.Set(span, uint16(code)) | ||
// if code >= http.StatusBadRequest { | ||
// ext.Error.Set(span, true) | ||
// } | ||
// } | ||
return reader.ReadResponse(response, consumer) | ||
}) | ||
|
||
submit, err := t.transport.Submit(op) | ||
// if err != nil && span != nil { | ||
// ext.Error.Set(span, true) | ||
// span.LogFields(log.Error(err)) | ||
// } | ||
return submit, err | ||
} | ||
|
||
func createOpenTelemetryClientSpan(op *runtime.ClientOperation, header http.Header, host string, opts []trace.SpanStartOption) trace.Span { | ||
ctx := op.Context | ||
span := trace.SpanFromContext(ctx) | ||
|
||
if span != nil { | ||
// TODO: Can we get the version number for use with trace.WithInstrumentationVersion? | ||
tracer := otel.GetTracerProvider().Tracer("") | ||
|
||
ctx, span = tracer.Start(ctx, operationName(op), opts...) | ||
op.Context = ctx | ||
|
||
return span | ||
} | ||
|
||
// if span != nil { | ||
// opts = append(opts, ext.SpanKindRPCClient) | ||
// span, _ = opentracing.StartSpanFromContextWithTracer( | ||
// ctx, span.Tracer(), operationName(op), opts...) | ||
|
||
// ext.Component.Set(span, "go-openapi") | ||
// ext.PeerHostname.Set(span, host) | ||
// span.SetTag("http.path", op.PathPattern) | ||
// ext.HTTPMethod.Set(span, op.Method) | ||
|
||
// _ = span.Tracer().Inject( | ||
// span.Context(), | ||
// opentracing.HTTPHeaders, | ||
// opentracing.HTTPHeadersCarrier(header)) | ||
|
||
// return span | ||
// } | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel" | ||
tracesdk "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/go-openapi/runtime" | ||
) | ||
|
||
func Test_OpenTelemetryRuntime_submit(t *testing.T) { | ||
t.Parallel() | ||
|
||
exporter := tracetest.NewInMemoryExporter() | ||
|
||
tp := tracesdk.NewTracerProvider( | ||
tracesdk.WithSampler(tracesdk.AlwaysSample()), | ||
tracesdk.WithSyncer(exporter), | ||
) | ||
|
||
otel.SetTracerProvider(tp) | ||
|
||
tracer := tp.Tracer("go-runtime") | ||
ctx, _ := tracer.Start(context.Background(), "op") | ||
|
||
testOpenTelemetrySubmit(t, testOperation(ctx), exporter, 1) | ||
} | ||
|
||
// func Test_OpenTelemetryRuntime_submit_nilAuthInfo(t *testing.T) { | ||
// t.Parallel() | ||
// tracer := mocktracer.New() | ||
// _, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "op") | ||
// operation := testOperation(ctx) | ||
// operation.AuthInfo = nil | ||
// testOpenTelemetrySubmit(t, operation, tracer, 1) | ||
// } | ||
|
||
// func Test_OpenTelemetryRuntime_submit_nilContext(t *testing.T) { | ||
// t.Parallel() | ||
// tracer := mocktracer.New() | ||
// _, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "op") | ||
// operation := testOperation(ctx) | ||
// operation.Context = nil | ||
// testOpenTelemetrySubmit(t, operation, tracer, 0) // just don't panic | ||
// } | ||
|
||
// func Test_injectOpenTelemetrySpanContext(t *testing.T) { | ||
// t.Parallel() | ||
// tracer := mocktracer.New() | ||
// _, ctx := opentracing.StartSpanFromContextWithTracer(context.Background(), tracer, "op") | ||
// header := map[string][]string{} | ||
// createOpenTelemetryClientSpan(testOperation(ctx), header, "", nil) | ||
|
||
// // values are random - just check that something was injected | ||
// assert.Len(t, header, 3) | ||
// } | ||
|
||
func testOpenTelemetrySubmit(t *testing.T, operation *runtime.ClientOperation, exporter *tracetest.InMemoryExporter, expectedSpanCount int) { | ||
header := map[string][]string{} | ||
r := newOpenTelemetryTransport(&mockRuntime{runtime.TestClientRequest{Headers: header}}, | ||
"remote_host", | ||
[]trace.SpanStartOption{}) | ||
|
||
// // opentracing.Tag{ | ||
// Key: string(ext.PeerService), | ||
// Value: "service", | ||
// } | ||
|
||
_, err := r.Submit(operation) | ||
require.NoError(t, err) | ||
|
||
spans := exporter.GetSpans() | ||
assert.Len(t, spans, expectedSpanCount) | ||
|
||
if expectedSpanCount == 1 { | ||
span := spans[0] | ||
assert.Equal(t, "getCluster", span.Name) | ||
// assert.Equal(t, map[string]interface{}{ | ||
// "component": "go-openapi", | ||
// "http.method": "GET", | ||
// "http.path": "/kubernetes-clusters/{cluster_id}", | ||
// "http.status_code": uint16(490), | ||
// "peer.hostname": "remote_host", | ||
// "peer.service": "service", | ||
// "span.kind": ext.SpanKindRPCClientEnum, | ||
// "error": true, | ||
// }, span.Tags()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters