File: path_hash.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 (38 lines) | stat: -rw-r--r-- 1,059 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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package cat

import (
	"crypto/md5"
	"encoding/binary"
	"fmt"
	"regexp"
)

var pathHashValidator = regexp.MustCompile("^[0-9a-f]{8}$")

// GeneratePathHash generates a path hash given a referring path hash,
// transaction name, and application name. referringPathHash can be an empty
// string if there was no referring path hash.
func GeneratePathHash(referringPathHash, txnName, appName string) (string, error) {
	var rph uint32
	if referringPathHash != "" {
		if !pathHashValidator.MatchString(referringPathHash) {
			// Per the spec, invalid referring path hashes should be treated as "0".
			referringPathHash = "0"
		}

		if _, err := fmt.Sscanf(referringPathHash, "%x", &rph); err != nil {
			fmt.Println(rph)
			return "", err
		}
		rph = (rph << 1) | (rph >> 31)
	}

	hashInput := fmt.Sprintf("%s;%s", appName, txnName)
	hash := md5.Sum([]byte(hashInput))
	low32 := binary.BigEndian.Uint32(hash[12:])

	return fmt.Sprintf("%08x", rph^low32), nil
}