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.
Add missing network connection attributes to tcp/udp (#134)
* Add IP address resolver helper Signed-off-by: Dominik Rosiek <[email protected]> * tcp: use ip address resolver Signed-off-by: Dominik Rosiek <[email protected]> * udp: use ip address resolver Signed-off-by: Dominik Rosiek <[email protected]> * tcp: docs: update add_attributes description Signed-off-by: Dominik Rosiek <[email protected]> * udp: docs: update add_attributes description Signed-off-by: Dominik Rosiek <[email protected]> * Add cached IPResolver Signed-off-by: Dominik Rosiek <[email protected]> * Initialize IPResolver every time Signed-off-by: Dominik Rosiek <[email protected]> * docs: polish markdown for Signed-off-by: Dominik Rosiek <[email protected]> * udp: tcp: conditionally initialize resolver Signed-off-by: Dominik Rosiek <[email protected]> * Add tests for ip resolver Signed-off-by: Dominik Rosiek <[email protected]> * Get rid of race in ip_resolver test Signed-off-by: Dominik Rosiek <[email protected]>
- Loading branch information
1 parent
0ff9df0
commit 8b85a69
Showing
8 changed files
with
249 additions
and
6 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package helper | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// cacheEntry keeps information about host and expiration time | ||
type cacheEntry struct { | ||
hostname string | ||
expireTime time.Time | ||
} | ||
|
||
const ( | ||
defaultInvalidationInterval time.Duration = 5 * time.Minute | ||
) | ||
|
||
type IPResolver struct { | ||
cache map[string]cacheEntry | ||
mutex sync.RWMutex | ||
done chan bool | ||
stopped bool | ||
invalidationInterval time.Duration | ||
} | ||
|
||
// Create new resolver | ||
func NewIpResolver() *IPResolver { | ||
r := &IPResolver{ | ||
cache: make(map[string]cacheEntry), | ||
stopped: false, | ||
done: make(chan bool), | ||
invalidationInterval: defaultInvalidationInterval, | ||
} | ||
r.start() | ||
return r | ||
} | ||
|
||
// Stop cache invalidation | ||
func (r *IPResolver) Stop() { | ||
r.mutex.Lock() | ||
if r.stopped { | ||
r.mutex.Unlock() | ||
return | ||
} | ||
|
||
r.stopped = true | ||
r.mutex.Unlock() | ||
r.done <- true | ||
} | ||
|
||
// start runs cache invalidation every 5 minutes | ||
func (r *IPResolver) start() { | ||
ticker := time.NewTicker(r.invalidationInterval) | ||
go func() { | ||
for { | ||
select { | ||
case <-r.done: | ||
ticker.Stop() | ||
return | ||
case <-ticker.C: | ||
r.mutex.Lock() | ||
r.invalidateCache() | ||
r.mutex.Unlock() | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// invalidateCache removes not longer valid entries from cache | ||
func (r *IPResolver) invalidateCache() { | ||
now := time.Now() | ||
for key, entry := range r.cache { | ||
if entry.expireTime.Before(now) { | ||
delete(r.cache, key) | ||
} | ||
} | ||
} | ||
|
||
// GetHostFromIp returns hostname for given ip | ||
// It is taken from cache if exists, | ||
// otherwise lookup is performed and result is put into cache | ||
func (r *IPResolver) GetHostFromIp(ip string) (host string) { | ||
r.mutex.RLock() | ||
entry, ok := r.cache[ip] | ||
if ok { | ||
host = entry.hostname | ||
defer r.mutex.RUnlock() | ||
return host | ||
} | ||
r.mutex.RUnlock() | ||
|
||
host = r.lookupIpAddr(ip) | ||
|
||
r.mutex.Lock() | ||
r.cache[ip] = cacheEntry{ | ||
hostname: host, | ||
expireTime: time.Now().Add(5 * time.Minute), | ||
} | ||
r.mutex.Unlock() | ||
|
||
return host | ||
} | ||
|
||
// lookupIpAddr resturns hostname based on ip address | ||
func (r *IPResolver) lookupIpAddr(ip string) (host string) { | ||
res, err := net.LookupAddr(ip) | ||
if err != nil || len(res) == 0 { | ||
return ip | ||
} | ||
|
||
host = res[0] | ||
// Trim one trailing '.'. | ||
if last := len(host) - 1; last >= 0 && host[last] == '.' { | ||
host = host[:last] | ||
} | ||
return host | ||
} |
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,75 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
"unsafe" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestIPResolverCacheLookup(t *testing.T) { | ||
resolver := NewIpResolver() | ||
resolver.cache["127.0.0.1"] = cacheEntry{ | ||
hostname: "definitely invalid hostname", | ||
expireTime: time.Now().Add(time.Hour), | ||
} | ||
|
||
require.Equal(t, "definitely invalid hostname", resolver.GetHostFromIp("127.0.0.1")) | ||
} | ||
|
||
func TestIPResolverCacheInvalidation(t *testing.T) { | ||
resolver := NewIpResolver() | ||
|
||
resolver.cache["127.0.0.1"] = cacheEntry{ | ||
hostname: "definitely invalid hostname", | ||
expireTime: time.Now().Add(-1 * time.Hour), | ||
} | ||
|
||
resolver.Stop() | ||
resolver.invalidateCache() | ||
|
||
hostname := resolver.lookupIpAddr("127.0.0.1") | ||
require.Equal(t, hostname, resolver.GetHostFromIp("127.0.0.1")) | ||
} | ||
|
||
func TestIPResolver100Hits(t *testing.T) { | ||
resolver := NewIpResolver() | ||
resolver.cache["127.0.0.1"] = cacheEntry{ | ||
hostname: "definitely invalid hostname", | ||
expireTime: time.Now().Add(time.Hour), | ||
} | ||
|
||
for i := 0; i < 100; i++ { | ||
require.Equal(t, "definitely invalid hostname", resolver.GetHostFromIp("127.0.0.1")) | ||
} | ||
} | ||
|
||
func TestIPResolverWithMultipleStops(t *testing.T) { | ||
resolver := NewIpResolver() | ||
|
||
resolver.Stop() | ||
resolver.Stop() | ||
} | ||
|
||
func TestSizes(t *testing.T) { | ||
fmt.Printf("string %v \n", unsafe.Sizeof("")) | ||
fmt.Printf("cache entry %v \n", unsafe.Sizeof(cacheEntry{})) | ||
fmt.Printf("time %v \n", unsafe.Sizeof(time.Now())) | ||
fmt.Printf("time %v \n", unsafe.Sizeof(time.Now())) | ||
} |