Debian Codegraph @

pdata/xpdata/request/profiles_request.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package request // import "go.opentelemetry.io/collector/pdata/xpdata/request"
5
6 import (
7 "context"
8 "fmt"
9
10 "go.opentelemetry.io/collector/pdata/internal"
11 "go.opentelemetry.io/collector/pdata/pprofile"
12 )
13
14 // MarshalProfiles marshals pprofile.Profiles along with the context into a byte slice.
15 func MarshalProfiles(ctx context.Context, ld pprofile.Profiles) ([]byte, error) {
16 pr := internal.ProfilesRequest{
17 FormatVersion: requestFormatVersion,
18 ProfilesData: internal.ProfilesToProto(internal.ProfilesWrapper(ld)),
19 RequestContext: encodeContext(ctx),
20 }
21 buf := make([]byte, pr.SizeProto())
22 pr.MarshalProto(buf)
23 return buf, nil
24 }
25
26 // UnmarshalProfiles unmarshals a byte slice into pprofile.Profiles and the context.
27 func UnmarshalProfiles(buf []byte) (context.Context, pprofile.Profiles, error) {
28 ctx := context.Background()
29 if !isRequestPayloadV1(buf) {
30 return ctx, pprofile.Profiles{}, ErrInvalidFormat
31 }
32 pr := internal.ProfilesRequest{}
33 if err := pr.UnmarshalProto(buf); err != nil {
34 return ctx, pprofile.Profiles{}, fmt.Errorf("failed to unmarshal profiles request: %w", err)
35 }
36 return decodeContext(ctx, pr.RequestContext), pprofile.Profiles(internal.ProfilesFromProto(pr.ProfilesData)), nil
37 }