/**
 * Copyright 2017 VMware, Inc.
 * <p>
 * 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
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 io.micrometer.statsd.internal;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Statistic;
import io.micrometer.core.instrument.config.NamingConvention;
import io.micrometer.core.lang.Nullable;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public class TelegrafStatsdLineBuilder extends FlavorStatsdLineBuilder {
    private final Object conventionTagsLock = new Object();
    @SuppressWarnings({"NullableProblems", "unused"})
    private volatile NamingConvention namingConvention;
    @SuppressWarnings("NullableProblems")
    private volatile String name;
    @Nullable
    private volatile String conventionTags;
    @SuppressWarnings("NullableProblems")
    private volatile String tagsNoStat;
    private final ConcurrentMap<Statistic, String> tags = new ConcurrentHashMap<>();

    public TelegrafStatsdLineBuilder(Meter.Id id, MeterRegistry.Config config) {
        super(id, config);
    }

    @Override
    String line(String amount, @Nullable Statistic stat, String type) {
        updateIfNamingConventionChanged();
        return name + tagsByStatistic(stat) + ":" + amount + "|" + type;
    }

    private void updateIfNamingConventionChanged() {
        NamingConvention next = config.namingConvention();
        if (this.namingConvention != next) {
            synchronized (conventionTagsLock) {
                if (this.namingConvention == next) {
                    return;
                }
                this.tags.clear();
                this.conventionTags = id.getTagsAsIterable().iterator().hasNext() ?
                        id.getConventionTags(next).stream()
                                .map(t -> telegrafEscape(t.getKey()) + "=" + telegrafEscape(t.getValue()))
                                .collect(Collectors.joining(","))
                        : null;
            }
            this.name = telegrafEscape(next.name(id.getName(), id.getType(), id.getBaseUnit()));
            this.tagsNoStat = tags(null, conventionTags, "=", ",");
            this.namingConvention = next;
        }
    }

    private String tagsByStatistic(@Nullable Statistic stat) {
        return stat == null ? tagsNoStat : tags.computeIfAbsent(stat, this::telegrafTag);
    }

    private String telegrafTag(@Nullable Statistic stat) {
        synchronized (conventionTagsLock) {
            return tags(stat, conventionTags, "=", ",");
        }
    }

    /**
     * Backslash escape '=' works fine.
     * <p>
     * Trying to escape spaces and commas causes the rest of the name to be dropped by telegraf.
     * Trying to escape colons doesn't work. All of these must be replaced.
     */
    // backslash escape =
    // trying to escape spaces and comma drops everything after that
    private String telegrafEscape(String value) {
        return value.replaceAll("=", "\\=")
                .replaceAll("[\\s,:]", "_");
    }
}

@@ -2880,23 +2880,20 @@
         
-return 
+if (
 stat == 
@@ -2900,136 +2900,158 @@
 null
- ? tagsNoStat : tags.computeIfAbsent(stat, this::telegrafTag);
+) %7B%0A            return tagsNoStat;%0A        %7D%0A        String tags = this.tags.get(stat);%0A        if (tags != null) %7B
 %0A    
-%7D%0A%0A
+  
     
-private String telegrafTag(@Nullable Statistic stat) %7B
+  return tags;%0A        %7D
 %0A   
@@ -3105,24 +3105,65 @@
       return
+ this.tags.computeIfAbsent(stat, (key) -%3E
  tags(stat, 
@@ -3179,32 +3179,33 @@
 nTags, %22=%22, %22,%22)
+)
 ;%0A        %7D%0A    

Fix deadlock in TelegrafStatsdLineBuilder (#2326)