Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
go-common
Commits
438dda6c
Commit
438dda6c
authored
6 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Add tracing support to LDAP requests
parent
fd088454
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
ldap/pool.go
+45
-10
45 additions, 10 deletions
ldap/pool.go
with
45 additions
and
10 deletions
ldap/pool.go
+
45
−
10
View file @
438dda6c
...
...
@@ -8,6 +8,7 @@ import (
"time"
"github.com/cenkalti/backoff"
"go.opencensus.io/trace"
"gopkg.in/ldap.v2"
)
...
...
@@ -147,9 +148,18 @@ func NewConnectionPool(uri, bindDN, bindPw string, cacheSize int) (*ConnectionPo
},
nil
}
func
(
p
*
ConnectionPool
)
doRequest
(
ctx
context
.
Context
,
fn
func
(
*
ldap
.
Conn
)
error
)
error
{
return
backoff
.
Retry
(
func
()
error
{
conn
,
err
:=
p
.
Get
(
ctx
)
func
(
p
*
ConnectionPool
)
doRequest
(
ctx
context
.
Context
,
name
string
,
attrs
[]
trace
.
Attribute
,
fn
func
(
*
ldap
.
Conn
)
error
)
error
{
// Tracing: initialize a new client span.
sctx
,
span
:=
trace
.
StartSpan
(
ctx
,
name
,
trace
.
WithSpanKind
(
trace
.
SpanKindClient
))
defer
span
.
End
()
if
len
(
attrs
)
>
0
{
span
.
AddAttributes
(
attrs
...
)
}
rerr
:=
backoff
.
Retry
(
func
()
error
{
conn
,
err
:=
p
.
Get
(
sctx
)
if
err
!=
nil
{
// Here conn is nil, so we don't need to Release it.
if
isTemporaryLDAPError
(
err
)
{
...
...
@@ -158,7 +168,7 @@ func (p *ConnectionPool) doRequest(ctx context.Context, fn func(*ldap.Conn) erro
return
backoff
.
Permanent
(
err
)
}
if
deadline
,
ok
:=
ctx
.
Deadline
();
ok
{
if
deadline
,
ok
:=
s
ctx
.
Deadline
();
ok
{
conn
.
SetTimeout
(
time
.
Until
(
deadline
))
}
...
...
@@ -169,30 +179,42 @@ func (p *ConnectionPool) doRequest(ctx context.Context, fn func(*ldap.Conn) erro
}
return
err
},
backoff
.
WithContext
(
newExponentialBackOff
(),
ctx
))
// Tracing: set the final status.
span
.
SetStatus
(
errorToTraceStatus
(
rerr
))
return
rerr
}
// Search performs the given search request. It will retry the request
// on temporary errors.
func
(
p
*
ConnectionPool
)
Search
(
ctx
context
.
Context
,
searchRequest
*
ldap
.
SearchRequest
)
(
*
ldap
.
SearchResult
,
error
)
{
var
result
*
ldap
.
SearchResult
err
:=
p
.
doRequest
(
ctx
,
func
(
conn
*
ldap
.
Conn
)
error
{
var
err
error
result
,
err
=
conn
.
Search
(
searchRequest
)
return
err
err
:=
p
.
doRequest
(
ctx
,
"ldap.Search"
,
[]
trace
.
Attribute
{
trace
.
StringAttribute
(
"ldap.base"
,
searchRequest
.
BaseDN
),
trace
.
StringAttribute
(
"ldap.filter"
,
searchRequest
.
Filter
),
trace
.
Int64Attribute
(
"ldap.scope"
,
int64
(
searchRequest
.
Scope
)),
},
func
(
conn
*
ldap
.
Conn
)
(
cerr
error
)
{
result
,
cerr
=
conn
.
Search
(
searchRequest
)
return
})
return
result
,
err
}
// Modify issues a ModifyRequest to the LDAP server.
func
(
p
*
ConnectionPool
)
Modify
(
ctx
context
.
Context
,
modifyRequest
*
ldap
.
ModifyRequest
)
error
{
return
p
.
doRequest
(
ctx
,
func
(
conn
*
ldap
.
Conn
)
error
{
return
p
.
doRequest
(
ctx
,
"ldap.Modify"
,
[]
trace
.
Attribute
{
trace
.
StringAttribute
(
"ldap.dn"
,
modifyRequest
.
DN
),
},
func
(
conn
*
ldap
.
Conn
)
error
{
return
conn
.
Modify
(
modifyRequest
)
})
}
// Add issues an AddRequest to the LDAP server.
func
(
p
*
ConnectionPool
)
Add
(
ctx
context
.
Context
,
addRequest
*
ldap
.
AddRequest
)
error
{
return
p
.
doRequest
(
ctx
,
func
(
conn
*
ldap
.
Conn
)
error
{
return
p
.
doRequest
(
ctx
,
"ldap.Add"
,
[]
trace
.
Attribute
{
trace
.
StringAttribute
(
"ldap.dn"
,
addRequest
.
DN
),
},
func
(
conn
*
ldap
.
Conn
)
error
{
return
conn
.
Add
(
addRequest
)
})
}
...
...
@@ -219,3 +241,16 @@ func isTemporaryLDAPError(err error) bool {
return
false
}
}
func
errorToTraceStatus
(
err
error
)
trace
.
Status
{
switch
err
{
case
nil
:
return
trace
.
Status
{
Code
:
trace
.
StatusCodeOK
,
Message
:
"OK"
}
case
context
.
Canceled
:
return
trace
.
Status
{
Code
:
trace
.
StatusCodeCancelled
,
Message
:
"CANCELED"
}
case
context
.
DeadlineExceeded
:
return
trace
.
Status
{
Code
:
trace
.
StatusCodeDeadlineExceeded
,
Message
:
"DEADLINE_EXCEEDED"
}
default
:
return
trace
.
Status
{
Code
:
trace
.
StatusCodeUnknown
,
Message
:
err
.
Error
()}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment