config.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright The OpenTelemetry Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package stdouttrace // import "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
  15. import (
  16. "io"
  17. "os"
  18. )
  19. var (
  20. defaultWriter = os.Stdout
  21. defaultPrettyPrint = false
  22. defaultTimestamps = true
  23. )
  24. // config contains options for the STDOUT exporter.
  25. type config struct {
  26. // Writer is the destination. If not set, os.Stdout is used.
  27. Writer io.Writer
  28. // PrettyPrint will encode the output into readable JSON. Default is
  29. // false.
  30. PrettyPrint bool
  31. // Timestamps specifies if timestamps should be printed. Default is
  32. // true.
  33. Timestamps bool
  34. }
  35. // newConfig creates a validated Config configured with options.
  36. func newConfig(options ...Option) (config, error) {
  37. cfg := config{
  38. Writer: defaultWriter,
  39. PrettyPrint: defaultPrettyPrint,
  40. Timestamps: defaultTimestamps,
  41. }
  42. for _, opt := range options {
  43. cfg = opt.apply(cfg)
  44. }
  45. return cfg, nil
  46. }
  47. // Option sets the value of an option for a Config.
  48. type Option interface {
  49. apply(config) config
  50. }
  51. // WithWriter sets the export stream destination.
  52. func WithWriter(w io.Writer) Option {
  53. return writerOption{w}
  54. }
  55. type writerOption struct {
  56. W io.Writer
  57. }
  58. func (o writerOption) apply(cfg config) config {
  59. cfg.Writer = o.W
  60. return cfg
  61. }
  62. // WithPrettyPrint prettifies the emitted output.
  63. func WithPrettyPrint() Option {
  64. return prettyPrintOption(true)
  65. }
  66. type prettyPrintOption bool
  67. func (o prettyPrintOption) apply(cfg config) config {
  68. cfg.PrettyPrint = bool(o)
  69. return cfg
  70. }
  71. // WithoutTimestamps sets the export stream to not include timestamps.
  72. func WithoutTimestamps() Option {
  73. return timestampsOption(false)
  74. }
  75. type timestampsOption bool
  76. func (o timestampsOption) apply(cfg config) config {
  77. cfg.Timestamps = bool(o)
  78. return cfg
  79. }