Debian Codegraph @

blob/otel_test.go

raw
1 // Copyright 2019 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 blob_test
16
17 import (
18 "go.opentelemetry.io/otel/attribute"
19 "go.opentelemetry.io/otel/sdk/metric/metricdata"
20 "gocloud.dev/blob"
21 "gocloud.dev/blob/memblob"
22 "gocloud.dev/gcerrors"
23 "gocloud.dev/internal/testing/oteltest"
24 "testing"
25 )
26
27 func TestOpenTelemetry(t *testing.T) {
28 ctx := t.Context()
29
30 te := oteltest.NewTestExporter(t, blob.OpenTelemetryViews)
31 defer func() { _ = te.Shutdown(ctx) }()
32
33 bytes := []byte("hello world")
34 b := memblob.OpenBucket(nil)
35 defer func() { _ = b.Close() }()
36
37 if err := b.WriteAll(ctx, "key", bytes, nil); err != nil {
38 t.Fatal(err)
39 }
40 if _, err := b.ReadAll(ctx, "key"); err != nil {
41 t.Fatal(err)
42 }
43 if _, err := b.Attributes(ctx, "key"); err != nil {
44 t.Fatal(err)
45 }
46 if _, _, err := b.ListPage(ctx, blob.FirstPageToken, 3, nil); err != nil {
47 t.Fatal(err)
48 }
49 if err := b.Delete(ctx, "key"); err != nil {
50 t.Fatal(err)
51 }
52 if _, err := b.ReadAll(ctx, "noSuchKey"); err == nil {
53 t.Fatal("got nil, want error")
54 }
55
56 const driver = "gocloud.dev/blob/memblob"
57
58 spans := te.GetSpans()
59 metrics := te.GetMetrics(ctx)
60
61 diff := oteltest.Diff(spans.Snapshots(), metrics, "gocloud.dev/blob", driver, []oteltest.Call{
62 {Method: "NewWriter", Code: gcerrors.OK},
63 {Method: "NewRangeReader", Code: gcerrors.OK},
64 {Method: "Attributes", Code: gcerrors.OK},
65 {Method: "ListPage", Code: gcerrors.OK},
66 {Method: "Delete", Code: gcerrors.OK},
67 {Method: "NewRangeReader", Code: gcerrors.NotFound},
68 })
69 if diff != "" {
70 t.Error(diff)
71 }
72
73 // Find and verify the bytes read/written metrics.
74 var sawRead, sawWritten bool
75 providerAttr := attribute.Key("gocdk_provider")
76
77 for _, sm := range metrics {
78
79 for _, data := range sm.Metrics {
80
81 switch data.Name {
82 case "gocloud.dev/blob/bytes_read":
83 if sawRead {
84 continue
85 }
86 sawRead = true
87 case "gocloud.dev/blob/bytes_written":
88 if sawWritten {
89 continue
90 }
91 sawWritten = true
92 default:
93 continue
94 }
95
96 providerVal, ok := sm.Scope.Attributes.Value(providerAttr)
97 if !ok || providerVal.AsString() != driver {
98 t.Errorf("provider tags for %s is : %s instead of :%s", data.Name, providerVal.AsString(), driver)
99 continue
100 }
101 sd, ok := data.Data.(metricdata.Sum[int64])
102 if !ok {
103 t.Errorf("%s: data is %T, want SumData", data.Name, data.Data)
104 continue
105 }
106 if got := int(sd.DataPoints[0].Value); got < len(bytes) {
107 t.Errorf("%s: got %d, want at least %d", data.Name, got, len(bytes))
108 }
109
110 if sawRead && sawWritten {
111 return
112 }
113 }
114 }
115 }