File: id.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (44 lines) | stat: -rw-r--r-- 912 bytes parent folder | download | duplicates (2)
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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package cat

import (
	"errors"
	"strconv"
	"strings"
)

// IDHeader represents a decoded cross process ID header (generally encoded as
// a string in the form ACCOUNT#BLOB).
type IDHeader struct {
	AccountID int
	Blob      string
}

var (
	errInvalidAccountID = errors.New("invalid account ID")
)

// NewIDHeader parses the given decoded ID header and creates an IDHeader
// representing it.
func NewIDHeader(in []byte) (*IDHeader, error) {
	parts := strings.Split(string(in), "#")
	if len(parts) != 2 {
		return nil, errUnexpectedArraySize{
			label:    "unexpected number of ID elements",
			expected: 2,
			actual:   len(parts),
		}
	}

	account, err := strconv.Atoi(parts[0])
	if err != nil {
		return nil, errInvalidAccountID
	}

	return &IDHeader{
		AccountID: account,
		Blob:      parts[1],
	}, nil
}