File: metric.go

package info (click to toggle)
golang-opentelemetry-otel 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, experimental
  • size: 4,880 kB
  • sloc: sh: 244; makefile: 196
file content (167 lines) | stat: -rw-r--r-- 4,750 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package stdoutmetric // import "go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"

import (
	"context"
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/metric/sdkapi"
	exportmetric "go.opentelemetry.io/otel/sdk/export/metric"
	"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
	"go.opentelemetry.io/otel/sdk/instrumentation"
	"go.opentelemetry.io/otel/sdk/resource"
)

type metricExporter struct {
	config config
}

var _ exportmetric.Exporter = &metricExporter{}

type line struct {
	Name      string      `json:"Name"`
	Min       interface{} `json:"Min,omitempty"`
	Max       interface{} `json:"Max,omitempty"`
	Sum       interface{} `json:"Sum,omitempty"`
	Count     interface{} `json:"Count,omitempty"`
	LastValue interface{} `json:"Last,omitempty"`

	// Note: this is a pointer because omitempty doesn't work when time.IsZero()
	Timestamp *time.Time `json:"Timestamp,omitempty"`
}

func (e *metricExporter) TemporalityFor(desc *sdkapi.Descriptor, kind aggregation.Kind) aggregation.Temporality {
	return aggregation.StatelessTemporalitySelector().TemporalityFor(desc, kind)
}

func (e *metricExporter) Export(_ context.Context, res *resource.Resource, reader exportmetric.InstrumentationLibraryReader) error {
	var aggError error
	var batch []line
	aggError = reader.ForEach(func(lib instrumentation.Library, mr exportmetric.Reader) error {

		var instLabels []attribute.KeyValue
		if name := lib.Name; name != "" {
			instLabels = append(instLabels, attribute.String("instrumentation.name", name))
			if version := lib.Version; version != "" {
				instLabels = append(instLabels, attribute.String("instrumentation.version", version))
			}
			if schema := lib.SchemaURL; schema != "" {
				instLabels = append(instLabels, attribute.String("instrumentation.schema_url", schema))
			}
		}
		instSet := attribute.NewSet(instLabels...)
		encodedInstLabels := instSet.Encoded(e.config.LabelEncoder)

		return mr.ForEach(e, func(record exportmetric.Record) error {
			desc := record.Descriptor()
			agg := record.Aggregation()
			kind := desc.NumberKind()
			encodedResource := res.Encoded(e.config.LabelEncoder)

			var expose line

			if sum, ok := agg.(aggregation.Sum); ok {
				value, err := sum.Sum()
				if err != nil {
					return err
				}
				expose.Sum = value.AsInterface(kind)
			}

			if mmsc, ok := agg.(aggregation.MinMaxSumCount); ok {
				count, err := mmsc.Count()
				if err != nil {
					return err
				}
				expose.Count = count

				max, err := mmsc.Max()
				if err != nil {
					return err
				}
				expose.Max = max.AsInterface(kind)

				min, err := mmsc.Min()
				if err != nil {
					return err
				}
				expose.Min = min.AsInterface(kind)
			} else if lv, ok := agg.(aggregation.LastValue); ok {
				value, timestamp, err := lv.LastValue()
				if err != nil {
					return err
				}
				expose.LastValue = value.AsInterface(kind)

				if e.config.Timestamps {
					expose.Timestamp = &timestamp
				}
			}

			var encodedLabels string
			iter := record.Labels().Iter()
			if iter.Len() > 0 {
				encodedLabels = record.Labels().Encoded(e.config.LabelEncoder)
			}

			var sb strings.Builder

			sb.WriteString(desc.Name())

			if len(encodedLabels) > 0 || len(encodedResource) > 0 || len(encodedInstLabels) > 0 {
				sb.WriteRune('{')
				sb.WriteString(encodedResource)
				if len(encodedInstLabels) > 0 && len(encodedResource) > 0 {
					sb.WriteRune(',')
				}
				sb.WriteString(encodedInstLabels)
				if len(encodedLabels) > 0 && (len(encodedInstLabels) > 0 || len(encodedResource) > 0) {
					sb.WriteRune(',')
				}
				sb.WriteString(encodedLabels)
				sb.WriteRune('}')
			}

			expose.Name = sb.String()

			batch = append(batch, expose)
			return nil
		})
	})
	if len(batch) == 0 {
		return aggError
	}

	data, err := e.marshal(batch)
	if err != nil {
		return err
	}
	fmt.Fprintln(e.config.Writer, string(data))

	return aggError
}

// marshal v with appropriate indentation.
func (e *metricExporter) marshal(v interface{}) ([]byte, error) {
	if e.config.PrettyPrint {
		return json.MarshalIndent(v, "", "\t")
	}
	return json.Marshal(v)
}