Debian Codegraph @

pdata/pprofile/json.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"
5
6 import (
7 "slices"
8
9 "go.opentelemetry.io/collector/pdata/internal/json"
10 "go.opentelemetry.io/collector/pdata/internal/otlp"
11 )
12
13 // JSONMarshaler marshals pprofile.Profiles to JSON bytes using the OTLP/JSON format.
14 type JSONMarshaler struct{}
15
16 // MarshalProfiles to the OTLP/JSON format.
17 func (*JSONMarshaler) MarshalProfiles(pd Profiles) ([]byte, error) {
18 dest := json.BorrowStream(nil)
19 defer json.ReturnStream(dest)
20 pd.getOrig().MarshalJSON(dest)
21 if dest.Error() != nil {
22 return nil, dest.Error()
23 }
24 return slices.Clone(dest.Buffer()), nil
25 }
26
27 // JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to pprofile.Profiles.
28 type JSONUnmarshaler struct{}
29
30 // UnmarshalProfiles from OTLP/JSON format into pprofile.Profiles.
31 func (*JSONUnmarshaler) UnmarshalProfiles(buf []byte) (Profiles, error) {
32 iter := json.BorrowIterator(buf)
33 defer json.ReturnIterator(iter)
34 pd := NewProfiles()
35 pd.getOrig().UnmarshalJSON(iter)
36 if iter.Error() != nil {
37 return Profiles{}, iter.Error()
38 }
39 otlp.MigrateProfiles(pd.getOrig().ResourceProfiles)
40 return pd, nil
41 }