• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ory / x / 10831805583

12 Sep 2024 01:26PM UTC coverage: 62.702% (-0.2%) from 62.856%
10831805583

push

github

web-flow
feat(otelx): nullable attribute helpers (#809)

0 of 28 new or added lines in 1 file covered. (0.0%)

7143 of 11392 relevant lines covered (62.7%)

4066.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/otelx/attribute.go
1
// Copyright © 2023 Ory Corp
2
// SPDX-License-Identifier: Apache-2.0
3

4
package otelx
5

6
import (
7
        "database/sql"
8
        "fmt"
9

10
        "go.opentelemetry.io/otel/attribute"
11
)
12

13
const nullString = "<null>"
14

15
func StringAttrs(attrs map[string]string) []attribute.KeyValue {
×
16
        s := []attribute.KeyValue{}
×
17
        for k, v := range attrs {
×
18
                s = append(s, attribute.String(k, v))
×
19
        }
×
20
        return s
×
21
}
22

NEW
23
func AutoInt[I int | int32 | int64](k string, v I) attribute.KeyValue {
×
NEW
24
        // Internally, the OpenTelemetry SDK uses int64 for all integer values anyway.
×
NEW
25
        return attribute.Int64(k, int64(v))
×
NEW
26
}
×
27

NEW
28
func Nullable[V any, VN *V | sql.Null[V], A func(string, V) attribute.KeyValue](a A, k string, v VN) attribute.KeyValue {
×
NEW
29
        switch v := any(v).(type) {
×
NEW
30
        case *V:
×
NEW
31
                if v == nil {
×
NEW
32
                        return attribute.String(k, nullString)
×
NEW
33
                }
×
NEW
34
                return a(k, *v)
×
NEW
35
        case sql.Null[V]:
×
NEW
36
                if !v.Valid {
×
NEW
37
                        return attribute.String(k, nullString)
×
NEW
38
                }
×
NEW
39
                return a(k, v.V)
×
40
        }
41
        // This should never happen, as the type switch above is exhaustive to the generic type VN.
NEW
42
        return attribute.String(k, fmt.Sprintf("<got unsupported type %T>", v))
×
43
}
44

NEW
45
func NullString[V *string | sql.Null[string]](k string, v V) attribute.KeyValue {
×
NEW
46
        return Nullable(attribute.String, k, v)
×
NEW
47
}
×
48

NEW
49
func NullStringer(k string, v fmt.Stringer) attribute.KeyValue {
×
NEW
50
        if v == nil {
×
NEW
51
                return attribute.String(k, nullString)
×
NEW
52
        }
×
NEW
53
        return attribute.String(k, v.String())
×
54
}
55

NEW
56
func NullInt[I int | int32 | int64, V *I | sql.Null[I]](k string, v V) attribute.KeyValue {
×
NEW
57
        return Nullable[I](AutoInt, k, v)
×
NEW
58
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc