-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Validate that the trace middleware adds the expected traces * Validate that certificate hashes with both '/' and '+' characters are converted correctly
- Loading branch information
Showing
5 changed files
with
146 additions
and
5 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
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
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,54 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package server | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/otel" | ||
tracesdk "go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
"go.opentelemetry.io/otel/trace" | ||
"golang.org/x/exp/maps" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func GetTracer() (trace.Tracer, *tracetest.InMemoryExporter) { | ||
traceExporter := tracetest.NewInMemoryExporter() | ||
tracerProvider := tracesdk.NewTracerProvider( | ||
tracesdk.WithSampler(tracesdk.AlwaysSample()), | ||
tracesdk.WithSyncer(traceExporter), | ||
) | ||
otel.SetTracerProvider(tracerProvider) | ||
|
||
return tracerProvider.Tracer("test"), traceExporter | ||
} | ||
|
||
func AssertSpan(t *testing.T, span *tracetest.SpanStub, name string, attributes map[string]any) { | ||
assert.Equal(t, name, span.Name) | ||
assert.Len(t, span.Attributes, len(attributes)) | ||
var gotKeys []string | ||
for _, attr := range span.Attributes { | ||
gotKeys = append(gotKeys, string(attr.Key)) | ||
want, ok := attributes[string(attr.Key)] | ||
if !ok { | ||
t.Errorf("unexpected attribute %s", attr.Key) | ||
} | ||
switch want.(type) { | ||
case string: | ||
assert.Equal(t, want, attr.Value.AsString()) | ||
case int: | ||
assert.Equal(t, want, int(attr.Value.AsInt64())) | ||
case bool: | ||
assert.Equal(t, want, attr.Value.AsBool()) | ||
case float64: | ||
assert.Equal(t, want, attr.Value.AsFloat64()) | ||
default: | ||
t.Errorf("unsupported attribute type %T", want) | ||
} | ||
} | ||
sort.Strings(gotKeys) | ||
wantKeys := maps.Keys(attributes) | ||
sort.Strings(wantKeys) | ||
assert.Equal(t, wantKeys, gotKeys) | ||
} |