Debian Codegraph @

docstore/otel_test.go

raw
1 // Copyright 2019-2025 The Go Cloud Development Kit Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package docstore_test
16
17 import (
18 "context"
19 "gocloud.dev/docstore"
20 "gocloud.dev/docstore/memdocstore"
21 "gocloud.dev/gcerrors"
22 "gocloud.dev/internal/testing/oteltest"
23 "testing"
24 )
25
26 func TestOpenTelemetry(t *testing.T) {
27 ctx := context.Background()
28
29 // Setup the test exporter for both trace and metrics.
30 te := oteltest.NewTestExporter(t, docstore.OpenTelemetryViews)
31 defer te.Shutdown(ctx)
32
33 // Open a collection for testing.
34 coll, err := memdocstore.OpenCollection("_id", nil)
35 if err != nil {
36 t.Fatal(err)
37 }
38 defer coll.Close()
39
40 // Test ActionList.Do by creating a document.
41 if err := coll.Create(ctx, map[string]any{"_id": "a", "count": 0}); err != nil {
42 t.Fatal(err)
43 }
44
45 // Test Query.Get.
46 iter := coll.Query().Get(ctx)
47 iter.Stop()
48
49 spanStubs := te.GetSpans()
50 metrics := te.GetMetrics(ctx)
51 const (
52 pkgName = "gocloud.dev/docstore"
53 driver = "gocloud.dev/docstore/memdocstore"
54 )
55
56 diff := oteltest.Diff(spanStubs.Snapshots(), metrics, pkgName, driver, []oteltest.Call{
57 {Method: "ActionList.Do", Code: gcerrors.OK},
58 {Method: "Query.Get", Code: gcerrors.OK},
59 })
60 if diff != "" {
61 t.Error(diff)
62 }
63 }