summaryrefslogtreecommitdiff
path: root/vendor/go.opentelemetry.io/auto/sdk/internal/telemetry/resource.go
blob: cecad8bae3c98487598f4ecceca0e0b8d8bd6f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package telemetry

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
)

// Resource information.
type Resource struct {
	// Attrs are the set of attributes that describe the resource. Attribute
	// keys MUST be unique (it is not allowed to have more than one attribute
	// with the same key).
	Attrs []Attr `json:"attributes,omitempty"`
	// DroppedAttrs is the number of dropped attributes. If the value
	// is 0, then no attributes were dropped.
	DroppedAttrs uint32 `json:"droppedAttributesCount,omitempty"`
}

// UnmarshalJSON decodes the OTLP formatted JSON contained in data into r.
func (r *Resource) UnmarshalJSON(data []byte) error {
	decoder := json.NewDecoder(bytes.NewReader(data))

	t, err := decoder.Token()
	if err != nil {
		return err
	}
	if t != json.Delim('{') {
		return errors.New("invalid Resource type")
	}

	for decoder.More() {
		keyIface, err := decoder.Token()
		if err != nil {
			if errors.Is(err, io.EOF) {
				// Empty.
				return nil
			}
			return err
		}

		key, ok := keyIface.(string)
		if !ok {
			return fmt.Errorf("invalid Resource field: %#v", keyIface)
		}

		switch key {
		case "attributes":
			err = decoder.Decode(&r.Attrs)
		case "droppedAttributesCount", "dropped_attributes_count":
			err = decoder.Decode(&r.DroppedAttrs)
		default:
			// Skip unknown.
		}

		if err != nil {
			return err
		}
	}
	return nil
}