2019-02-20 19:51:33 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright 2018 gRPC 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 binarylog implementation binary logging as defined in
|
|
|
|
// https://github.com/grpc/proposal/blob/master/A16-binary-logging.md.
|
|
|
|
package binarylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Logger is the global binary logger. It can be used to get binary logger for
|
|
|
|
// each method.
|
|
|
|
type Logger interface {
|
|
|
|
getMethodLogger(methodName string) *MethodLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
// binLogger is the global binary logger for the binary. One of this should be
|
command: Unmanaged providers
This adds supports for "unmanaged" providers, or providers with process
lifecycles not controlled by Terraform. These providers are assumed to
be started before Terraform is launched, and are assumed to shut
themselves down after Terraform has finished running.
To do this, we must update the go-plugin dependency to v1.3.0, which
added support for the "test mode" plugin serving that powers all this.
As a side-effect of not needing to manage the process lifecycle anymore,
Terraform also no longer needs to worry about the provider's binary, as
it won't be used for anything anymore. Because of this, we can disable
the init behavior that concerns itself with downloading that provider's
binary, checking its version, and otherwise managing the binary.
This is all managed on a per-provider basis, so managed providers that
Terraform downloads, starts, and stops can be used in the same commands
as unmanaged providers. The TF_REATTACH_PROVIDERS environment variable
is added, and is a JSON encoding of the provider's address to the
information we need to connect to it.
This change enables two benefits: first, delve and other debuggers can
now be attached to provider server processes, and Terraform can connect.
This allows for attaching debuggers to provider processes, which before
was difficult to impossible. Second, it allows the SDK test framework to
host the provider in the same process as the test driver, while running
a production Terraform binary against the provider. This allows for Go's
built-in race detector and test coverage tooling to work as expected in
provider tests.
Unmanaged providers are expected to work in the exact same way as
managed providers, with one caveat: Terraform kills provider processes
and restarts them once per graph walk, meaning multiple times during
most Terraform CLI commands. As unmanaged providers can't be killed by
Terraform, and have no visibility into graph walks, unmanaged providers
are likely to have differences in how their global mutable state behaves
when compared to managed providers. Namely, unmanaged providers are
likely to retain global state when managed providers would have reset
it. Developers relying on global state should be aware of this.
2020-05-27 02:48:57 +02:00
|
|
|
// built at init time from the configuration (environment variable or flags).
|
2019-02-20 19:51:33 +01:00
|
|
|
//
|
|
|
|
// It is used to get a methodLogger for each individual method.
|
|
|
|
var binLogger Logger
|
|
|
|
|
|
|
|
// SetLogger sets the binarg logger.
|
|
|
|
//
|
|
|
|
// Only call this at init time.
|
|
|
|
func SetLogger(l Logger) {
|
|
|
|
binLogger = l
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMethodLogger returns the methodLogger for the given methodName.
|
|
|
|
//
|
|
|
|
// methodName should be in the format of "/service/method".
|
|
|
|
//
|
|
|
|
// Each methodLogger returned by this method is a new instance. This is to
|
|
|
|
// generate sequence id within the call.
|
|
|
|
func GetMethodLogger(methodName string) *MethodLogger {
|
|
|
|
if binLogger == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return binLogger.getMethodLogger(methodName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
const envStr = "GRPC_BINARY_LOG_FILTER"
|
|
|
|
configStr := os.Getenv(envStr)
|
|
|
|
binLogger = NewLoggerFromConfigString(configStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
type methodLoggerConfig struct {
|
|
|
|
// Max length of header and message.
|
|
|
|
hdr, msg uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type logger struct {
|
|
|
|
all *methodLoggerConfig
|
|
|
|
services map[string]*methodLoggerConfig
|
|
|
|
methods map[string]*methodLoggerConfig
|
|
|
|
|
|
|
|
blacklist map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newEmptyLogger creates an empty logger. The map fields need to be filled in
|
|
|
|
// using the set* functions.
|
|
|
|
func newEmptyLogger() *logger {
|
|
|
|
return &logger{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set method logger for "*".
|
|
|
|
func (l *logger) setDefaultMethodLogger(ml *methodLoggerConfig) error {
|
|
|
|
if l.all != nil {
|
|
|
|
return fmt.Errorf("conflicting global rules found")
|
|
|
|
}
|
|
|
|
l.all = ml
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set method logger for "service/*".
|
|
|
|
//
|
|
|
|
// New methodLogger with same service overrides the old one.
|
|
|
|
func (l *logger) setServiceMethodLogger(service string, ml *methodLoggerConfig) error {
|
|
|
|
if _, ok := l.services[service]; ok {
|
command: Unmanaged providers
This adds supports for "unmanaged" providers, or providers with process
lifecycles not controlled by Terraform. These providers are assumed to
be started before Terraform is launched, and are assumed to shut
themselves down after Terraform has finished running.
To do this, we must update the go-plugin dependency to v1.3.0, which
added support for the "test mode" plugin serving that powers all this.
As a side-effect of not needing to manage the process lifecycle anymore,
Terraform also no longer needs to worry about the provider's binary, as
it won't be used for anything anymore. Because of this, we can disable
the init behavior that concerns itself with downloading that provider's
binary, checking its version, and otherwise managing the binary.
This is all managed on a per-provider basis, so managed providers that
Terraform downloads, starts, and stops can be used in the same commands
as unmanaged providers. The TF_REATTACH_PROVIDERS environment variable
is added, and is a JSON encoding of the provider's address to the
information we need to connect to it.
This change enables two benefits: first, delve and other debuggers can
now be attached to provider server processes, and Terraform can connect.
This allows for attaching debuggers to provider processes, which before
was difficult to impossible. Second, it allows the SDK test framework to
host the provider in the same process as the test driver, while running
a production Terraform binary against the provider. This allows for Go's
built-in race detector and test coverage tooling to work as expected in
provider tests.
Unmanaged providers are expected to work in the exact same way as
managed providers, with one caveat: Terraform kills provider processes
and restarts them once per graph walk, meaning multiple times during
most Terraform CLI commands. As unmanaged providers can't be killed by
Terraform, and have no visibility into graph walks, unmanaged providers
are likely to have differences in how their global mutable state behaves
when compared to managed providers. Namely, unmanaged providers are
likely to retain global state when managed providers would have reset
it. Developers relying on global state should be aware of this.
2020-05-27 02:48:57 +02:00
|
|
|
return fmt.Errorf("conflicting service rules for service %v found", service)
|
2019-02-20 19:51:33 +01:00
|
|
|
}
|
|
|
|
if l.services == nil {
|
|
|
|
l.services = make(map[string]*methodLoggerConfig)
|
|
|
|
}
|
|
|
|
l.services[service] = ml
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set method logger for "service/method".
|
|
|
|
//
|
|
|
|
// New methodLogger with same method overrides the old one.
|
|
|
|
func (l *logger) setMethodMethodLogger(method string, ml *methodLoggerConfig) error {
|
|
|
|
if _, ok := l.blacklist[method]; ok {
|
command: Unmanaged providers
This adds supports for "unmanaged" providers, or providers with process
lifecycles not controlled by Terraform. These providers are assumed to
be started before Terraform is launched, and are assumed to shut
themselves down after Terraform has finished running.
To do this, we must update the go-plugin dependency to v1.3.0, which
added support for the "test mode" plugin serving that powers all this.
As a side-effect of not needing to manage the process lifecycle anymore,
Terraform also no longer needs to worry about the provider's binary, as
it won't be used for anything anymore. Because of this, we can disable
the init behavior that concerns itself with downloading that provider's
binary, checking its version, and otherwise managing the binary.
This is all managed on a per-provider basis, so managed providers that
Terraform downloads, starts, and stops can be used in the same commands
as unmanaged providers. The TF_REATTACH_PROVIDERS environment variable
is added, and is a JSON encoding of the provider's address to the
information we need to connect to it.
This change enables two benefits: first, delve and other debuggers can
now be attached to provider server processes, and Terraform can connect.
This allows for attaching debuggers to provider processes, which before
was difficult to impossible. Second, it allows the SDK test framework to
host the provider in the same process as the test driver, while running
a production Terraform binary against the provider. This allows for Go's
built-in race detector and test coverage tooling to work as expected in
provider tests.
Unmanaged providers are expected to work in the exact same way as
managed providers, with one caveat: Terraform kills provider processes
and restarts them once per graph walk, meaning multiple times during
most Terraform CLI commands. As unmanaged providers can't be killed by
Terraform, and have no visibility into graph walks, unmanaged providers
are likely to have differences in how their global mutable state behaves
when compared to managed providers. Namely, unmanaged providers are
likely to retain global state when managed providers would have reset
it. Developers relying on global state should be aware of this.
2020-05-27 02:48:57 +02:00
|
|
|
return fmt.Errorf("conflicting blacklist rules for method %v found", method)
|
2019-02-20 19:51:33 +01:00
|
|
|
}
|
|
|
|
if _, ok := l.methods[method]; ok {
|
command: Unmanaged providers
This adds supports for "unmanaged" providers, or providers with process
lifecycles not controlled by Terraform. These providers are assumed to
be started before Terraform is launched, and are assumed to shut
themselves down after Terraform has finished running.
To do this, we must update the go-plugin dependency to v1.3.0, which
added support for the "test mode" plugin serving that powers all this.
As a side-effect of not needing to manage the process lifecycle anymore,
Terraform also no longer needs to worry about the provider's binary, as
it won't be used for anything anymore. Because of this, we can disable
the init behavior that concerns itself with downloading that provider's
binary, checking its version, and otherwise managing the binary.
This is all managed on a per-provider basis, so managed providers that
Terraform downloads, starts, and stops can be used in the same commands
as unmanaged providers. The TF_REATTACH_PROVIDERS environment variable
is added, and is a JSON encoding of the provider's address to the
information we need to connect to it.
This change enables two benefits: first, delve and other debuggers can
now be attached to provider server processes, and Terraform can connect.
This allows for attaching debuggers to provider processes, which before
was difficult to impossible. Second, it allows the SDK test framework to
host the provider in the same process as the test driver, while running
a production Terraform binary against the provider. This allows for Go's
built-in race detector and test coverage tooling to work as expected in
provider tests.
Unmanaged providers are expected to work in the exact same way as
managed providers, with one caveat: Terraform kills provider processes
and restarts them once per graph walk, meaning multiple times during
most Terraform CLI commands. As unmanaged providers can't be killed by
Terraform, and have no visibility into graph walks, unmanaged providers
are likely to have differences in how their global mutable state behaves
when compared to managed providers. Namely, unmanaged providers are
likely to retain global state when managed providers would have reset
it. Developers relying on global state should be aware of this.
2020-05-27 02:48:57 +02:00
|
|
|
return fmt.Errorf("conflicting method rules for method %v found", method)
|
2019-02-20 19:51:33 +01:00
|
|
|
}
|
|
|
|
if l.methods == nil {
|
|
|
|
l.methods = make(map[string]*methodLoggerConfig)
|
|
|
|
}
|
|
|
|
l.methods[method] = ml
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set blacklist method for "-service/method".
|
|
|
|
func (l *logger) setBlacklist(method string) error {
|
|
|
|
if _, ok := l.blacklist[method]; ok {
|
command: Unmanaged providers
This adds supports for "unmanaged" providers, or providers with process
lifecycles not controlled by Terraform. These providers are assumed to
be started before Terraform is launched, and are assumed to shut
themselves down after Terraform has finished running.
To do this, we must update the go-plugin dependency to v1.3.0, which
added support for the "test mode" plugin serving that powers all this.
As a side-effect of not needing to manage the process lifecycle anymore,
Terraform also no longer needs to worry about the provider's binary, as
it won't be used for anything anymore. Because of this, we can disable
the init behavior that concerns itself with downloading that provider's
binary, checking its version, and otherwise managing the binary.
This is all managed on a per-provider basis, so managed providers that
Terraform downloads, starts, and stops can be used in the same commands
as unmanaged providers. The TF_REATTACH_PROVIDERS environment variable
is added, and is a JSON encoding of the provider's address to the
information we need to connect to it.
This change enables two benefits: first, delve and other debuggers can
now be attached to provider server processes, and Terraform can connect.
This allows for attaching debuggers to provider processes, which before
was difficult to impossible. Second, it allows the SDK test framework to
host the provider in the same process as the test driver, while running
a production Terraform binary against the provider. This allows for Go's
built-in race detector and test coverage tooling to work as expected in
provider tests.
Unmanaged providers are expected to work in the exact same way as
managed providers, with one caveat: Terraform kills provider processes
and restarts them once per graph walk, meaning multiple times during
most Terraform CLI commands. As unmanaged providers can't be killed by
Terraform, and have no visibility into graph walks, unmanaged providers
are likely to have differences in how their global mutable state behaves
when compared to managed providers. Namely, unmanaged providers are
likely to retain global state when managed providers would have reset
it. Developers relying on global state should be aware of this.
2020-05-27 02:48:57 +02:00
|
|
|
return fmt.Errorf("conflicting blacklist rules for method %v found", method)
|
2019-02-20 19:51:33 +01:00
|
|
|
}
|
|
|
|
if _, ok := l.methods[method]; ok {
|
command: Unmanaged providers
This adds supports for "unmanaged" providers, or providers with process
lifecycles not controlled by Terraform. These providers are assumed to
be started before Terraform is launched, and are assumed to shut
themselves down after Terraform has finished running.
To do this, we must update the go-plugin dependency to v1.3.0, which
added support for the "test mode" plugin serving that powers all this.
As a side-effect of not needing to manage the process lifecycle anymore,
Terraform also no longer needs to worry about the provider's binary, as
it won't be used for anything anymore. Because of this, we can disable
the init behavior that concerns itself with downloading that provider's
binary, checking its version, and otherwise managing the binary.
This is all managed on a per-provider basis, so managed providers that
Terraform downloads, starts, and stops can be used in the same commands
as unmanaged providers. The TF_REATTACH_PROVIDERS environment variable
is added, and is a JSON encoding of the provider's address to the
information we need to connect to it.
This change enables two benefits: first, delve and other debuggers can
now be attached to provider server processes, and Terraform can connect.
This allows for attaching debuggers to provider processes, which before
was difficult to impossible. Second, it allows the SDK test framework to
host the provider in the same process as the test driver, while running
a production Terraform binary against the provider. This allows for Go's
built-in race detector and test coverage tooling to work as expected in
provider tests.
Unmanaged providers are expected to work in the exact same way as
managed providers, with one caveat: Terraform kills provider processes
and restarts them once per graph walk, meaning multiple times during
most Terraform CLI commands. As unmanaged providers can't be killed by
Terraform, and have no visibility into graph walks, unmanaged providers
are likely to have differences in how their global mutable state behaves
when compared to managed providers. Namely, unmanaged providers are
likely to retain global state when managed providers would have reset
it. Developers relying on global state should be aware of this.
2020-05-27 02:48:57 +02:00
|
|
|
return fmt.Errorf("conflicting method rules for method %v found", method)
|
2019-02-20 19:51:33 +01:00
|
|
|
}
|
|
|
|
if l.blacklist == nil {
|
|
|
|
l.blacklist = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
l.blacklist[method] = struct{}{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getMethodLogger returns the methodLogger for the given methodName.
|
|
|
|
//
|
|
|
|
// methodName should be in the format of "/service/method".
|
|
|
|
//
|
|
|
|
// Each methodLogger returned by this method is a new instance. This is to
|
|
|
|
// generate sequence id within the call.
|
|
|
|
func (l *logger) getMethodLogger(methodName string) *MethodLogger {
|
|
|
|
s, m, err := parseMethodName(methodName)
|
|
|
|
if err != nil {
|
|
|
|
grpclog.Infof("binarylogging: failed to parse %q: %v", methodName, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ml, ok := l.methods[s+"/"+m]; ok {
|
|
|
|
return newMethodLogger(ml.hdr, ml.msg)
|
|
|
|
}
|
|
|
|
if _, ok := l.blacklist[s+"/"+m]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ml, ok := l.services[s]; ok {
|
|
|
|
return newMethodLogger(ml.hdr, ml.msg)
|
|
|
|
}
|
|
|
|
if l.all == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return newMethodLogger(l.all.hdr, l.all.msg)
|
|
|
|
}
|