-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
observability: add unitests for tracing
Signed-off-by: Prashanth Dintyala <[email protected]>
- Loading branch information
1 parent
6e6af34
commit 15db34f
Showing
6 changed files
with
200 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Package tracing offers support for distributed tracing utilizing OpenTelemetry (OTEL). | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
*/ | ||
package tracing | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestTracing(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, t.Name()) | ||
} |
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,165 @@ | ||
//go:build oteltracing | ||
|
||
// Package tracing offers support for distributed tracing utilizing OpenTelemetry (OTEL). | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
*/ | ||
package tracing | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
"github.com/NVIDIA/aistore/cmn" | ||
"github.com/NVIDIA/aistore/core/meta" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
"go.opentelemetry.io/otel/sdk/trace/tracetest" | ||
) | ||
|
||
var _ = Describe("Tracing", func() { | ||
const aisVersion = "v3.33" | ||
|
||
var ( | ||
exporter *tracetest.InMemoryExporter | ||
|
||
origExporter = newExporter | ||
dummySnode = &meta.Snode{DaeID: "test", DaeType: "proxy"} | ||
|
||
newTestHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("-")) | ||
}) | ||
|
||
expectResourceAttrs = func(attrs []attribute.KeyValue) { | ||
expectedAttributes := map[string]string{ | ||
"service.name": "aistore-" + dummySnode.DaeType, | ||
"version": aisVersion, | ||
"daemonID": dummySnode.DaeID, | ||
} | ||
Expect(len(attrs)).NotTo(BeEquivalentTo(0)) | ||
matched := 0 | ||
for _, attribute := range attrs { | ||
value, ok := expectedAttributes[string(attribute.Key)] | ||
if !ok { | ||
continue | ||
} | ||
Expect(attribute.Value.AsString()).To(BeEquivalentTo(value)) | ||
matched++ | ||
} | ||
Expect(matched).To(BeEquivalentTo(len(expectedAttributes))) | ||
} | ||
) | ||
|
||
BeforeEach(func() { | ||
exporter = tracetest.NewInMemoryExporter() | ||
newExporter = func(conf *cmn.TracingConf) (trace.SpanExporter, error) { | ||
return exporter, nil | ||
} | ||
}) | ||
|
||
AfterEach(func() { | ||
newExporter = origExporter | ||
}) | ||
|
||
Describe("Server", func() { | ||
AfterEach(func() { | ||
Shutdown() | ||
tp = nil | ||
}) | ||
It("should export server trace when tracing enabled", func() { | ||
Init(&cmn.TracingConf{ | ||
ExporterEndpoint: "dummy", | ||
Enabled: true, | ||
SamplerProbability: 1.0, | ||
}, dummySnode, aisVersion) | ||
Expect(IsEnabled()).To(BeTrue()) | ||
|
||
server := httptest.NewServer(NewTraceableHandler(newTestHandler, "testendpoint")) | ||
defer server.Close() | ||
|
||
_, err := http.Get(server.URL) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
tp.ForceFlush(context.Background()) | ||
|
||
Expect(len(exporter.GetSpans())).To(BeEquivalentTo(1)) | ||
span := exporter.GetSpans()[0] | ||
expectResourceAttrs(span.Resource.Attributes()) | ||
}) | ||
|
||
It("should do nothing when tracing disabled", func() { | ||
Init(&cmn.TracingConf{ | ||
Enabled: false, | ||
}, dummySnode, aisVersion) | ||
Expect(IsEnabled()).To(BeFalse()) | ||
|
||
server := httptest.NewServer(NewTraceableHandler(newTestHandler, "testendpoint")) | ||
defer server.Close() | ||
|
||
_, err := http.Get(server.URL) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(len(exporter.GetSpans())).To(BeEquivalentTo(0)) | ||
}) | ||
}) | ||
|
||
Describe("Client", func() { | ||
AfterEach(func() { | ||
Shutdown() | ||
tp = nil | ||
}) | ||
It("should export client trace when tracing enabled", func() { | ||
Init(&cmn.TracingConf{ | ||
ExporterEndpoint: "dummy", | ||
Enabled: true, | ||
SamplerProbability: 1.0, | ||
}, dummySnode, aisVersion) | ||
Expect(IsEnabled()).To(BeTrue()) | ||
|
||
server := httptest.NewServer(newTestHandler) | ||
defer server.Close() | ||
|
||
client := NewTraceableClient(http.DefaultClient) | ||
_, isOtelType := client.Transport.(*otelhttp.Transport) | ||
Expect(isOtelType).To(BeTrue()) | ||
|
||
resp, err := client.Get(server.URL) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = io.ReadAll(resp.Body) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
tp.ForceFlush(context.Background()) | ||
|
||
Expect(len(exporter.GetSpans())).To(BeEquivalentTo(1)) | ||
span := exporter.GetSpans()[0] | ||
expectResourceAttrs(span.Resource.Attributes()) | ||
}) | ||
|
||
It("should do nothing when tracing disabled", func() { | ||
Init(&cmn.TracingConf{ | ||
Enabled: false, | ||
}, dummySnode, aisVersion) | ||
Expect(IsEnabled()).To(BeFalse()) | ||
|
||
server := httptest.NewServer(newTestHandler) | ||
defer server.Close() | ||
|
||
client := NewTraceableClient(http.DefaultClient) | ||
|
||
resp, err := client.Get(server.URL) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
_, err = io.ReadAll(resp.Body) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(len(exporter.GetSpans())).To(BeEquivalentTo(0)) | ||
}) | ||
}) | ||
}) |