github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/eum.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2018
     3  
     4  package instana
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  	"sort"
    10  	"strings"
    11  )
    12  
    13  const eumTemplate string = "eum.js"
    14  
    15  // EumSnippet generates javascript code to initialize JavaScript agent
    16  //
    17  // Deprecated: this snippet is outdated and this method will be removed in
    18  // the next major version. To learn about the way to install Instana EUM snippet
    19  // please refer to https://docs.instana.io/products/website_monitoring/#installation
    20  func EumSnippet(apiKey string, traceID string, meta map[string]string) string {
    21  
    22  	if len(apiKey) == 0 || len(traceID) == 0 {
    23  		return ""
    24  	}
    25  
    26  	b, err := ioutil.ReadFile(eumTemplate)
    27  	if err != nil {
    28  		return ""
    29  	}
    30  
    31  	snippet := strings.Replace(string(b), "$apiKey", apiKey, -1)
    32  	snippet = strings.Replace(snippet, "$traceId", traceID, -1)
    33  
    34  	var keys []string
    35  	for k := range meta {
    36  		keys = append(keys, k)
    37  	}
    38  	sort.Strings(keys)
    39  
    40  	var metaBuffer bytes.Buffer
    41  	for _, k := range keys {
    42  		metaBuffer.WriteString("ineum('meta','" + k + "','" + meta[k] + "');")
    43  	}
    44  
    45  	snippet = strings.Replace(snippet, "$meta", metaBuffer.String(), -1)
    46  
    47  	return snippet
    48  }