forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[exporter/awsemfexporter] Add app signals specific user agent. (open-…
- Loading branch information
Showing
12 changed files
with
281 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: awsemfexporter | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Adds additional user agents for the telemetry SDKs if AppSignals is enabled. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [170] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# e.g. '[aws]' | ||
# Include 'aws' if the change is done done by cwa | ||
# Default: '[user]' | ||
change_logs: [aws] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
116 changes: 116 additions & 0 deletions
116
exporter/awsemfexporter/internal/appsignals/useragent.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package appsignals | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/jellydator/ttlcache/v3" | ||
semconv "go.opentelemetry.io/collector/semconv/v1.18.0" | ||
) | ||
|
||
const ( | ||
handlerName = "aws.appsignals.UserAgentHandler" | ||
// defaultTTL is how long an item in the cache will remain if it has not been re-seen. | ||
defaultTTL = time.Minute | ||
// cacheSize is the maximum number of unique telemetry SDK languages that can be stored before one will be evicted. | ||
cacheSize = 5 | ||
// attrLengthLimit is the maximum length of the language and version that will be used for the user agent. | ||
attrLengthLimit = 20 | ||
|
||
// TODO: Available in semconv/v1.21.0+. Replace after collector dependency is v0.91.0+. | ||
attributeTelemetryDistroVersion = "telemetry.distro.version" | ||
) | ||
|
||
type UserAgent struct { | ||
mu sync.RWMutex | ||
prebuiltStr string | ||
cache *ttlcache.Cache[string, string] | ||
} | ||
|
||
func NewUserAgent() *UserAgent { | ||
return newUserAgent(defaultTTL) | ||
} | ||
|
||
func newUserAgent(ttl time.Duration) *UserAgent { | ||
ua := &UserAgent{ | ||
cache: ttlcache.New[string, string]( | ||
ttlcache.WithTTL[string, string](ttl), | ||
ttlcache.WithCapacity[string, string](cacheSize), | ||
), | ||
} | ||
ua.cache.OnEviction(func(context.Context, ttlcache.EvictionReason, *ttlcache.Item[string, string]) { | ||
ua.build() | ||
}) | ||
go ua.cache.Start() | ||
return ua | ||
} | ||
|
||
// Handler creates a named handler with the UserAgent's handle function. | ||
func (ua *UserAgent) Handler() request.NamedHandler { | ||
return request.NamedHandler{ | ||
Name: handlerName, | ||
Fn: ua.handle, | ||
} | ||
} | ||
|
||
// handle adds the pre-built user agent string to the user agent header. | ||
func (ua *UserAgent) handle(r *request.Request) { | ||
ua.mu.RLock() | ||
defer ua.mu.RUnlock() | ||
request.AddToUserAgent(r, ua.prebuiltStr) | ||
} | ||
|
||
// Process takes the telemetry SDK language and version and adds them to the cache. If it already exists in the | ||
// cache and has the same value, extends the TTL. If not, then it sets it and rebuilds the user agent string. | ||
func (ua *UserAgent) Process(labels map[string]string) { | ||
language := labels[semconv.AttributeTelemetrySDKLanguage] | ||
version := labels[attributeTelemetryDistroVersion] | ||
if version == "" { | ||
version = labels[semconv.AttributeTelemetryAutoVersion] | ||
} | ||
if language != "" && version != "" { | ||
language = truncate(language, attrLengthLimit) | ||
version = truncate(version, attrLengthLimit) | ||
value := ua.cache.Get(language) | ||
if value == nil || value.Value() != version { | ||
ua.cache.Set(language, version, ttlcache.DefaultTTL) | ||
ua.build() | ||
} | ||
} | ||
} | ||
|
||
// build the user agent string from the items in the cache. Format is telemetry-sdk (<lang1>/<ver1>;<lang2>/<ver2>). | ||
func (ua *UserAgent) build() { | ||
ua.mu.Lock() | ||
defer ua.mu.Unlock() | ||
var items []string | ||
for _, item := range ua.cache.Items() { | ||
items = append(items, formatStr(item.Key(), item.Value())) | ||
} | ||
ua.prebuiltStr = "" | ||
if len(items) > 0 { | ||
sort.Strings(items) | ||
ua.prebuiltStr = fmt.Sprintf("telemetry-sdk (%s)", strings.Join(items, ";")) | ||
} | ||
} | ||
|
||
// formatStr formats the telemetry SDK language and version into the user agent format. | ||
func formatStr(language, version string) string { | ||
return language + "/" + version | ||
} | ||
|
||
func truncate(s string, n int) string { | ||
s = strings.TrimSpace(s) | ||
if len(s) > n { | ||
return strings.TrimSpace(s[:n]) | ||
} | ||
return s | ||
} |
111 changes: 111 additions & 0 deletions
111
exporter/awsemfexporter/internal/appsignals/useragent_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package appsignals | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/stretchr/testify/assert" | ||
semconv "go.opentelemetry.io/collector/semconv/v1.18.0" | ||
) | ||
|
||
func TestUserAgent(t *testing.T) { | ||
testCases := map[string]struct { | ||
labelSets []map[string]string | ||
want string | ||
}{ | ||
"WithEmpty": {}, | ||
"WithPartialAttributes": { | ||
labelSets: []map[string]string{ | ||
{ | ||
semconv.AttributeTelemetrySDKLanguage: "foo", | ||
}, | ||
{ | ||
semconv.AttributeTelemetryAutoVersion: "1.0", | ||
}, | ||
}, | ||
}, | ||
"WithMultipleLanguages": { | ||
labelSets: []map[string]string{ | ||
{ | ||
semconv.AttributeTelemetrySDKLanguage: "foo", | ||
attributeTelemetryDistroVersion: "1.1", | ||
}, | ||
{ | ||
semconv.AttributeTelemetrySDKLanguage: "bar", | ||
semconv.AttributeTelemetryAutoVersion: "2.0", | ||
attributeTelemetryDistroVersion: "1.0", | ||
}, | ||
{ | ||
semconv.AttributeTelemetrySDKLanguage: "baz", | ||
semconv.AttributeTelemetryAutoVersion: "2.0", | ||
}, | ||
}, | ||
want: "telemetry-sdk (bar/1.0;baz/2.0;foo/1.1)", | ||
}, | ||
"WithMultipleVersions": { | ||
labelSets: []map[string]string{ | ||
{ | ||
semconv.AttributeTelemetrySDKLanguage: "test", | ||
semconv.AttributeTelemetryAutoVersion: "1.1", | ||
}, | ||
{ | ||
semconv.AttributeTelemetrySDKLanguage: "test", | ||
attributeTelemetryDistroVersion: "1.0", | ||
}, | ||
}, | ||
want: "telemetry-sdk (test/1.0)", | ||
}, | ||
"WithTruncatedAttributes": { | ||
labelSets: []map[string]string{ | ||
{ | ||
semconv.AttributeTelemetrySDKLanguage: " incrediblyverboselanguagename", | ||
semconv.AttributeTelemetryAutoVersion: "notsemanticversioningversion", | ||
}, | ||
}, | ||
want: "telemetry-sdk (incrediblyverboselan/notsemanticversionin)", | ||
}, | ||
} | ||
for name, testCase := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
userAgent := NewUserAgent() | ||
for _, labelSet := range testCase.labelSets { | ||
userAgent.Process(labelSet) | ||
} | ||
req := &request.Request{ | ||
HTTPRequest: &http.Request{ | ||
Header: http.Header{}, | ||
}, | ||
} | ||
userAgent.Handler().Fn(req) | ||
assert.Equal(t, testCase.want, req.HTTPRequest.Header.Get("User-Agent")) | ||
}) | ||
} | ||
} | ||
|
||
func TestUserAgentExpiration(t *testing.T) { | ||
userAgent := newUserAgent(50 * time.Millisecond) | ||
req := &request.Request{ | ||
HTTPRequest: &http.Request{ | ||
Header: http.Header{}, | ||
}, | ||
} | ||
labels := map[string]string{ | ||
semconv.AttributeTelemetrySDKLanguage: "test", | ||
semconv.AttributeTelemetryAutoVersion: "1.0", | ||
} | ||
userAgent.Process(labels) | ||
userAgent.handle(req) | ||
assert.Equal(t, "telemetry-sdk (test/1.0)", req.HTTPRequest.Header.Get("User-Agent")) | ||
|
||
// wait for expiration | ||
time.Sleep(100 * time.Millisecond) | ||
// reset user-agent header | ||
req.HTTPRequest.Header.Del("User-Agent") | ||
userAgent.handle(req) | ||
assert.Empty(t, req.HTTPRequest.Header.Get("User-Agent")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.