Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide warning when $ is used in config for env var expansion #9162

Closed
bryan-aguilar opened this issue Dec 20, 2023 · 8 comments · Fixed by #10135
Closed

Provide warning when $ is used in config for env var expansion #9162

bryan-aguilar opened this issue Dec 20, 2023 · 8 comments · Fixed by #10135
Assignees

Comments

@bryan-aguilar
Copy link
Contributor

bryan-aguilar commented Dec 20, 2023

From Collector SIG Discussion 12/20/2023

Use of the $ in collector configurations for environment variable substitution will be deprecated in the future in favor of ${} or ${env:foo}. It would be nice if we started giving users warnings during collector startup if $ is used for env variable expansion to ease the transition pain.

@crobert-1
Copy link
Member

+1 on the warning message. It should also include the recommended syntax for expansion so users know exactly how to fix it as soon as they're aware the current method is being deprecated.

@tomasmota
Copy link
Contributor

I can make a pr on this

@mx-psi
Copy link
Member

mx-psi commented Jan 23, 2024

@tomasmota assigned to you :)

@tomasmota
Copy link
Contributor

tomasmota commented Jan 24, 2024

thanks @mx-psi , maybe you can give me a hint here. It seems like the only point at which we "know" we are dealing with a $ENV is inside this function we pass to os.Expand (https://github.com/open-telemetry/opentelemetry-collector/blob/main/confmap/converter/expandconverter/expand.go#L52). The thing is that it can be ran several times and we probably only want to print the warning once, so I suppose the solution would be setting some sort of flag from inside it? Not really sure how to approach it.

@mx-psi
Copy link
Member

mx-psi commented Jan 31, 2024

Hey @tomasmota, sorry for the delay.

It seems like the only point at which we "know" we are dealing with a $ENV is inside this function we pass to os.Expand

I would do this in the body of expandEnv (the function that calls os.Expand). Note that we want to log a warning if somebody is using the $ENV syntax, but not if they are using ${ENV}. os.Expand doesn't tell you this, so you probably want to use some sort of regex.

The thing is that it can be ran several times and we probably only want to print the warning once, so I suppose the solution would be setting some sort of flag from inside it? Not really sure how to approach it.

If you want to log once per environment variable, I think your best option is to keep a set of the environment variables for which you have already logged. You can add a new field to the converter struct of type map[string]struct{} and keep track of which variables you have already logged for.

One more thing here is that you don't really have a logger today. You can start with using a noop logger while I and other work on passing a logger to the expandconverter

bogdandrutu pushed a commit that referenced this issue Feb 1, 2024
…nd providers (#9443)

**Description:** 

For both #5615 and #9162 we need to be able to log during the confmap
resolution.

My proposed solution is to pass a `*zap.Logger` to converters and
providers during initialization. These components can then use this to
log any warnings they need. This PR does the first step: being able to
pass anything to converters and providers during initialization.

The obvious alternative to this is to change the interface of
`confmap.Provider` and `confmap.Converter` to pass any warnings in an
explicit struct. I think the `*zap.Logger` alternative is more natural
for developers of providers and converters: you just use a logger like
everywhere else in the Collector.

One problem for the Collector usage of `confmap` is: How does one pass a
`*zap.Logger` before knowing how a `*zap.Logger` should be configured? I
think we can work around this by:
1. Passing a special 'deferred' Logger that just stores the warnings
without actually logging them (we can use something like
`zaptest/observer` for this)
2. Resolving configuration
3. Building a `*zap.Logger` with said configuration
4. Logging the entries stored in (1) with the logger from (3) (using
`zaptest/observer` we can do that by taking the `zapcore.Core` out of
the logger and manually writing)

**We don't actually need ProviderSettings today, just ConverterSettings,
but I think it can still be useful.**

**Link to tracking Issue:** Relates to #5615 and #9162

---------

Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
mx-psi added a commit that referenced this issue Mar 26, 2024
**Description:** As requested by @mx-psi , added a no-op log for when
variables using the deprecated $VAR style are used. The logger should be
replaced once it is clear how to pass it down (see #9443). Also, from my
testing, the function passed to os.Expand is in fact only run when we
have $VAR and not for ${env:VAR}, so I did not add additional checking.

**Link to tracking Issue:** #9162 

**Testing:** I am not sure how to go about testing it, since we are not
passing a logger in yet, there is no easy way to know what is being
logged or what the map looks like. Some ideas on this would be
appreciated

---------

Co-authored-by: Pablo Baeyens <pablo.baeyens@datadoghq.com>
@mx-psi
Copy link
Member

mx-psi commented Mar 26, 2024

I just merged #9547 that adds the logging, we now need to actually pass a logger to close this!

@tomasmota
Copy link
Contributor

As far as I can see, so far no loggers are created before the service actually starts, any errors seem to just use fmt.Errorf, so I'm wondering what the best place is to create this logger. I was considering taking the LoggingOptions from CollectorSettings and creating a logger here, but afaik we don't have access to a LogsConfig, which is inside the service configuration itself which we can't access from there..

Could definitely use some pointers here :)

@mx-psi
Copy link
Member

mx-psi commented Apr 9, 2024

@tomasmota There is a draft PR (#9908) by @ankitpatel96 on this, we were discussing how to do this also with @evan-bradley. If you have an opinion on how to approach this, could you leave a review on #9908 or create an alternative PR?

codeboten added a commit that referenced this issue Apr 30, 2024
**Description:** Adds an RFC about how environment variable resolution
should work
**Link to tracking Issue:** Fixes #9515, relates to:
- #8215
- #8565
- #9162
- #9531 
- #9532

---------

Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
codeboten pushed a commit that referenced this issue May 6, 2024
#### Description

This is an RFC to help us decide how we want `otelcol` to provide a
logger before the primary logger is created. As we discuss I will update
the doc. Before this is merged we should have decided on a solution and
the Accepted Solution section must be updated.

Related to
#10056

#### Link to tracking issue
This unblocks:
- #9162
- #5615

---------

Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
Co-authored-by: Evan Bradley <11745660+evan-bradley@users.noreply.github.com>
mx-psi added a commit that referenced this issue May 10, 2024
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

Provides a logger to confmap that buffers logs in memory until the
primary logger can be used. Once the primary logger exists, places that
used the original logger are given the updated Core.

If an error occurs that would shut down the collector before the primary
logger could be created, the logs are written to stdout/err using a
fallback logger.

Alternative to
#10008

I've pushed the testing I did to show how the logger successfully
updates. Before config resolution the debug log in confmap is not
printed, but afterwards it is.

test config:
```yaml
receivers:
  nop:

exporters:
  otlphttp:
    endpoint: http://0.0.0.0:4317
    headers:
      # Not set
      x-test: ${env:TEMP3}
  debug:
    # set to "detailed"
    verbosity: $TEMP

service:
  telemetry:
    logs:
      level: debug
  pipelines:
    traces:
      receivers:
        - nop
      exporters:
        - debug
```


![image](https://github.com/open-telemetry/opentelemetry-collector/assets/12352919/6a17993f-1f97-4c54-9165-5c34dd58d108)

<!-- Issue number if applicable -->
#### Link to tracking issue
Related to
#9162
Related to
#5615

<!--Describe what testing was performed and which tests were added.-->
#### Testing
If we like this approach I'll add tests

<!--Describe the documentation added.-->
#### Documentation

---------

Co-authored-by: Dan Jaglowski <jaglows3@gmail.com>
Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
mx-psi pushed a commit that referenced this issue May 10, 2024
#### Description
Adds a Logger to ConverterSettings to enable logging from within
converters. Also update the expand converter to log a warning if the env
var is empty or missing.

#### Link to tracking issue
Closes
#9162
Closes
#5615

#### Testing

Unit tests and local testing


![image](https://github.com/open-telemetry/opentelemetry-collector/assets/12352919/af5dd1e2-62f9-4272-97c7-da57166ef07e)

```yaml
receivers:
  nop:

exporters:
  otlphttp:
    endpoint: http://0.0.0.0:4317
    headers:
      # Not set
      x-test: $TEMP3
  debug:
    # set to "detailed"
    verbosity: $TEMP

service:
  telemetry:
    logs:
      level: info
  pipelines:
    traces:
      receivers:
        - nop
      exporters:
        - otlphttp
        - debug
```

#### Documentation
Added godoc comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants